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?
+1
A:
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).
Alex Martelli
2010-01-02 05:10:50
Thanks. Just what I was looking for.
George Edison
2010-01-02 06:46:38