Function
PyObject* PyArray_TypeObjectFromType(int);
converts the type number for the scalar type NumPy (NPY_BOOL, NPY_BYTE, ...) into the corresponding type object.
How do you do the inverse conversion, from a type object for a scalar NumPy type to an appropriate type number?
Edit: The following code is based on Quatford's answer. It accepts both types of objects, such as int and numpy.int16, and strings, such as "int", "int", and "int16".
int numpyScalarTypeNumber(PyObject* obj) { PyArray_Descr* dtype; if(!PyArray_DescrConverter(obj, &dtype)) return NPY_NOTYPE; int typeNum = dtype->type_num; Py_DECREF(dtype); return typeNum; }
source share