Skip to content

Commit 760440b

Browse files
committed
Convert multiarray to multi-phase init (PEP 489)
1 parent bbdebae commit 760440b

File tree

2 files changed

+56
-62
lines changed

2 files changed

+56
-62
lines changed

numpy/_core/src/multiarray/_multiarray_tests.c.src

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,41 +2413,40 @@ static PyMethodDef Multiarray_TestsMethods[] = {
24132413
};
24142414

24152415

2416-
static struct PyModuleDef moduledef = {
2417-
PyModuleDef_HEAD_INIT,
2418-
"_multiarray_tests",
2419-
NULL,
2420-
-1,
2421-
Multiarray_TestsMethods,
2422-
NULL,
2423-
NULL,
2424-
NULL,
2425-
NULL
2426-
};
2427-
2428-
PyMODINIT_FUNC PyInit__multiarray_tests(void)
2416+
static int
2417+
_multiarray_tests_exec(PyObject *m)
24292418
{
2430-
PyObject *m;
2431-
2432-
m = PyModule_Create(&moduledef);
2433-
if (m == NULL) {
2434-
return m;
2435-
}
2436-
import_array();
2419+
import_array1(-1);
24372420
if (init_argparse_mutex() < 0) {
2438-
return NULL;
2439-
}
2440-
if (PyErr_Occurred()) {
2441-
PyErr_SetString(PyExc_RuntimeError,
2442-
"cannot load _multiarray_tests module.");
2421+
return -1;
24432422
}
24442423

2445-
#if Py_GIL_DISABLED
2446-
// signal this module supports running with the GIL disabled
2447-
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
2424+
return 0;
2425+
}
2426+
2427+
static struct PyModuleDef_Slot _multiarray_tests_slots[] = {
2428+
{Py_mod_exec, _multiarray_tests_exec},
2429+
#if PY_VERSION_HEX >= 0x030c00f0 // Python 3.12+
2430+
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
24482431
#endif
2432+
#if PY_VERSION_HEX >= 0x030d00f0 // Python 3.13+
2433+
// signal that this module supports running without an active GIL
2434+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
2435+
#endif
2436+
{0, NULL},
2437+
};
24492438

2450-
return m;
2439+
static struct PyModuleDef moduledef = {
2440+
.m_base = PyModuleDef_HEAD_INIT,
2441+
.m_name = "_multiarray_tests",
2442+
.m_size = -1,
2443+
.m_methods = Multiarray_TestsMethods,
2444+
.m_slots = _multiarray_tests_slots,
2445+
};
2446+
2447+
PyMODINIT_FUNC PyInit__multiarray_tests(void)
2448+
{
2449+
return PyModuleDef_Init(&moduledef);
24512450
}
24522451

24532452
NPY_NO_EXPORT int

numpy/_core/src/multiarray/multiarraymodule.c

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4773,28 +4773,9 @@ initialize_thread_unsafe_state(void) {
47734773
return 0;
47744774
}
47754775

4776-
static struct PyModuleDef moduledef = {
4777-
PyModuleDef_HEAD_INIT,
4778-
"_multiarray_umath",
4779-
NULL,
4780-
-1,
4781-
array_module_methods,
4782-
NULL,
4783-
NULL,
4784-
NULL,
4785-
NULL
4786-
};
4787-
4788-
/* Initialization function for the module */
4789-
PyMODINIT_FUNC PyInit__multiarray_umath(void) {
4790-
PyObject *m, *d, *s;
4791-
PyObject *c_api;
4792-
4793-
/* Create the module and add the functions */
4794-
m = PyModule_Create(&moduledef);
4795-
if (!m) {
4796-
return NULL;
4797-
}
4776+
static int
4777+
_multiarray_umath_exec(PyObject *m) {
4778+
PyObject *d, *s, *c_api;
47984779

47994780
/* Initialize CPU features */
48004781
if (npy_cpu_init() < 0) {
@@ -5135,18 +5116,32 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) {
51355116
goto err;
51365117
}
51375118

5138-
#if Py_GIL_DISABLED
5139-
// signal this module supports running with the GIL disabled
5140-
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
5119+
return 0;
5120+
5121+
err:
5122+
return -1;
5123+
}
5124+
5125+
static struct PyModuleDef_Slot _multiarray_umath_slots[] = {
5126+
{Py_mod_exec, _multiarray_umath_exec},
5127+
#if PY_VERSION_HEX >= 0x030c00f0 // Python 3.12+
5128+
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
5129+
#endif
5130+
#if PY_VERSION_HEX >= 0x030d00f0 // Python 3.13+
5131+
// signal that this module supports running without an active GIL
5132+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
51415133
#endif
5134+
{0, NULL},
5135+
};
51425136

5143-
return m;
5137+
static struct PyModuleDef moduledef = {
5138+
.m_base = PyModuleDef_HEAD_INIT,
5139+
.m_name = "_multiarray_umath",
5140+
.m_size = -1,
5141+
.m_methods = array_module_methods,
5142+
.m_slots = _multiarray_umath_slots,
5143+
};
51445144

5145-
err:
5146-
if (!PyErr_Occurred()) {
5147-
PyErr_SetString(PyExc_RuntimeError,
5148-
"cannot load multiarray module.");
5149-
}
5150-
Py_DECREF(m);
5151-
return NULL;
5145+
PyMODINIT_FUNC PyInit__multiarray_umath(void) {
5146+
return PyModuleDef_Init(&moduledef);
51525147
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy