Detailed traceback

Aldo Cortesi aldo at
Tue Sep 13 05:09:53 CEST 2005

Thus spake Echo (oshecho at

> I have been working on handling unhanded exceptions and making a
> detailed print out of the traceback after the exception. I found that
> traceback.extract_tb worked nice and was quite simple.
> During my searching around I found out that it might be possible to
> get the variables and their respective values for each frame. I found
> this: to
> be extremely helpful.
> My only problem with that is that there appears to be no way to get
> the line of code for a frame.
> So I was wondering if it was possible to get the line of code for a frame.

Here's how to do what you describe:

import sys
    raise ValueError, "Foo"
    tb = sys.exc_info()[2]
    print tb.tb_frame.f_lineno
    print tb.tb_frame.f_code.co_filename

This will give you the file name and line number. If you want the actual text
of the line, you can simply read the information from the file. 



Aldo Cortesi
aldo at
Off: (02) 9283 1131
Mob: 0419 492 863

More information about the Python-list mailing list