Nathan Osman
Nathan Osman

Reputation: 73245

How to get Python error as C string?

I have a C++ application that embeds the Python interpreter. It calls PyImport_Import to load scripts. I need a way of getting any syntax errors as C strings. For example, if the script uses a undefined function, I would like an error saying something like 'Function xxx is undefined.' How would I do this?

Upvotes: 0

Views: 292

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 882123

PyErr_Occurred lets your C code check whether an exception has been raised, and, if so, what type; then, PyErr_Fetch lets you fetch all details (as Python objects), and you can get the string representation of the error instance with the usual high-level call PyObject_Str (just the same as except Exception, e: ...str(e)... in Python code).

Upvotes: 1

Related Questions