Prepending to traceback

Ian Bicking ianb at colorstudy.com
Wed Feb 9 12:01:16 EST 2005


Stefan Behnel wrote:
> I want them to look like this:
> 
> Traceback (most recent call last):
>  ...
>   File "/usr/lib/python2.4/site-packages/pyparsing.py", line 456, in parse
>     loc,tokens = self.parseImpl( instring, loc, doActions )
>   File "/usr/lib/python2.4/site-packages/pyparsing.py", line 727, in 
> parseImpl
>     raise exc
>   PyParsing, line 5, in SomeStatement
>     PARSER TEST FOR TESTING MISSING TERM
> ParseException: Expected "SOMEOTHERTERM" (146), (5,9)

This query's a little old now, but I found this through the Weekly URL.

Anyway, Zope's ExceptionFormatter has one answer to this; you put a 
local variable like:

     __traceback_info__ = ('PyParsing, line %i, in %s\n  %s'
         % (line, statement, message))

And then that will show up in the traceback, much like you want.  The 
formatter looks through all the frames for this local variable and 
prints it out when found.  It's used in Zope for showing line numbers 
and other information when its interpreting scripts like Zope Page 
Templates or Python Scripts -- so that instead of just seeing the 
traceback from the interpreter, you also see information about what the 
interpreter is doing.  This sounds similar to what you want.

It's pretty simple to use and it doesn't depend on the rest of Zope: 
http://cvs.zope.org/Products/ErrorReporter/ExceptionFormatter.py?rev=HEAD&content-type=text/vnd.viewcvs-markup


-- 
Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org



More information about the Python-list mailing list