question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`PyObject_IsInstance` not working for `FunctionType` checks

See original GitHub issue

When doing an instance check of a Python function compiled with Nuitka against the stdlib class types.FunctionType, it should return True. It does in pure Python code, i.e. using isinstance. However, it does not when using PyObject_IsInstance inside a Python extension module.

My minimal example consists of the extension module (check.c) and the Python module that gets compiled with Nuitka (main.py).

  • check.c
#include <Python.h>

static PyObject *check_check(PyObject *self, PyObject *args) {
    PyObject *obj;
    PyArg_ParseTuple(args, "O", &obj);

    PyObject *types = PyImport_ImportModule("types");
    PyObject *FunctionType = PyObject_GetAttrString(types, "FunctionType");
    if (PyObject_IsInstance(obj, FunctionType)) {
        Py_RETURN_TRUE;
    } else {
        Py_RETURN_FALSE;
    }
}

static PyMethodDef check_methods[] = {
    {"check",  check_check, METH_VARARGS, "do check"},
    {NULL, NULL, 0, NULL},
};

static struct PyModuleDef checkmodule = {
    PyModuleDef_HEAD_INIT,
    "check",
    NULL,
    0,
    check_methods,
};

PyMODINIT_FUNC PyInit_check(void) {
    return PyModule_Create(&checkmodule);
}
  • main.py
from check import check

def foo():
    pass

assert check(foo)

Compile check.c to an extension module, place it in the Python path and run main.py with Python 3. The assert passes and no output is generated.

Now compile main.py with Nuitka and run the resulting binary:

$ python3 -m nuitka --nofollow-imports main.py
$ ./main.bin
Traceback (most recent call last):
  File "/tmp/venv/main.py", line 6, in <module>
    assert check(foo)
AssertionError

Clearly the instance check inside the extension module fails.


  • Nuitka version, full Python version and Platform

    • Nuitka: 0.6.5rc5 (also tested with 0.6.4)
    • Python: 3.6.7 (default, Oct 22 2018, 11:32:17)
    • OS: Linux (Ubuntu 18.04 on WSL)
    • Arch: x86_64
  • How did you install Nuitka and Python?

$ python3 -m venv venv
$ source venv/bin/activate
$ pip install http://nuitka.net/releases/Nuitka-0.6.5rc5.tar.gz

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:29 (20 by maintainers)

github_iconTop GitHub Comments

1reaction
kayhayencommented, Nov 10, 2021

That’s fantastic, seems pydantic can be considered supported soon. I need to cover my bases here though, please don’t be upset if this is not yet the default with 0.6.18, but you will be able to enable it yourself.

0reactions
kayhayencommented, Feb 20, 2022

With 0.7 this works out of the box and the experimental flag makes no difference anymore.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How do I detect whether a variable is a function?
The proper way to check properties of duck-typed objects is to ask them if they quack, not to see if they fit in...
Read more >
Python Type Checking (Guide) - Real Python
In this guide, you'll look at Python type checking. Traditionally, types have been handled by the Python interpreter in a flexible but implicit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found