Skip to content

Commit 92b2cca

Browse files
committed
Convert pocketfft_umath to multi-phase init (PEP 489)
1 parent 0145d7c commit 92b2cca

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

numpy/fft/_pocketfft_umath.cpp

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -388,41 +388,58 @@ add_gufuncs(PyObject *dictionary) {
388388
return 0;
389389
}
390390

391-
static struct PyModuleDef moduledef = {
392-
PyModuleDef_HEAD_INIT,
393-
"_multiarray_umath",
394-
NULL,
395-
-1,
396-
NULL,
397-
NULL,
398-
NULL,
399-
NULL,
400-
NULL
401-
};
391+
static int module_loaded = 0;
402392

403-
/* Initialization function for the module */
404-
PyMODINIT_FUNC PyInit__pocketfft_umath(void)
393+
static int
394+
_pocketfft_umath_exec(PyObject *m)
405395
{
406-
PyObject *m = PyModule_Create(&moduledef);
407-
if (m == NULL) {
408-
return NULL;
396+
// https://docs.python.org/3/howto/isolating-extensions.html#opt-out-limiting-to-one-module-object-per-process
397+
if (module_loaded) {
398+
PyErr_SetString(PyExc_ImportError,
399+
"cannot load module more than once per process");
400+
return -1;
409401
}
402+
module_loaded = 1;
410403

411404
/* Import the array and ufunc objects */
412-
import_array();
413-
import_ufunc();
405+
if (PyArray_ImportNumPyAPI() < 0) {
406+
return -1;
407+
}
408+
if (PyUFunc_ImportUFuncAPI() < 0) {
409+
return -1;
410+
}
414411

415412
PyObject *d = PyModule_GetDict(m);
416413
if (add_gufuncs(d) < 0) {
417414
Py_DECREF(d);
418-
Py_DECREF(m);
419-
return NULL;
415+
return -1;
420416
}
417+
Py_DECREF(d);
421418

422-
#if Py_GIL_DISABLED
423-
// signal this module supports running with the GIL disabled
424-
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
419+
return 0;
420+
}
421+
422+
static struct PyModuleDef_Slot _pocketfft_umath_slots[] = {
423+
{Py_mod_exec, (void*)_pocketfft_umath_exec},
424+
#if PY_VERSION_HEX >= 0x030c00f0 // Python 3.12+
425+
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
426+
#endif
427+
#if PY_VERSION_HEX >= 0x030d00f0 // Python 3.13+
428+
// signal that this module supports running without an active GIL
429+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
425430
#endif
431+
{0, NULL},
432+
};
433+
434+
static struct PyModuleDef moduledef = {
435+
PyModuleDef_HEAD_INIT, /* m_base */
436+
"_pocketfft_umath", /* m_name */
437+
NULL, /* m_doc */
438+
0, /* m_size */
439+
NULL, /* m_methods */
440+
_pocketfft_umath_slots, /* m_slots */
441+
};
426442

427-
return m;
443+
PyMODINIT_FUNC PyInit__pocketfft_umath(void) {
444+
return PyModuleDef_Init(&moduledef);
428445
}

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