@@ -388,41 +388,58 @@ add_gufuncs(PyObject *dictionary) {
388
388
return 0 ;
389
389
}
390
390
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 ;
402
392
403
- /* Initialization function for the module */
404
- PyMODINIT_FUNC PyInit__pocketfft_umath ( void )
393
+ static int
394
+ _pocketfft_umath_exec (PyObject *m )
405
395
{
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 ;
409
401
}
402
+ module_loaded = 1 ;
410
403
411
404
/* 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
+ }
414
411
415
412
PyObject *d = PyModule_GetDict (m);
416
413
if (add_gufuncs (d) < 0 ) {
417
414
Py_DECREF (d);
418
- Py_DECREF (m);
419
- return NULL ;
415
+ return -1 ;
420
416
}
417
+ Py_DECREF (d);
421
418
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},
425
430
#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
+ };
426
442
427
- return m;
443
+ PyMODINIT_FUNC PyInit__pocketfft_umath (void ) {
444
+ return PyModuleDef_Init (&moduledef);
428
445
}
0 commit comments