[Numpy-svn] r8376 - in trunk/numpy: core/code_generators core/include/numpy core/src/multiarray core/src/private core/src/umath f2py/src lib numarray numarray/include/numpy random/mtrand

numpy-svn at scipy.org numpy-svn at scipy.org
Mon May 3 03:50:02 EDT 2010


Author: charris
Date: 2010-05-03 02:50:02 -0500 (Mon, 03 May 2010)
New Revision: 8376

Modified:
   trunk/numpy/core/code_generators/generate_numpy_api.py
   trunk/numpy/core/code_generators/generate_ufunc_api.py
   trunk/numpy/core/include/numpy/ndarrayobject.h
   trunk/numpy/core/src/multiarray/arraytypes.c.src
   trunk/numpy/core/src/multiarray/common.c
   trunk/numpy/core/src/multiarray/convert_datatype.c
   trunk/numpy/core/src/multiarray/ctors.c
   trunk/numpy/core/src/multiarray/descriptor.c
   trunk/numpy/core/src/multiarray/getset.c
   trunk/numpy/core/src/multiarray/multiarraymodule.c
   trunk/numpy/core/src/multiarray/scalarapi.c
   trunk/numpy/core/src/multiarray/scalartypes.c.src
   trunk/numpy/core/src/multiarray/scalartypes.h
   trunk/numpy/core/src/multiarray/usertypes.c
   trunk/numpy/core/src/private/npy_3kcompat.h
   trunk/numpy/core/src/umath/ufunc_object.c
   trunk/numpy/core/src/umath/umathmodule.c.src
   trunk/numpy/f2py/src/fortranobject.c
   trunk/numpy/f2py/src/fortranobject.h
   trunk/numpy/lib/type_check.py
   trunk/numpy/numarray/_capi.c
   trunk/numpy/numarray/include/numpy/libnumarray.h
   trunk/numpy/random/mtrand/Python.pxi
Log:
ENH, BUG: PyCObject will be deprecated in python 2.7. So use the NpyCapsule
compatibility functions in npy_3kcompat.h to replace the current calls.
This gets rid of a number of version checks and is easier to maintain.
Fix bug that was present in the ufunc _loop1d_list_free destructor in
the python3k case.

Modified: trunk/numpy/core/code_generators/generate_numpy_api.py
===================================================================
--- trunk/numpy/core/code_generators/generate_numpy_api.py	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/code_generators/generate_numpy_api.py	2010-05-03 07:50:02 UTC (rev 8376)
@@ -64,7 +64,7 @@
   }
   Py_DECREF(numpy);
 
-#if PY_VERSION_HEX >= 0x03010000
+#if PY_VERSION_HEX >= 0x02070000
   if (!PyCapsule_CheckExact(c_api)) {
       PyErr_SetString(PyExc_RuntimeError, "_ARRAY_API is not PyCapsule object");
       Py_DECREF(c_api);

Modified: trunk/numpy/core/code_generators/generate_ufunc_api.py
===================================================================
--- trunk/numpy/core/code_generators/generate_ufunc_api.py	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/code_generators/generate_ufunc_api.py	2010-05-03 07:50:02 UTC (rev 8376)
@@ -53,7 +53,7 @@
   }
   Py_DECREF(numpy);
 
-#if PY_VERSION_HEX >= 0x03010000
+#if PY_VERSION_HEX >= 0x02070000
   if (!PyCapsule_CheckExact(c_api)) {
       PyErr_SetString(PyExc_RuntimeError, "_UFUNC_API is not PyCapsule object");
       Py_DECREF(c_api);

Modified: trunk/numpy/core/include/numpy/ndarrayobject.h
===================================================================
--- trunk/numpy/core/include/numpy/ndarrayobject.h	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/include/numpy/ndarrayobject.h	2010-05-03 07:50:02 UTC (rev 8376)
@@ -598,9 +598,19 @@
         int sec, us, ps, as;
 } npy_timedeltastruct;
 
+#if PY_VERSION_HEX >= 0x02070000
+#define PyDataType_GetDatetimeMetaData(descr)                                 \
+    ((descr->metadata == NULL) ? NULL :                                       \
+        ((PyArray_DatetimeMetaData *)(PyCapsule_GetPointer(                   \
+                PyDict_GetItemString(                                         \
+                    descr->metadata, NPY_METADATA_DTSTR), NULL))))
+#else
+#define PyDataType_GetDatetimeMetaData(descr)                                 \
+    ((descr->metadata == NULL) ? NULL :                                       \
+        ((PyArray_DatetimeMetaData *)(PyCObject_AsVoidPtr(                    \
+                PyDict_GetItemString(descr->metadata, NPY_METADATA_DTSTR)))))
+#endif
 
-#define PyDataType_GetDatetimeMetaData(descr) ((descr->metadata == NULL) ? NULL : ((PyArray_DatetimeMetaData *)(PyCObject_AsVoidPtr(PyDict_GetItemString(descr->metadata, NPY_METADATA_DTSTR)))))
-
 typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *);
 
 /* Means c-style contiguous (last index varies the fastest). The

Modified: trunk/numpy/core/src/multiarray/arraytypes.c.src
===================================================================
--- trunk/numpy/core/src/multiarray/arraytypes.c.src	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/src/multiarray/arraytypes.c.src	2010-05-03 07:50:02 UTC (rev 8376)
@@ -3537,14 +3537,7 @@
  * There is no error check here and no way to indicate an error
  * until the metadata turns up NULL.
  */
-#if defined(NPY_PY3K)
-    cobj = PyCapsule_New((void *)dt_data, NULL, simple_capsule_dtor);
-    if (cobj == NULL) {
-        PyErr_Clear();
-    }
-#else
-    cobj = PyCObject_FromVoidPtr((void *)dt_data, simple_capsule_dtor);
-#endif
+    cobj = NpyCapsule_FromVoidPtr((void *)dt_data, simple_capsule_dtor);
     descr->metadata = PyDict_New();
     PyDict_SetItemString(descr->metadata, NPY_METADATA_DTSTR, cobj);
     Py_DECREF(cobj);

Modified: trunk/numpy/core/src/multiarray/common.c
===================================================================
--- trunk/numpy/core/src/multiarray/common.c	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/src/multiarray/common.c	2010-05-03 07:50:02 UTC (rev 8376)
@@ -258,25 +258,14 @@
         PyArrayInterface *inter;
         char buf[40];
 
-#if defined(NPY_PY3K)
-        if (PyCapsule_CheckExact(ip)) {
-            inter = (PyArrayInterface *)PyCapsule_GetPointer(ip, NULL);
+        if (NpyCapsule_Check(ip)) {
+            inter = (PyArrayInterface *)NpyCapsule_AsVoidPtr(ip);
             if (inter->two == 2) {
                 PyOS_snprintf(buf, sizeof(buf),
                         "|%c%d", inter->typekind, inter->itemsize);
                 chktype = _array_typedescr_fromstr(buf);
             }
         }
-#else
-        if (PyCObject_Check(ip)) {
-            inter = (PyArrayInterface *)PyCObject_AsVoidPtr(ip);
-            if (inter->two == 2) {
-                PyOS_snprintf(buf, sizeof(buf),
-                        "|%c%d", inter->typekind, inter->itemsize);
-                chktype = _array_typedescr_fromstr(buf);
-            }
-        }
-#endif
         Py_DECREF(ip);
         if (chktype) {
             goto finish;

Modified: trunk/numpy/core/src/multiarray/convert_datatype.c
===================================================================
--- trunk/numpy/core/src/multiarray/convert_datatype.c	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/src/multiarray/convert_datatype.c	2010-05-03 07:50:02 UTC (rev 8376)
@@ -102,15 +102,9 @@
             key = PyInt_FromLong(type_num);
             cobj = PyDict_GetItem(obj, key);
             Py_DECREF(key);
-#if defined(NPY_PY3K)
-            if (PyCapsule_CheckExact(cobj)) {
-                castfunc = PyCapsule_GetPointer(cobj, NULL);
-            }
-#else
-            if (PyCObject_Check(cobj)) {
+            if (NpyCapsule_Check(cobj)) {
                 castfunc = PyCObject_AsVoidPtr(cobj);
             }
-#endif
         }
     }
     if (PyTypeNum_ISCOMPLEX(descr->type_num) &&

Modified: trunk/numpy/core/src/multiarray/ctors.c
===================================================================
--- trunk/numpy/core/src/multiarray/ctors.c	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/src/multiarray/ctors.c	2010-05-03 07:50:02 UTC (rev 8376)
@@ -1140,18 +1140,9 @@
 #endif
     if ((e = PyObject_GetAttrString(s, "__array_struct__")) != NULL) {
         d = -1;
-#if defined(NPY_PY3K)
-        if (PyCapsule_CheckExact(e)) {
+        if (NpyCapsule_Check(e)) {
             PyArrayInterface *inter;
-            inter = (PyArrayInterface *)PyCapsule_GetPointer(e, NULL);
-            if (inter == NULL) {
-                PyErr_Clear();
-            }
-#else
-        if (PyCObject_Check(e)) {
-            PyArrayInterface *inter;
-            inter = (PyArrayInterface *)PyCObject_AsVoidPtr(e);
-#endif
+            inter = (PyArrayInterface *)NpyCapsule_AsVoidPtr(e);
             if (inter->two == 2) {
                 d = inter->nd;
             }
@@ -1574,20 +1565,10 @@
                  */
                 PyArray_UpdateFlags(self, UPDATE_ALL);
             }
-#if defined(NPY_PY3K)
-            if PyCapsule_CheckExact(func) {
+            if (NpyCapsule_Check(func)) {
                 /* A C-function is stored here */
                 PyArray_FinalizeFunc *cfunc;
-                cfunc = PyCapsule_GetPointer(func, NULL);
-                if (cfunc == NULL) {
-                    PyErr_Clear();
-                }
-#else
-            if PyCObject_Check(func) {
-                /* A C-function is stored here */
-                PyArray_FinalizeFunc *cfunc;
-                cfunc = PyCObject_AsVoidPtr(func);
-#endif
+                cfunc = NpyCapsule_AsVoidPtr(func);
                 Py_DECREF(func);
                 if (cfunc(self, obj) < 0) {
                     goto fail;
@@ -2129,20 +2110,10 @@
         PyErr_Clear();
         return Py_NotImplemented;
     }
-#if defined(NPY_PY3K)
-    if (!PyCapsule_CheckExact(attr)) {
+    if (!NpyCapsule_Check(attr)) {
         goto fail;
     }
-    inter = PyCapsule_GetPointer(attr, NULL);
-    if (inter == NULL) {
-        PyErr_Clear();
-    }
-#else
-    if (!PyCObject_Check(attr)) {
-        goto fail;
-    }
-    inter = PyCObject_AsVoidPtr(attr);
-#endif
+    inter = NpyCapsule_AsVoidPtr(attr);
     if (inter->two != 2) {
         goto fail;
     }

Modified: trunk/numpy/core/src/multiarray/descriptor.c
===================================================================
--- trunk/numpy/core/src/multiarray/descriptor.c	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/src/multiarray/descriptor.c	2010-05-03 07:50:02 UTC (rev 8376)
@@ -646,14 +646,7 @@
     PyArray_DatetimeMetaData *dt_data;
     PyObject *dt_tuple;
 
-#if defined(NPY_PY3K)
-    dt_data = PyCapsule_GetPointer(cobj, NULL);
-    if (dt_data == NULL) {
-        PyErr_Clear();
-    }
-#else
-    dt_data = PyCObject_AsVoidPtr(cobj);
-#endif
+    dt_data = NpyCapsule_AsVoidPtr(cobj);
     dt_tuple = PyTuple_New(4);
 
     PyTuple_SET_ITEM(dt_tuple, 0,
@@ -692,14 +685,7 @@
 /* FIXME
  * There is no error handling here.
  */
-#if defined(NPY_PY3K)
-    ret = PyCapsule_New((void *)dt_data, NULL, simple_capsule_dtor);
-    if (ret == NULL) {
-        PyErr_Clear();
-    }
-#else
-    ret = PyCObject_FromVoidPtr((void *)dt_data, simple_capsule_dtor);
-#endif
+    ret = NpyCapsule_FromVoidPtr((void *)dt_data, simple_capsule_dtor);
     return ret;
 }
 
@@ -1563,14 +1549,7 @@
         return ret;
     }
     tmp = PyDict_GetItemString(self->metadata, NPY_METADATA_DTSTR);
-#if defined(NPY_PY3K)
-    dt_data = PyCapsule_GetPointer(tmp, NULL);
-    if (dt_data == NULL) {
-        PyErr_Clear();
-    }
-#else
-    dt_data = PyCObject_AsVoidPtr(tmp);
-#endif
+    dt_data = NpyCapsule_AsVoidPtr(tmp);
     num = dt_data->num;
     den = dt_data->den;
     events = dt_data->events;

Modified: trunk/numpy/core/src/multiarray/getset.c
===================================================================
--- trunk/numpy/core/src/multiarray/getset.c	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/src/multiarray/getset.c	2010-05-03 07:50:02 UTC (rev 8376)
@@ -552,19 +552,7 @@
         inter->descr = NULL;
     }
     Py_INCREF(self);
-#if defined(NPY_PY3K)
-    ret = PyCapsule_New(inter, NULL, gentype_struct_free);
-    if (ret == NULL) {
-        PyErr_Clear();
-    }
-    else if (PyCapsule_SetContext(ret, self) != 0) {
-        PyErr_Clear();
-        Py_DECREF(ret);
-        ret = NULL;
-    }
-#else
-    ret = PyCObject_FromVoidPtrAndDesc(inter, self, gentype_struct_free);
-#endif
+    ret = NpyCapsule_FromVoidPtrAndDesc(inter, self, gentype_struct_free);
     return ret;
 }
 

Modified: trunk/numpy/core/src/multiarray/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarray/multiarraymodule.c	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/src/multiarray/multiarraymodule.c	2010-05-03 07:50:02 UTC (rev 8376)
@@ -1353,19 +1353,8 @@
 /* FIXME
  * There is no err handling here.
  */
-#if defined(NPY_PY3K)
-    data1 = PyCapsule_GetPointer(cobj1, NULL);
-    if (data1 == NULL) {
-        PyErr_Clear();
-    }
-    data2 = PyCapsule_GetPointer(cobj2, NULL);
-    if (data2 == NULL) {
-        PyErr_Clear();
-    }
-#else
-    data1 = PyCObject_AsVoidPtr(cobj1);
-    data2 = PyCObject_AsVoidPtr(cobj2);
-#endif
+    data1 = NpyCapsule_AsVoidPtr(cobj1);
+    data2 = NpyCapsule_AsVoidPtr(cobj2);
     return ((data1->base == data2->base)
             && (data1->num == data2->num)
             && (data1->den == data2->den)
@@ -3062,14 +3051,7 @@
 /* FIXME
  * There is no error handling here
  */
-#if defined(NPY_PY3K)
-    c_api = PyCapsule_New((void *)PyArray_API, NULL, NULL);
-    if (c_api == NULL) {
-        PyErr_Clear();
-    }
-#else
-    c_api = PyCObject_FromVoidPtr((void *)PyArray_API, NULL);
-#endif
+    c_api = NpyCapsule_FromVoidPtr((void *)PyArray_API, NULL);
     PyDict_SetItemString(d, "_ARRAY_API", c_api);
     Py_DECREF(c_api);
     if (PyErr_Occurred()) {
@@ -3103,14 +3085,7 @@
 /* FIXME
  * There is no error handling here
  */
-#if defined(NPY_PY3K)
-    s = PyCapsule_New((void *)_datetime_strings, NULL, NULL);
-    if (s == NULL) {
-        PyErr_Clear();
-    }
-#else
-    s = PyCObject_FromVoidPtr((void *)_datetime_strings, NULL);
-#endif
+    s = NpyCapsule_FromVoidPtr((void *)_datetime_strings, NULL);
     PyDict_SetItemString(d, "DATETIMEUNITS", s);
     Py_DECREF(s);
 

Modified: trunk/numpy/core/src/multiarray/scalarapi.c
===================================================================
--- trunk/numpy/core/src/multiarray/scalarapi.c	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/src/multiarray/scalarapi.c	2010-05-03 07:50:02 UTC (rev 8376)
@@ -534,14 +534,7 @@
             memcpy(dt_data, &((PyTimedeltaScalarObject *)sc)->obmeta,
                    sizeof(PyArray_DatetimeMetaData));
         }
-#if defined(NPY_PY3K)
-        cobj = PyCapsule_New((void *)dt_data, NULL, simple_capsule_dtor);
-        if (cobj == NULL) {
-            PyErr_Clear();
-        }
-#else
-        cobj = PyCObject_FromVoidPtr((void *)dt_data, simple_capsule_dtor);
-#endif
+        cobj = NpyCapsule_FromVoidPtr((void *)dt_data, simple_capsule_dtor);
 
         /* Add correct meta-data to the data-type */
         if (descr == NULL) {
@@ -677,14 +670,7 @@
 /* FIXME
  * There is no error handling here.
  */
-#if defined(NPY_PY3K)
-        dt_data = PyCapsule_GetPointer(cobj, NULL);
-        if (dt_data == NULL) {
-            PyErr_Clear();
-        }
-#else
-        dt_data = PyCObject_AsVoidPtr(cobj);
-#endif
+        dt_data = NpyCapsule_AsVoidPtr(cobj);
         memcpy(&(((PyDatetimeScalarObject *)obj)->obmeta), dt_data,
                sizeof(PyArray_DatetimeMetaData));
     }

Modified: trunk/numpy/core/src/multiarray/scalartypes.c.src
===================================================================
--- trunk/numpy/core/src/multiarray/scalartypes.c.src	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/src/multiarray/scalartypes.c.src	2010-05-03 07:50:02 UTC (rev 8376)
@@ -810,7 +810,7 @@
     return PyInt_FromLong(1);
 }
 
-#if defined(NPY_PY3K)
+#if PY_VERSION_HEX >= 0x02070000
 NPY_NO_EXPORT void
 gentype_struct_free(PyObject *ptr)
 {
@@ -857,18 +857,7 @@
     inter->data = arr->data;
     inter->descr = NULL;
 
-#if defined(NPY_PY3K)
-    ret = PyCapsule_New(inter, NULL, gentype_struct_free);
-    if (ret == NULL) {
-        PyErr_Clear();
-    }
-    else if (PyCapsule_SetContext(ret, arr) != 0) {
-        PyErr_Clear();
-        ret == NULL;
-    }
-#else
-    ret = PyCObject_FromVoidPtrAndDesc(inter, arr, gentype_struct_free);
-#endif
+    ret = NpyCapsule_FromVoidPtrAndDesc(inter, arr, gentype_struct_free);
     return ret;
 }
 

Modified: trunk/numpy/core/src/multiarray/scalartypes.h
===================================================================
--- trunk/numpy/core/src/multiarray/scalartypes.h	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/src/multiarray/scalartypes.h	2010-05-03 07:50:02 UTC (rev 8376)
@@ -7,7 +7,7 @@
 NPY_NO_EXPORT void
 format_longdouble(char *buf, size_t buflen, longdouble val, unsigned int prec);
 
-#if defined(NPY_PY3K)
+#if PY_VERSION_HEX >= 0x02070000
 NPY_NO_EXPORT void
 gentype_struct_free(PyObject *ptr);
 #else

Modified: trunk/numpy/core/src/multiarray/usertypes.c
===================================================================
--- trunk/numpy/core/src/multiarray/usertypes.c	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/src/multiarray/usertypes.c	2010-05-03 07:50:02 UTC (rev 8376)
@@ -206,15 +206,8 @@
     if (PyErr_Occurred()) {
         return -1;
     }
-#if defined(NPY_PY3K)
-    cobj = PyCapsule_New((void *)castfunc, NULL, NULL);
+    cobj = NpyCapsule_FromVoidPtr((void *)castfunc, NULL);
     if (cobj == NULL) {
-        PyErr_Clear();
-    }
-#else
-    cobj = PyCObject_FromVoidPtr((void *)castfunc, NULL);
-#endif
-    if (cobj == NULL) {
         Py_DECREF(key);
         return -1;
     }

Modified: trunk/numpy/core/src/private/npy_3kcompat.h
===================================================================
--- trunk/numpy/core/src/private/npy_3kcompat.h	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/src/private/npy_3kcompat.h	2010-05-03 07:50:02 UTC (rev 8376)
@@ -219,7 +219,7 @@
  * The main job here is to get rid of the improved error handling
  * of PyCapsules. It's a shame...
  */
-#if defined(NPY_PY3K)
+#if PY_VERSION_HEX >= 0x02070000
 
 static NPY_INLINE PyObject *
 NpyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *))

Modified: trunk/numpy/core/src/umath/ufunc_object.c
===================================================================
--- trunk/numpy/core/src/umath/ufunc_object.c	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/src/umath/ufunc_object.c	2010-05-03 07:50:02 UTC (rev 8376)
@@ -372,14 +372,7 @@
     PyUFunc_Loop1d *funcdata;
     int i;
 
-#if defined(NPY_PY3K)
-    funcdata = (PyUFunc_Loop1d *)PyCapsule_GetPointer(obj, NULL);
-    if (funcdata == NULL) {
-        PyErr_Clear();
-    }
-#else
-    funcdata = (PyUFunc_Loop1d *)PyCObject_AsVoidPtr(obj);
-#endif
+    funcdata = (PyUFunc_Loop1d *)NpyCapsule_AsVoidPtr(obj);
     while (funcdata != NULL) {
         for (i = 0; i < nin; i++) {
             if (!PyArray_CanCoerceScalar(arg_types[i],
@@ -524,14 +517,7 @@
          * extract the correct function
          * data and argtypes
          */
-#if defined(NPY_PY3K)
-        funcdata = (PyUFunc_Loop1d *)PyCapsule_GetPointer(obj, NULL);
-        if (funcdata == NULL) {
-            PyErr_Clear();
-        }
-#else
-        funcdata = (PyUFunc_Loop1d *)PyCObject_AsVoidPtr(obj);
-#endif
+        funcdata = (PyUFunc_Loop1d *)NpyCapsule_AsVoidPtr(obj);
         while (funcdata != NULL) {
             if (n != 1) {
                 for (i = 0; i < nargs; i++) {
@@ -3890,36 +3876,31 @@
  * This frees the linked-list structure when the CObject
  * is destroyed (removed from the internal dictionary)
 */
-#if defined(NPY_PY3K)
 static void
-_loop1d_list_free(PyObject *ptr)
+_free_loop1d_list(PyUFunc_Loop1d *data)
 {
-    PyUFunc_Loop1d *funcdata;
-
-    funcdata = (PyUFunc_Loop1d *)PyCapsule_GetPointer(ptr, NULL);
-    if (funcdata == NULL) {
+    if (data == NULL) {
         return;
     }
-    _pya_free(funcdata->arg_types);
-    _loop1d_list_free(funcdata->next);
-    _pya_free(funcdata);
+    _pya_free(data->arg_types);
+    _free_loop1d_list(data->next);
+    _pya_free(data);
 }
 
+#if PY_VERSION_HEX >= 0x02070000
+static void
+_loop1d_list_free(PyObject *ptr)
+{
+    PyUFunc_Loop1d *data = (PyUFunc_Loop1d *)PyCapsule_GetPointer(ptr, NULL);
+    _free_loop1d_list(data);
+}
+
 #else
 static void
 _loop1d_list_free(void *ptr)
 {
-    PyUFunc_Loop1d *funcdata;
-    if (ptr == NULL) {
-        return;
-    }
-    funcdata = (PyUFunc_Loop1d *)ptr;
-    if (funcdata == NULL) {
-        return;
-    }
-    _pya_free(funcdata->arg_types);
-    _loop1d_list_free(funcdata->next);
-    _pya_free(funcdata);
+    PyUFunc_Loop1d *data = (PyUFunc_Loop1d *)ptr;
+    _free_loop1d_list(data);
 }
 #endif
 
@@ -3980,15 +3961,8 @@
     cobj = PyDict_GetItem(ufunc->userloops, key);
     /* If it's not there, then make one and return. */
     if (cobj == NULL) {
-#if defined(NPY_PY3K)
-        cobj = PyCapsule_New((void *)funcdata, NULL, _loop1d_list_free);
+        cobj = NpyCapsule_FromVoidPtr((void *)funcdata, _loop1d_list_free);
         if (cobj == NULL) {
-            PyErr_Clear();
-        }
-#else
-        cobj = PyCObject_FromVoidPtr((void *)funcdata, _loop1d_list_free);
-#endif
-        if (cobj == NULL) {
             goto fail;
         }
         PyDict_SetItem(ufunc->userloops, key, cobj);
@@ -4005,14 +3979,7 @@
          * is exactly like this one, then just replace.
          * Otherwise insert.
          */
-#if defined(NPY_PY3K)
-        current = (PyUFunc_Loop1d *)PyCapsule_GetPointer(cobj, NULL);
-        if (current == NULL) {
-            PyErr_Clear();
-        }
-#else
-        current = (PyUFunc_Loop1d *)PyCObject_AsVoidPtr(cobj);
-#endif
+        current = (PyUFunc_Loop1d *)NpyCapsule_AsVoidPtr(cobj);
         while (current != NULL) {
             cmp = cmp_arg_types(current->arg_types, newtypes, ufunc->nargs);
             if (cmp >= 0) {

Modified: trunk/numpy/core/src/umath/umathmodule.c.src
===================================================================
--- trunk/numpy/core/src/umath/umathmodule.c.src	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/core/src/umath/umathmodule.c.src	2010-05-03 07:50:02 UTC (rev 8376)
@@ -304,14 +304,7 @@
     /* Add some symbolic constants to the module */
     d = PyModule_GetDict(m);
 
-#if defined(NPY_PY3K)
-    c_api = PyCapsule_New((void *)PyUFunc_API, NULL, NULL);
-    if (c_api == NULL) {
-        PyErr_Clear();
-    }
-#else
-    c_api = PyCObject_FromVoidPtr((void *)PyUFunc_API, NULL);
-#endif
+    c_api = NpyCapsule_FromVoidPtr((void *)PyUFunc_API, NULL);
     if (PyErr_Occurred()) {
         goto err;
     }

Modified: trunk/numpy/f2py/src/fortranobject.c
===================================================================
--- trunk/numpy/f2py/src/fortranobject.c	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/f2py/src/fortranobject.c	2010-05-03 07:50:02 UTC (rev 8376)
@@ -913,11 +913,11 @@
     return PyArray_CopyInto(out, (PyArrayObject *)arr);
 }
 
-/*******************************************/
-/* Compatibility functions for Python 3.1  */
-/*******************************************/
+/*********************************************/
+/* Compatibility functions for Python >= 2.7 */
+/*********************************************/
 
-#if PY_VERSION_HEX >= 0X03010000
+#if PY_VERSION_HEX >= 0X02070000
 
 PyObject *
 F2PyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *))

Modified: trunk/numpy/f2py/src/fortranobject.h
===================================================================
--- trunk/numpy/f2py/src/fortranobject.h	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/f2py/src/fortranobject.h	2010-05-03 07:50:02 UTC (rev 8376)
@@ -76,7 +76,7 @@
 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
 
 PyFortranObject represents various Fortran objects:
-Fortran (module) routines, COMMON blocks, module data. 
+Fortran (module) routines, COMMON blocks, module data.
 
 Author: Pearu Peterson <pearu at cens.ioc.ee>
 */
@@ -121,7 +121,7 @@
   extern PyObject * PyFortranObject_New(FortranDataDef* defs, f2py_void_func init);
   extern PyObject * PyFortranObject_NewAsAttr(FortranDataDef* defs);
 
-#if PY_VERSION_HEX >= 0x03010000
+#if PY_VERSION_HEX >= 0x02070000
 
 PyObject * F2PyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *));
 void * F2PyCapsule_AsVoidPtr(PyObject *obj);

Modified: trunk/numpy/lib/type_check.py
===================================================================
--- trunk/numpy/lib/type_check.py	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/lib/type_check.py	2010-05-03 07:50:02 UTC (rev 8376)
@@ -620,7 +620,7 @@
                     ('events', ctypes.c_int)]
 
     import sys
-    if sys.version_info[:2] >= (3,1):
+    if sys.version_info[:2] >= (2,7):
         func = ctypes.pythonapi.PyCapsule_GetPointer
         func.argtypes = [ctypes.py_object, ctypes.c_char_p]
         func.restype = ctypes.c_void_p

Modified: trunk/numpy/numarray/_capi.c
===================================================================
--- trunk/numpy/numarray/_capi.c	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/numarray/_capi.c	2010-05-03 07:50:02 UTC (rev 8376)
@@ -3402,7 +3402,7 @@
     _Error = PyErr_NewException("numpy.numarray._capi.error", NULL, NULL);
 
     /* Create a CObject containing the API pointer array's address */
-#if PY_VERSION_HEX >= 0x03010000
+#if PY_VERSION_HEX >= 0x02070000
     m = PyModule_Create(&moduledef);
     c_api_object = PyCapsule_New((void *)libnumarray_API, NULL, NULL);
     if (c_api_object == NULL) {

Modified: trunk/numpy/numarray/include/numpy/libnumarray.h
===================================================================
--- trunk/numpy/numarray/include/numpy/libnumarray.h	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/numarray/include/numpy/libnumarray.h	2010-05-03 07:50:02 UTC (rev 8376)
@@ -40,7 +40,7 @@
 #endif
 #endif
 
-#if PY_VERSION_HEX >= 0x03010000
+#if PY_VERSION_HEX >= 0x02070000
 #define _import_libnumarray()                                                    \
         {                                                                        \
         PyObject *module = PyImport_ImportModule("numpy.numarray._capi");        \

Modified: trunk/numpy/random/mtrand/Python.pxi
===================================================================
--- trunk/numpy/random/mtrand/Python.pxi	2010-05-02 20:02:14 UTC (rev 8375)
+++ trunk/numpy/random/mtrand/Python.pxi	2010-05-03 07:50:02 UTC (rev 8376)
@@ -29,6 +29,9 @@
     void Py_XINCREF(object obj)
 
     # CObject API
+# If this is uncommented it needs to be fixed to use PyCapsule
+# for Python >= 2.7
+#
 #    ctypedef void (*destructor1)(void* cobj)
 #    ctypedef void (*destructor2)(void* cobj, void* desc)
 #    int PyCObject_Check(object p)




More information about the Numpy-svn mailing list