Skip to content

Instantly share code, notes, and snippets.

@dnozay
Created October 7, 2015 18:12
Show Gist options
  • Save dnozay/b599a96dc2d8c69b84c6 to your computer and use it in GitHub Desktop.
Save dnozay/b599a96dc2d8c69b84c6 to your computer and use it in GitHub Desktop.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment