diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2010-02-23 18:54:09 +0000 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2010-02-23 18:54:09 +0000 |
commit | d96e881ccc7178628b213f6e2c958099bbcbf2ca (patch) | |
tree | 4847afa3f35d70c0d6f435c38aa16b84b774e282 | |
parent | c9dec27b5abb1ed11d5b2c58c5671060ffb8cf46 (diff) | |
download | numpy-d96e881ccc7178628b213f6e2c958099bbcbf2ca.tar.gz |
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.
-rw-r--r-- | numpy/core/src/private/npy_3kcompat.h | 78 |
1 files changed, 76 insertions, 2 deletions
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_ */ |