From d96e881ccc7178628b213f6e2c958099bbcbf2ca Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Tue, 23 Feb 2010 18:54:09 +0000 Subject: 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. --- numpy/core/src/private/npy_3kcompat.h | 78 ++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/private/npy_3kcompat.h b/numpy/core/src/private/npy_3kcompat.h index adf8b7312..9cbc52e7b 100644 --- a/numpy/core/src/private/npy_3kcompat.h +++ b/numpy/core/src/private/npy_3kcompat.h @@ -213,15 +213,89 @@ PyObject_Cmp(PyObject *i1, PyObject *i2, int *cmp) #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_ */ -- cgit v1.2.1