Detailed traceback

Aldo Cortesi aldo at nullcube.com
Mon Sep 12 23:09:53 EDT 2005


Thus spake Echo (oshecho at gmail.com):

> 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: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52215 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
try:
    raise ValueError, "Foo"
except:
    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. 


Cheers,



Aldo


-- 
Aldo Cortesi
aldo at nullcube.com
http://www.nullcube.com
Off: (02) 9283 1131
Mob: 0419 492 863



More information about the Python-list mailing list