see https://stackoverflow.com/questions/31949760
Example on how to print a traceback and ignore low-level calls.
see https://stackoverflow.com/questions/31949760
Example on how to print a traceback and ignore low-level calls.
| # used for tracebacks | |
| __mycode = True | |
| from mycode import main | |
| main() |
| # used for tracebacks | |
| __mycode = True | |
| import sys | |
| def handle_exception(exc_type, exc_info, tb): | |
| import traceback | |
| length = mycode_traceback_levels(tb) | |
| print ''.join(traceback.format_exception(exc_type, exc_info, tb, length)) | |
| def is_mycode(tb): | |
| # returns True if the top frame is part of my code. | |
| globals = tb.tb_frame.f_globals | |
| return globals.has_key('__mycode') | |
| def mycode_traceback_levels(tb): | |
| # counts how many frames are part of my code. | |
| length = 0 | |
| while tb and is_mycode(tb): | |
| length += 1 | |
| tb = tb.tb_next | |
| return length | |
| # handle all unhandled exceptions | |
| sys.excepthook = handle_exception | |
| from thirdparty import work | |
| def main(): | |
| work() | |
| def subwork(): | |
| 1 / 0 | |
| def work(): | |
| subwork() |