Skip to content

Instantly share code, notes, and snippets.

@sivabudh
Created July 16, 2016 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivabudh/3500ddb9caba3bdc0e3b2ce3eaf57aa4 to your computer and use it in GitHub Desktop.
Save sivabudh/3500ddb9caba3bdc0e3b2ce3eaf57aa4 to your computer and use it in GitHub Desktop.
Function to print out the stacktrace when an exception occurs. Use it in your except block!
"""
Python 3 only, brotha'
Credit: http://blog.dscpl.com.au/2015/03/generating-full-stack-traces-for.html
"""
def full_stack():
tb = sys.exc_info()[2]
msg = 'Traceback (most recent call last):\n'
for item in reversed(inspect.getouterframes(tb.tb_frame)[1:]):
msg += ' File "{1}", line {2}, in {3}\n'.format(*item)
for line in item[4]:
msg += ' ' + line.lstrip()
for item in inspect.getinnerframes(tb):
msg += ' File "{1}", line {2}, in {3}\n'.format(*item)
for line in item[4]:
msg += ' ' + line.lstrip()
return msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment