Oiltevz

Python stacktrace

Sep 24th, 2020
907
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def full_stack():
  2.     import traceback, sys
  3.     exc = sys.exc_info()[0]
  4.     stack = traceback.extract_stack()[:-1]  # last one would be full_stack()
  5.     if exc is not None:  # i.e. an exception is present
  6.         del stack[-1]       # remove call of full_stack, the printed exception
  7.                             # will contain the caught exception caller instead
  8.     trc = 'Traceback (most recent call last):\n'
  9.     stackstr = trc + ''.join(traceback.format_list(stack))
  10.     if exc is not None:
  11.          stackstr += '  ' + traceback.format_exc().lstrip(trc)
  12.     return stackstr
RAW Paste Data