[Numpy-svn] r8262 - trunk/numpy/core/code_generators

numpy-svn@scip... numpy-svn@scip...
Wed Feb 24 23:20:26 CST 2010


Author: charris
Date: 2010-02-24 23:20:26 -0600 (Wed, 24 Feb 2010)
New Revision: 8262

Modified:
   trunk/numpy/core/code_generators/generate_numpy_api.py
   trunk/numpy/core/code_generators/generate_ufunc_api.py
Log:
ENH: Cleanup numpy and ufunc apis for Python >= 3.1. Make sure that an error is
set for all error returns so that PyErr_Print() works properly in the
import_umath* and import_array* macros.

Modified: trunk/numpy/core/code_generators/generate_numpy_api.py
===================================================================
--- trunk/numpy/core/code_generators/generate_numpy_api.py	2010-02-25 05:20:18 UTC (rev 8261)
+++ trunk/numpy/core/code_generators/generate_numpy_api.py	2010-02-25 05:20:26 UTC (rev 8262)
@@ -49,40 +49,57 @@
 _import_array(void)
 {
   int st;
-  PyObject *numpy = NULL;
+  PyObject *numpy = PyImport_ImportModule("numpy.core.multiarray");
   PyObject *c_api = NULL;
 
+  if (numpy == NULL) {
+      PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
+      return -1;
+  }
+  c_api = PyObject_GetAttrString(numpy, "_ARRAY_API");
+  if (c_api == NULL) {
+      PyErr_SetString(PyExc_AttributeError, "_ARRAY_API not found");
+      Py_DECREF(numpy);
+      return -1;
+  }
+  Py_DECREF(numpy);
 
-
-numpy = PyImport_ImportModule("numpy.core.multiarray");
-if (numpy == NULL) return -1;
-c_api = PyObject_GetAttrString(numpy, "_ARRAY_API");
-if (c_api == NULL) {Py_DECREF(numpy); return -1;}
-
 #if PY_VERSION_HEX >= 0x03010000
-  if (PyCapsule_CheckExact(c_api)) {
-      PyArray_API = (void **)PyCapsule_GetPointer(c_api, NULL);
+  if (!PyCapsule_CheckExact(c_api)) {
+      PyErr_SetString(PyExc_RuntimeError, "_ARRAY_API is not PyCapsule object");
+      Py_DECREF(c_api);
+      return -1;
   }
+  PyArray_API = (void **)PyCapsule_GetPointer(c_api, NULL);
+  Py_DECREF(c_api);
+  if (PyArray_API == NULL) {
+      return -1;
+  }
 #else
-  if (PyCObject_Check(c_api)) {
-      PyArray_API = (void **)PyCObject_AsVoidPtr(c_api);
+  if (!PyCObject_Check(c_api)) {
+      PyErr_SetString(PyExc_RuntimeError, "_ARRAY_API is not PyCObject object");
+      Py_DECREF(c_api);
+      return -1;
   }
+  PyArray_API = (void **)PyCObject_AsVoidPtr(c_api);
+  Py_DECREF(c_api);
+  if (PyArray_API == NULL) {
+      PyErr_SetString(PyExc_RuntimeError, "_ARRAY_API is NULL pointer");
+      return -1;
+  }
 #endif
-  Py_DECREF(c_api);
-  Py_DECREF(numpy);
-  if (PyArray_API == NULL) return -1;
   /* Perform runtime check of C API version */
   if (NPY_VERSION != PyArray_GetNDArrayCVersion()) {
-    PyErr_Format(PyExc_RuntimeError, "module compiled against "\
-        "ABI version %%x but this version of numpy is %%x", \
-        (int) NPY_VERSION, (int) PyArray_GetNDArrayCVersion());
-    return -1;
+      PyErr_Format(PyExc_RuntimeError, "module compiled against "\
+             "ABI version %%x but this version of numpy is %%x", \
+             (int) NPY_VERSION, (int) PyArray_GetNDArrayCVersion());
+      return -1;
   }
   if (NPY_FEATURE_VERSION > PyArray_GetNDArrayCFeatureVersion()) {
-    PyErr_Format(PyExc_RuntimeError, "module compiled against "\
-        "API version %%x but this version of numpy is %%x", \
-        (int) NPY_FEATURE_VERSION, (int) PyArray_GetNDArrayCFeatureVersion());
-    return -1;
+      PyErr_Format(PyExc_RuntimeError, "module compiled against "\
+             "API version %%x but this version of numpy is %%x", \
+             (int) NPY_FEATURE_VERSION, (int) PyArray_GetNDArrayCFeatureVersion());
+      return -1;
   }
 
   /*
@@ -91,20 +108,20 @@
    */
   st = PyArray_GetEndianness();
   if (st == NPY_CPU_UNKNOWN_ENDIAN) {
-    PyErr_Format(PyExc_RuntimeError, "FATAL: module compiled as unknown endian");
-    return -1;
+      PyErr_Format(PyExc_RuntimeError, "FATAL: module compiled as unknown endian");
+      return -1;
   }
-#if NPY_BYTE_ORDER ==NPY_BIG_ENDIAN
+#if NPY_BYTE_ORDER == NPY_BIG_ENDIAN
   if (st != NPY_CPU_BIG) {
-    PyErr_Format(PyExc_RuntimeError, "FATAL: module compiled as "\
-        "big endian, but detected different endianness at runtime");
-    return -1;
+      PyErr_Format(PyExc_RuntimeError, "FATAL: module compiled as "\
+             "big endian, but detected different endianness at runtime");
+      return -1;
   }
 #elif NPY_BYTE_ORDER == NPY_LITTLE_ENDIAN
   if (st != NPY_CPU_LITTLE) {
-    PyErr_Format(PyExc_RuntimeError, "FATAL: module compiled as "\
-        "little endian, but detected different endianness at runtime");
-    return -1;
+      PyErr_Format(PyExc_RuntimeError, "FATAL: module compiled as "\
+             "little endian, but detected different endianness at runtime");
+      return -1;
   }
 #endif
 

Modified: trunk/numpy/core/code_generators/generate_ufunc_api.py
===================================================================
--- trunk/numpy/core/code_generators/generate_ufunc_api.py	2010-02-25 05:20:18 UTC (rev 8261)
+++ trunk/numpy/core/code_generators/generate_ufunc_api.py	2010-02-25 05:20:26 UTC (rev 8262)
@@ -41,22 +41,43 @@
   PyObject *numpy = PyImport_ImportModule("numpy.core.umath");
   PyObject *c_api = NULL;
 
-  if (numpy == NULL) return -1;
+  if (numpy == NULL) {
+      PyErr_SetString(PyExc_ImportError, "numpy.core.umath failed to import");
+      return -1;
+  }
   c_api = PyObject_GetAttrString(numpy, "_UFUNC_API");
-  if (c_api == NULL) {Py_DECREF(numpy); return -1;}
+  if (c_api == NULL) {
+      PyErr_SetString(PyExc_AttributeError, "_UFUNC_API not found");
+      Py_DECREF(numpy);
+      return -1;
+  }
+  Py_DECREF(numpy);
 
 #if PY_VERSION_HEX >= 0x03010000
-  if (PyCapsule_CheckExact(c_api)) {
-      PyUFunc_API = (void **)PyCapsule_GetPointer(c_api, NULL);
+  if (!PyCapsule_CheckExact(c_api)) {
+      PyErr_SetString(PyExc_RuntimeError, "_UFUNC_API is not PyCapsule object");
+      Py_DECREF(c_api);
+      return -1;
   }
+  PyUFunc_API = (void **)PyCapsule_GetPointer(c_api, NULL);
+  Py_DECREF(c_api);
+  if (PyUFunc_API == NULL) {
+      return -1;
+  }
 #else
-  if (PyCObject_Check(c_api)) {
-      PyUFunc_API = (void **)PyCObject_AsVoidPtr(c_api);
+  if (!PyCObject_Check(c_api)) {
+      PyErr_SetString(PyExc_RuntimeError, "_UFUNC_API is not PyCObject object");
+      Py_DECREF(c_api);
+      return -1;
   }
-#endif
+  PyUFunc_API = (void **)PyCObject_AsVoidPtr(c_api);
   Py_DECREF(c_api);
   Py_DECREF(numpy);
-  if (PyUFunc_API == NULL) return -1;
+  if (PyUFunc_API == NULL) {
+      PyErr_SetString(PyExc_RuntimeError, "_UFUNC_API is NULL pointer");
+      return -1;
+  }
+#endif
   return 0;
 }
 



More information about the Numpy-svn mailing list