You will have to print your own stacktrace. You can just skip the first two
frames (or however many you want to skip).
except (KeyboardInterrupt, SystemExit):
sys.exit() # You probably don't need a stacktrace here
sys.exc_traceback.tb_next.tb_next) # Skip two frames
You can just pick apart the stackframe and print the interesting
information. You can even reverse the printing order of the stackframes (I
find it more intuitive to print the innermost stackframe first). I included
a version of stacktrace that I sometimes use.
| I know that by setting sys.tracebacklimit I can keep the
| number of tracebacks printed to a constant limit, but what I want is
| to just make sure a traceback print rolls through all but the LAST TWO
| levels? This means that the number of tracebacks printed will vary
Seems like a perfect extension to sys.tracebacklimit. If you make it
negative, it will print (total - abs(sys.tracebacklimit)) stackframes.
------- Sample implementation of stacktrace
# Prints innermost first, and only line number. If consecutive frames are
# from the same file, it print the linenumbers on the same line after the
# file or module name. It doesn't print the offending line.
import sys, os
def stacktrace(type, value, traceback):
print type + ': ' + str(value)
print 'Stacktrace (innermost first):'
discard = pick_apart(traceback)
if tb: last_file = pick_apart(tb.tb_next)
file = tb.tb_frame.f_code.co_filename
line = tb.tb_frame.f_lineno
if file != last_file:
if file[-3:] == '.py':
module = os.path.split(file)[:-3]
sys.stdout.write('\n Module ' + `module`)
sys.stdout.write('\n File ' + `file`)
sys.stdout.write(': line ' + `line`)
sys.stdout.write(', line ' + `line`)