Re: ???: telling tracebacks to stop

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Wed, 23 Mar 1994 13:53:44 -0500

You can modify this function - it mimics the internal traceback printout.
It needs to be changed to write to sys.stderr, and the return of
last_traceback is only there for poking around.
(Formatting of the output, especially whitespace, not guaranteed identical.)

I assume there is something similar in one of the debugger modules.

I *THINK* you want to skip the first 2, not the last two, is that
correct ? ( or was it the other way around? )

- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics

def trace():
from sys import last_traceback,last_type,last_value
from linecache import getline
tb = last_traceback
print 'Traceback (innermost last):'
while tb:
fname = tb.tb_frame.f_code.co_filename
line = getline( fname, tb.tb_lineno )
print ' File "'+fname+'", line', tb.tb_lineno
if line[:-1] : print ' ',line[:-1]
tb = tb.tb_next
print last_type+':', last_value
return last_traceback

#
# some test functions:
# try err(), then errt() and compare.
#

def f(a,b):
print f, (a,b)
return g(a,b)

def g(a,b):
print g, (a,b)
return f(a)

def err():
print err
return f(1,2)

def errt():
print errt
try:
return f(1,2)
except:
return trace()