Created
July 16, 2016 10:20
-
-
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!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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