Last active
December 9, 2017 04:23
-
-
Save auxiliary/13641cac8e1d5c8a2812b54e5dd34745 to your computer and use it in GitHub Desktop.
Catch exceptions anywhere and open the bpython interpreter
This file contains hidden or 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
import os | |
import sys | |
import traceback | |
import code | |
import bpython | |
def print_banner(): | |
message = "Select a frame to view using the select(<frame #>) command" | |
print '\n\x1b[0;34;40m' + message + '\x1b[0m' | |
message = "Reload the script using the reload() command" | |
print '\x1b[0;34;40m' + message + '\x1b[0m' | |
def print_with_color(text): | |
print '\x1b[0;34;40m' + text + '\x1b[0m', | |
def catch_with_bpython(_type, _value, _traceback): | |
#traceback.print_exception(_type, _value, _traceback) | |
listing = traceback.format_exception(_type, _value, _traceback) | |
counter = 0 | |
for line in listing: | |
if line.strip().startswith("File "): | |
print_with_color(str(counter) + ") ") | |
counter += 1 | |
print line | |
print_banner() | |
all_locals = [_traceback.tb_frame.f_locals] | |
while _traceback.tb_next != None: | |
_traceback = _traceback.tb_next | |
all_locals.append(_traceback.tb_frame.f_locals) | |
def select(frame_no): | |
bpython.embed(all_locals[frame_no]) | |
def reload(): | |
os.execv(sys.executable, ['python'] + sys.argv) | |
code.interact(local=locals(), banner='') | |
if __name__ == '__main__': | |
sys.excepthook = catch_with_bpython | |
execfile(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use it in a similar way to pdb:
python -m debug <your script name>