Given a PyObject* in C++ how to determine if it is an instance of PyLongDoubleScalarObject?
PyLongDoubleScalarObject is the corresponding numpy class to store 128bit floats (long double), which is defined in numpy/arrayscalars.h.
Using dynamic_cast doesn’t work as PyObject* non-polymorphic. When I look how to do something corresponding to dynamic_cast for PyObject then I mostly find a hint to use functions such as PyLong_Check, but I don’t find something like PyLongDouble_Check.
The C API equivalent to Python’s
isinstancefunction isPyObject_TypeCheck, so you could do something like:Alternatively, if you want to check the exact type of the object and not accept subtypes, you can check the type directly:
Note that you need to pass the object representing the type at the Python level rather than the C struct defining the layout of instances. Looking at the numpy headers, it looks like the type object you’re after is probably
&PyLongDoubleArrType_Type.