Skip to content

Instantly share code, notes, and snippets.

@auxiliary
Last active December 9, 2017 04:23
Show Gist options
  • Save auxiliary/13641cac8e1d5c8a2812b54e5dd34745 to your computer and use it in GitHub Desktop.
Save auxiliary/13641cac8e1d5c8a2812b54e5dd34745 to your computer and use it in GitHub Desktop.
Catch exceptions anywhere and open the bpython interpreter
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])
@auxiliary
Copy link
Author

auxiliary commented Dec 4, 2017

Use it in a similar way to pdb: python -m debug <your script name>

@auxiliary
Copy link
Author

auxiliary commented Dec 9, 2017

Moving to https://github.com/auxiliary/dbug.py for more development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment