🌐 AI搜索 & 代理 主页
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Convert multiarray to multi-phase init (PEP 489)
  • Loading branch information
AA-Turner committed May 29, 2025
commit d4b3f9bddf8cc1fe32f056a51e29f27ec0bdfea6
4 changes: 4 additions & 0 deletions numpy/_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
except ImportError as exc:
import sys

# Bypass for the module re-initialization opt-out
if exc.msg == "cannot load module more than once per process":
raise

# Basically always, the problem should be that the C module is wrong/missing...
if (
isinstance(exc, ModuleNotFoundError)
Expand Down
59 changes: 37 additions & 22 deletions numpy/_core/src/multiarray/_multiarray_tests.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -2413,41 +2413,56 @@ static PyMethodDef Multiarray_TestsMethods[] = {
};


static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_multiarray_tests",
NULL,
-1,
Multiarray_TestsMethods,
NULL,
NULL,
NULL,
NULL
};
static int module_loaded = 0;

PyMODINIT_FUNC PyInit__multiarray_tests(void)
static int
_multiarray_tests_exec(PyObject *m)
{
PyObject *m;
// https://docs.python.org/3/howto/isolating-extensions.html#opt-out-limiting-to-one-module-object-per-process
if (module_loaded) {
PyErr_SetString(PyExc_ImportError,
"cannot load module more than once per process");
return -1;
}
module_loaded = 1;

m = PyModule_Create(&moduledef);
if (m == NULL) {
return m;
if (PyArray_ImportNumPyAPI() < 0) {
return -1;
}
import_array();
if (init_argparse_mutex() < 0) {
return NULL;
return -1;
}
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_RuntimeError,
"cannot load _multiarray_tests module.");
}

#if Py_GIL_DISABLED
// signal this module supports running with the GIL disabled
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
return 0;
}

static struct PyModuleDef_Slot _multiarray_tests_slots[] = {
{Py_mod_exec, _multiarray_tests_exec},
#if PY_VERSION_HEX >= 0x030c00f0 // Python 3.12+
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
#endif
#if PY_VERSION_HEX >= 0x030d00f0 // Python 3.13+
// signal that this module supports running without an active GIL
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
#endif
{0, NULL},
};

static struct PyModuleDef moduledef = {
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "_multiarray_tests",
.m_size = 0,
.m_methods = Multiarray_TestsMethods,
.m_slots = _multiarray_tests_slots,
};

return m;
PyMODINIT_FUNC PyInit__multiarray_tests(void)
{
return PyModuleDef_Init(&moduledef);
}

NPY_NO_EXPORT int
Expand Down
Loading
Loading