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/common | |
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/common')
-rw-r--r-- | numpy/core/src/common/npy_ctypes.h | 49 |
1 files changed, 49 insertions, 0 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 |