Closed hauntsaninja closed 1 month ago
cc @ericsnowcurrently in case you happen to have time and kindness to take a look and offer any advice :-)
Here's a repro that doesn't involve mypyc. I'll read more about multi-phase init tomorrow, maybe imports in single init is not allowed or something. mypyc does globals, which I think multi-phase init doesn't like, would be a big change
printf '
#define PY_SSIZE_T_CLEAN
#include <Python.h>
static struct PyModuleDef nativemodule = {
PyModuleDef_HEAD_INIT,
.m_name = "native",
};
PyObject* module = NULL;
PyMODINIT_FUNC PyInit_native(void) {
if (module) {
Py_INCREF(module);
return module;
}
module = PyModule_Create(&nativemodule);
assert(module);
Py_XDECREF(PyImport_ImportModule("non_native"));
PySys_WriteStdout("hello from native\\n");
return module;
}
' > native.c
printf 'import native # circular import' > non_native.py
printf '
from setuptools import setup, Extension
setup(name="native", ext_modules=[Extension("native", sources=["native.c"])])
' > setup.py
python setup.py build_ext --inplace
python -c 'import native'
My understanding is that multi-phase init would solve the issue. I also encountered this when running tests on the release candidates, but I haven't had time to look into it in detail yet (and probably won't have time in the next week or two at least, unfortunately). Several mypyc tests fail because of this issue.
We've been relying on circular imports working without multi-phase init for years now, even if they haven't been officially supported by the Python C API.
Just to be clear, this also blocks compiling mypy on 3.13 release candidates, since mypy contains (many) import cycles.
I opened https://github.com/python/cpython/issues/123880 upstream
This was fixed upstream, we should be all set for 3.13.0
Would be nice to make a release, I opened https://github.com/python/mypy/issues/17815
@cdce8p already bisected this upstream to https://github.com/python/cpython/pull/118532/
To Reproduce
Using a debug build of Python, you get:
Using a non-debug build, you get:
mypyc transpiles Python code to an extension module that uses the C API. In the above, we compile native.py into an extension module and form an import cycle with a normal pure Python module
The C code mypyc generates is visible in the build directory