Embedding -- getting error/traceback information not to stderr? (Summary)

Erwin S. Andreasen erwin at andreasen.com
Sun Jan 30 11:44:26 EST 2000


Jon K Hellan wrote:

> erw at dde.dk (Erwin S. Andreasen) writes:
> 
> > In my application using embedded Python, I'd like to print out the error
> > information as it occurrs (traceback, etc.). This is what PyErr_PrintEx()
> > (called from PyErr_Print()) does -- unfortunately, it does it to sys.stderr.

Thanks to everyone for the responses. I ended up replacing sys.stderr with
a cStringIO object for now. At the beginning of the application, sys.stderr
is replaced and a reference to the object is saved in a C variable. When
the C code evaluates some user-entered code and PyRun_String returns
NULL, PyErr_Print() is called, the value within the stream is fetched with
its getvalue method and then reset() and truncate() are called to
make ready for the next error.

I tried running a Python function using the exception module after an
error occurred, but it seems that no further code can be compiled 
after a syntax error exception, until that exception is cleared. 
I guess I could have gotten around this by precompiling the function 
to do traceback.

I may have to end up doing something more complicated anyway, to provide
proper display of the source lines -- the pathnames of the files
as seen by the user are not the same as they are as stored on disk, and
AFAIK, Python does not store the source together with the compiled code
but rather fetches it from disk again when printing out the error.
Plus I plan on doing some preprocessing of the Python code to allow
for some special extensions.

I found after posting that this has been discussed on the newsgroup
in the past 3 years (the topic seems to reappear every 3-4 months 
or so :) -- you will find several discussions on retrieving traceback 
information from C by searching for PyErr_Fetch on Deja.

-- 
=======================================================================
<erwin at andreasen.com>      Herlev, Denmark       UNIX System Programmer
<URL:http://www.andreasen.org/>      <*>              Goodbye, Richard!
=======================================================================



More information about the Python-list mailing list