diff options
| author | Eric Wieser <wieser.eric@gmail.com> | 2018-10-24 00:27:22 -0700 |
|---|---|---|
| committer | Eric Wieser <wieser.eric@gmail.com> | 2018-10-25 21:22:19 -0700 |
| commit | ea3a3d4b9d7330ecae40ebf2c7497e6dd25d07bb (patch) | |
| tree | 4c826d58848eaa3807ad16da768c609f68537ed6 /numpy/core/src | |
| parent | 8aa121415760cc6839a546c3f84e238d1dfa1aa6 (diff) | |
| download | numpy-ea3a3d4b9d7330ecae40ebf2c7497e6dd25d07bb.tar.gz | |
MAINT: Extract `_is_from_ctypes` to a header so that it can be shared
Diffstat (limited to 'numpy/core/src')
| -rw-r--r-- | numpy/core/src/common/npy_ctypes.h | 49 | ||||
| -rw-r--r-- | numpy/core/src/multiarray/ctors.c | 12 |
2 files changed, 51 insertions, 10 deletions
diff --git a/numpy/core/src/common/npy_ctypes.h b/numpy/core/src/common/npy_ctypes.h new file mode 100644 index 000000000..f26db9e05 --- /dev/null +++ b/numpy/core/src/common/npy_ctypes.h @@ -0,0 +1,49 @@ +#ifndef NPY_CTYPES_H +#define NPY_CTYPES_H + +#include <Python.h> + +#include "npy_import.h" + +/* + * Check if a python type is a ctypes class. + * + * Works like the Py<type>_Check functions, returning true if the argument + * looks like a ctypes object. + * + * This entire function is just a wrapper around the Python function of the + * same name. + */ +NPY_INLINE static int +npy_ctypes_check(PyTypeObject *obj) +{ + static PyObject *py_func = NULL; + PyObject *ret_obj; + int ret; + + npy_cache_import("numpy.core._internal", "npy_ctypes_check", &py_func); + if (py_func == NULL) { + goto fail; + } + + ret_obj = PyObject_CallFunctionObjArgs(py_func, (PyObject *)obj, NULL); + if (ret_obj == NULL) { + goto fail; + } + + ret = PyObject_IsTrue(ret_obj); + if (ret == -1) { + goto fail; + } + + return ret; + +fail: + /* If the above fails, then we should just assume that the type is not from + * ctypes + */ + PyErr_Clear(); + return 0; +} + +#endif diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index aaaaeee82..bf888659d 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -11,7 +11,7 @@ #include "npy_config.h" -#include "npy_import.h" +#include "npy_ctypes.h" #include "npy_pycompat.h" #include "multiarraymodule.h" @@ -1381,15 +1381,7 @@ _array_from_buffer_3118(PyObject *memoryview) * Note that even if the above are fixed in master, we have to drop the * early patch versions of python to actually make use of the fixes. */ - - int is_ctypes = _is_from_ctypes(view->obj); - if (is_ctypes < 0) { - /* This error is not useful */ - PyErr_WriteUnraisable(view->obj); - is_ctypes = 0; - } - - if (!is_ctypes) { + if (!npy_ctypes_check(Py_TYPE(view->obj))) { /* This object has no excuse for a broken PEP3118 buffer */ PyErr_Format( PyExc_RuntimeError, |
