I’ve got a C python extension, and I would like to print out some diagnostics.
I’m receiving a string as a PyObject*.
What’s the canonical way to obtain a string representation of this object, such that it usable as a const char *?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
PyObject_Repr(to mimic Python’sreprfunction) orPyObject_Str(to mimicstr), and then callPyString_AsStringto getchar *(you can, and usually should, use it asconst char*, for example:This method is OK for any
PyObject. If you are absolutely sureyourObjectis a Python string and not something else, like for instance a number, you can skip the first line and just do: