`PyObject_IsInstance` not working for `FunctionType` checks
See original GitHub issueWhen 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:
- Created 4 years ago
- Comments:29 (20 by maintainers)
Top GitHub Comments
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.With 0.7 this works out of the box and the experimental flag makes no difference anymore.