Adding extra frames to traceback in C module

John Machin sjmachin at lexicon.net
Sat Jun 10 04:28:33 EDT 2006


On 10/06/2006 1:24 PM, Roger Binns wrote:
> One thing I would like to do in my extension module is
> add extra frames to the traceback when an extension
> occurs.  At the moment C code is invisible to tracebacks.
> This is relevant when the C code makes a Python callback.

[snip]

> I couldn't find anything on the web or in the documentation
> to explain how to do it.  I did find snippets of code
> doing things like PyTraceback_Here but they use a real
> Python frame which I don't have and don't know how to
> synthesize.

In the C code generated by Pyrex, the frame is faked up on the fly if an 
error occurs:

C:\junk>demotrbackmain.py
Traceback (most recent call last):
   File "C:\junk\demotrbackmain.py", line 12, in ?
     funcb()
   File "C:\junk\demotrbackmain.py", line 3, in funcb
     demotrback.funcc(funce)
   File "demotrback.pyx", line 2, in demotrback.funcc
     funcd1(cbfunc)
   File "demotrback.pyx", line 5, in demotrback.funcd1
     funcd2(callb)
   File "demotrback.pyx", line 8, in demotrback.funcd2
     callb()
   File "C:\junk\demotrbackmain.py", line 6, in funce
     funcf()
   File "C:\junk\demotrbackmain.py", line 9, in funcf
     x = 1 / 0
ZeroDivisionError: integer division or modulo by zero

Check out 
http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/version/Doc/overview.html#ExceptionValues
then whip up some quick examples and look at the generated code.

HTH,
John



More information about the Python-list mailing list