summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Droettboom <mdboom@gmail.com>2018-03-20 22:07:08 -0400
committerMichael Droettboom <mdboom@gmail.com>2018-03-30 11:42:56 -0400
commit2a6565c6432c682c2798e75d48adc587da661822 (patch)
tree535f247d0b0f00c8b61db6e42ec5fc4768d7cc15
parent517f4c0749f83488d25493ccea62b91caa03c2d6 (diff)
downloadnumpy-2a6565c6432c682c2798e75d48adc587da661822.tar.gz
Return NULL from PyInit_* when exception is raised
I don't think this is documented anywhere, but I'm pretty sure module init functions should return NULL in order to communicate that an exception occurred during initialization (as is the standard Python/C API convention). It's clear from the CPython code [here](https://github.com/python/cpython/blob/master/Python/importdl.c#L162) that if you don't return NULL, the exception is swallowed and replaced with the message "initialization of %s raised unreported exception". Admittedly, this is only useful for people porting Numpy to new platforms where it is helpful to know where module initialization is failing, but it can't hurt.
-rw-r--r--numpy/core/src/multiarray/multiarraymodule.c31
-rw-r--r--numpy/core/src/umath/umathmodule.c14
-rw-r--r--numpy/fft/fftpack_litemodule.c9
-rw-r--r--numpy/linalg/lapack_litemodule.c8
-rw-r--r--numpy/linalg/umath_linalg.c.src9
5 files changed, 40 insertions, 31 deletions
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c
index 0008cb04b..7eccb4a4b 100644
--- a/numpy/core/src/multiarray/multiarraymodule.c
+++ b/numpy/core/src/multiarray/multiarraymodule.c
@@ -4732,10 +4732,10 @@ static struct PyModuleDef moduledef = {
/* Initialization function for the module */
#if defined(NPY_PY3K)
-#define RETVAL m
+#define RETVAL(x) x
PyMODINIT_FUNC PyInit_multiarray(void) {
#else
-#define RETVAL
+#define RETVAL(x)
PyMODINIT_FUNC initmultiarray(void) {
#endif
PyObject *m, *d, *s;
@@ -4763,6 +4763,10 @@ PyMODINIT_FUNC initmultiarray(void) {
/* Initialize access to the PyDateTime API */
numpy_pydatetime_import();
+ if (PyErr_Occurred()) {
+ goto err;
+ }
+
/* Add some symbolic constants to the module */
d = PyModule_GetDict(m);
if (!d) {
@@ -4776,7 +4780,7 @@ PyMODINIT_FUNC initmultiarray(void) {
*/
PyArray_Type.tp_hash = PyObject_HashNotImplemented;
if (PyType_Ready(&PyArray_Type) < 0) {
- return RETVAL;
+ goto err;
}
if (setup_scalartypes(d) < 0) {
goto err;
@@ -4786,32 +4790,32 @@ PyMODINIT_FUNC initmultiarray(void) {
PyArrayMultiIter_Type.tp_iter = PyObject_SelfIter;
PyArrayMultiIter_Type.tp_free = PyArray_free;
if (PyType_Ready(&PyArrayIter_Type) < 0) {
- return RETVAL;
+ goto err;
}
if (PyType_Ready(&PyArrayMapIter_Type) < 0) {
- return RETVAL;
+ goto err;
}
if (PyType_Ready(&PyArrayMultiIter_Type) < 0) {
- return RETVAL;
+ goto err;
}
PyArrayNeighborhoodIter_Type.tp_new = PyType_GenericNew;
if (PyType_Ready(&PyArrayNeighborhoodIter_Type) < 0) {
- return RETVAL;
+ goto err;
}
if (PyType_Ready(&NpyIter_Type) < 0) {
- return RETVAL;
+ goto err;
}
PyArrayDescr_Type.tp_hash = PyArray_DescrHash;
if (PyType_Ready(&PyArrayDescr_Type) < 0) {
- return RETVAL;
+ goto err;
}
if (PyType_Ready(&PyArrayFlags_Type) < 0) {
- return RETVAL;
+ goto err;
}
NpyBusDayCalendar_Type.tp_new = PyType_GenericNew;
if (PyType_Ready(&NpyBusDayCalendar_Type) < 0) {
- return RETVAL;
+ goto err;
}
c_api = NpyCapsule_FromVoidPtr((void *)PyArray_API, NULL);
@@ -4897,12 +4901,13 @@ PyMODINIT_FUNC initmultiarray(void) {
if (set_typeinfo(d) != 0) {
goto err;
}
- return RETVAL;
+
+ return RETVAL(m);
err:
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_RuntimeError,
"cannot load multiarray module.");
}
- return RETVAL;
+ return RETVAL(NULL);
}
diff --git a/numpy/core/src/umath/umathmodule.c b/numpy/core/src/umath/umathmodule.c
index 03bf5bfd8..15da831b2 100644
--- a/numpy/core/src/umath/umathmodule.c
+++ b/numpy/core/src/umath/umathmodule.c
@@ -310,10 +310,10 @@ static struct PyModuleDef moduledef = {
#include <stdio.h>
#if defined(NPY_PY3K)
-#define RETVAL m
+#define RETVAL(x) x
PyMODINIT_FUNC PyInit_umath(void)
#else
-#define RETVAL
+#define RETVAL(x)
PyMODINIT_FUNC initumath(void)
#endif
{
@@ -330,7 +330,7 @@ PyMODINIT_FUNC initumath(void)
m = Py_InitModule("umath", methods);
#endif
if (!m) {
- return RETVAL;
+ goto err;
}
/* Import the array */
@@ -339,12 +339,12 @@ PyMODINIT_FUNC initumath(void)
PyErr_SetString(PyExc_ImportError,
"umath failed: Could not import array core.");
}
- return RETVAL;
+ goto err;
}
/* Initialize the types */
if (PyType_Ready(&PyUFunc_Type) < 0)
- return RETVAL;
+ goto err;
/* Add some symbolic constants to the module */
d = PyModule_GetDict(m);
@@ -426,7 +426,7 @@ PyMODINIT_FUNC initumath(void)
goto err;
}
- return RETVAL;
+ return RETVAL(m);
err:
/* Check for errors */
@@ -434,5 +434,5 @@ PyMODINIT_FUNC initumath(void)
PyErr_SetString(PyExc_RuntimeError,
"cannot load umath module.");
}
- return RETVAL;
+ return RETVAL(NULL);
}
diff --git a/numpy/fft/fftpack_litemodule.c b/numpy/fft/fftpack_litemodule.c
index dfa0d211b..bd6cfc120 100644
--- a/numpy/fft/fftpack_litemodule.c
+++ b/numpy/fft/fftpack_litemodule.c
@@ -330,10 +330,10 @@ static struct PyModuleDef moduledef = {
/* Initialization function for the module */
#if PY_MAJOR_VERSION >= 3
-#define RETVAL m
+#define RETVAL(x) x
PyMODINIT_FUNC PyInit_fftpack_lite(void)
#else
-#define RETVAL
+#define RETVAL(x)
PyMODINIT_FUNC
initfftpack_lite(void)
#endif
@@ -348,6 +348,9 @@ initfftpack_lite(void)
fftpack_module_documentation,
(PyObject*)NULL,PYTHON_API_VERSION);
#endif
+ if (m == NULL) {
+ return RETVAL(NULL);
+ }
/* Import the array object */
import_array();
@@ -359,5 +362,5 @@ initfftpack_lite(void)
/* XXXX Add constants here */
- return RETVAL;
+ return RETVAL(m);
}
diff --git a/numpy/linalg/lapack_litemodule.c b/numpy/linalg/lapack_litemodule.c
index bdde2e22d..696a6d874 100644
--- a/numpy/linalg/lapack_litemodule.c
+++ b/numpy/linalg/lapack_litemodule.c
@@ -331,10 +331,10 @@ static struct PyModuleDef moduledef = {
/* Initialization function for the module */
#if PY_MAJOR_VERSION >= 3
-#define RETVAL m
+#define RETVAL(x) x
PyMODINIT_FUNC PyInit_lapack_lite(void)
#else
-#define RETVAL
+#define RETVAL(x)
PyMODINIT_FUNC
initlapack_lite(void)
#endif
@@ -347,12 +347,12 @@ initlapack_lite(void)
"", (PyObject*)NULL,PYTHON_API_VERSION);
#endif
if (m == NULL) {
- return RETVAL;
+ return RETVAL(NULL);
}
import_array();
d = PyModule_GetDict(m);
LapackError = PyErr_NewException("lapack_lite.LapackError", NULL, NULL);
PyDict_SetItemString(d, "LapackError", LapackError);
- return RETVAL;
+ return RETVAL(m);
}
diff --git a/numpy/linalg/umath_linalg.c.src b/numpy/linalg/umath_linalg.c.src
index 0248518ac..5fd4dcc29 100644
--- a/numpy/linalg/umath_linalg.c.src
+++ b/numpy/linalg/umath_linalg.c.src
@@ -3251,10 +3251,10 @@ static struct PyModuleDef moduledef = {
#endif
#if defined(NPY_PY3K)
-#define RETVAL m
+#define RETVAL(x) x
PyObject *PyInit__umath_linalg(void)
#else
-#define RETVAL
+#define RETVAL(x)
PyMODINIT_FUNC
init_umath_linalg(void)
#endif
@@ -3270,7 +3270,7 @@ init_umath_linalg(void)
m = Py_InitModule(UMATH_LINALG_MODULE_NAME, UMath_LinAlgMethods);
#endif
if (m == NULL) {
- return RETVAL;
+ return RETVAL(NULL);
}
import_array();
@@ -3288,7 +3288,8 @@ init_umath_linalg(void)
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_RuntimeError,
"cannot load _umath_linalg module.");
+ return RETVAL(NULL);
}
- return RETVAL;
+ return RETVAL(m);
}