1 #ifndef NPY_CTYPES_H
2 #define NPY_CTYPES_H
3 
4 #include <Python.h>
5 
6 #include "npy_import.h"
7 
8 /*
9  * Check if a python type is a ctypes class.
10  *
11  * Works like the Py<type>_Check functions, returning true if the argument
12  * looks like a ctypes object.
13  *
14  * This entire function is just a wrapper around the Python function of the
15  * same name.
16  */
17 NPY_INLINE static int
npy_ctypes_check(PyTypeObject * obj)18 npy_ctypes_check(PyTypeObject *obj)
19 {
20     static PyObject *py_func = NULL;
21     PyObject *ret_obj;
22     int ret;
23 
24     npy_cache_import("numpy.core._internal", "npy_ctypes_check", &py_func);
25     if (py_func == NULL) {
26         goto fail;
27     }
28 
29     ret_obj = PyObject_CallFunctionObjArgs(py_func, (PyObject *)obj, NULL);
30     if (ret_obj == NULL) {
31         goto fail;
32     }
33 
34     ret = PyObject_IsTrue(ret_obj);
35     Py_DECREF(ret_obj);
36     if (ret == -1) {
37         goto fail;
38     }
39 
40     return ret;
41 
42 fail:
43     /* If the above fails, then we should just assume that the type is not from
44      * ctypes
45      */
46     PyErr_Clear();
47     return 0;
48 }
49 
50 #endif
51