diff --git a/mkl_umath/src/ufuncsmodule.c b/mkl_umath/src/ufuncsmodule.c index 50dd7790..7fc25435 100644 --- a/mkl_umath/src/ufuncsmodule.c +++ b/mkl_umath/src/ufuncsmodule.c @@ -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. */ + import_array(); + import_umath(); + m = PyModule_Create(&_ufuncs_module); if (m == NULL) return NULL; @@ -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);