Skip to content
Open
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
8 changes: 5 additions & 3 deletions mkl_umath/src/ufuncsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ PyMODINIT_FUNC PyInit__ufuncs(void)
PyObject *m;
PyObject *d;

/* import_array()/import_umath() expand to `return NULL;` on failure, so
* call them before creating the module object to avoid leaking it. */
Comment on lines +43 to +44

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if these return NULL, why would the module object leak? It's not clear to me, we don't check if the imports are NULL anywhere either

@antonwolfy antonwolfy Aug 31, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will be leak (if any import_array()/import_umath() failed and call return NULL) if calls placed after m = PyModule_Create(&_ufuncs_module); since no Py_XDECREF(m)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, these functions are macros internally in NumPy which expand to 'return NULL' or 'return' respectively, so it actually crashes later or unexpectedly

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed it was using PyImport_ImportModule. In that case, maybe make the comment a bit clearer for future reference

import_array();
import_umath();

m = PyModule_Create(&_ufuncs_module);
if (m == NULL)
return NULL;
Expand All @@ -50,9 +55,6 @@ PyMODINIT_FUNC PyInit__ufuncs(void)
return NULL;
}

import_array();
import_umath();

if (InitOperators(d) < 0) {
Py_XDECREF(d);
Py_XDECREF(m);
Expand Down
Loading