My C++ code has this check in it:
if (1 != PyString_Check( key ))
and I'd like to get a "char*" of the type that it actually is in order to provide a more useful error message. Using the C API for Python, how would I do this?
PyTypeObject* type = key->ob_type;
const char* p = type->tp_name;
std::cout << "My type is " << p << std::endl;
As of Python 2.6, there is a macro, Py_TYPE
, which should be used to access the type object of a PythonObject
. You can then fetch the tp_name
field which contains the fully-qualified name of the type. This means that the type of an object obj
can be fetched as a C-string with:
const char* p = Py_TYPE(obj)->tp_name;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With