PyErr_Occurred - how to use results?

Richard Harvey tririch at connect.net
Fri Sep 1 11:11:10 EDT 2000


I have Python embedded in a C/C++ application, and I'm calling user defined
Python scripts that themselves can call back into my application. Right now
I'm having problems when an error occurs in a script; I haven't found a
clean way to report errors back within my application.  I've been browsing
various code samples that reroute stderr, but I can't see how I can route
these back to a C function for display.  So, I'm taking a look now at
PyErr_Occurred. This function appears to do exactly what I want by returning
the last raised exception. However, now that I have the PyObject* result,
how can I decode this object to get an error number, string, and line
number? I hoped this would be something like a dictionary object where I
could choose which keys I'm interested in and display the error to the user,
but it appears this is the actual exception object itself.

Can anyone point me in the right direction, either by how to decode
PyErr_Occurred from the C API, or setting the global stderr to call back
into my C code with objects/strings that can be evaluated in C for output?
I'm quite impressed by what PythonWin pulled off, by offering source level
debugging on top of the Python engine from a C application.

Rich






More information about the Python-list mailing list