Skip to content

Commit 681567a

Browse files
antonwolfyclaude
andcommitted
Avoid leaking module object on numpy C-API import failure
import_array() and import_umath() are macros that expand to `return NULL;` on failure. When they were called after PyModule_Create(), a failing import would return directly out of PyInit__ufuncs without releasing the module object, leaking the strong reference created by PyModule_Create(). Move the imports before the module is created so there is no owned reference to leak when an import fails. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 5202088 commit 681567a

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

‎mkl_umath/src/ufuncsmodule.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ PyMODINIT_FUNC PyInit__ufuncs(void)
4040
PyObject *m;
4141
PyObject *d;
4242

43+
/* import_array()/import_umath() expand to `return NULL;` on failure, so
44+
* call them before creating the module object to avoid leaking it. */
45+
import_array();
46+
import_umath();
47+
4348
m = PyModule_Create(&_ufuncs_module);
4449
if (m == NULL)
4550
return NULL;
@@ -50,9 +55,6 @@ PyMODINIT_FUNC PyInit__ufuncs(void)
5055
return NULL;
5156
}
5257

53-
import_array();
54-
import_umath();
55-
5658
if (InitOperators(d) < 0) {
5759
Py_XDECREF(d);
5860
Py_XDECREF(m);

0 commit comments

Comments
 (0)