[Numpy-svn] r8258 - trunk/numpy/core/src/private

numpy-svn@scip... numpy-svn@scip...
Tue Feb 23 12:54:10 CST 2010


Author: charris
Date: 2010-02-23 12:54:09 -0600 (Tue, 23 Feb 2010)
New Revision: 8258

Modified:
   trunk/numpy/core/src/private/npy_3kcompat.h
Log:
ENH: Add compatibility functions in npy_3kcompat.h for the PyCObject ->
PyCapsule update. The improved error handling of PyCapsules is tossed out. When
Python3k becomes the required version and a major refactoring is undertaken,
that defect should be fixed.

Modified: trunk/numpy/core/src/private/npy_3kcompat.h
===================================================================
--- trunk/numpy/core/src/private/npy_3kcompat.h	2010-02-23 10:01:49 UTC (rev 8257)
+++ trunk/numpy/core/src/private/npy_3kcompat.h	2010-02-23 18:54:09 UTC (rev 8258)
@@ -213,15 +213,89 @@
 #endif
 
 /*
- * A destructor is needed for PyCapsule objects to
- * replace _pya_free.
+ * PyCObject functions adapted to PyCapsules.
+ *
+ * The main job here is to get rid of the improved error handling
+ * of PyCapsules. It's a shame...
  */
 #if defined(NPY_PY3K)
+
+static NPY_INLINE PyObject *
+NpyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *))
+{
+    PyObject *ret = PyCapsule_New(ptr, NULL, dtor);
+    if (ret == NULL) {
+        PyErr_Clear();
+    }
+    return ret;
+}
+
+static NPY_INLINE PyObject *
+NpyCapsule_FromVoidPtrAndDesc(void *ptr, void* context, void (*dtor)(PyObject *))
+{
+    PyObject *ret = NpyCapsule_New(ptr, dtor);
+    if (ret != NULL && PyCapsule_SetContext(ret, context) != 0) {
+        PyErr_Clear();
+        Py_DECREF(ret);
+        ret = NULL;
+    }
+    return ret;
+}
+
+static NPY_INLINE void *
+NpyCapsule_AsVoidPtr(PyObject *obj)
+{
+    void *ret = PyCapsule_GetPointer(obj, NULL);
+    if (ret == NULL) {
+        PyErr_Clear();
+    }
+    return ret;
+}
+
+static NPY_INLINE int
+NpyCapsule_Check(PyObject *ptr)
+{
+    return PyCapsule_CheckExact(ptr);
+}
+
 static void
 simple_capsule_dtor(PyObject *cap)
 {
     PyArray_free(PyCapsule_GetPointer(cap, NULL));
 }
+
+#else
+
+static NPY_INLINE PyObject *
+NpyCapsule_FromVoidPtr(void *ptr, void (*dtor)(void *))
+{
+    return PyCObject_FromVoidPtr(ptr, dtor);
+}
+
+static NPY_INLINE PyObject *
+NpyCapsule_FromVoidPtrAndDesc(void *ptr, void* context, void (*dtor)(void *))
+{
+    return PyCObject_FromVoidPtrAndDesc(ptr, context, dtor);
+}
+
+static NPY_INLINE void *
+NpyCapsule_AsVoidPtr(PyObject *ptr)
+{
+    return PyCObject_AsVoidPtr(ptr);
+}
+
+static NPY_INLINE int
+NpyCapsule_Check(PyObject *ptr)
+{
+    return PyCObject_Check(ptr);
+}
+
+static void
+simple_capsule_dtor(void *ptr)
+{
+    PyArray_free(ptr);
+}
+
 #endif
 
 #endif /* _NPY_3KCOMPAT_H_ */



More information about the Numpy-svn mailing list