Skip to content

Instantly share code, notes, and snippets.

@notsobad
Forked from rctay/sitecustomize.py
Last active August 29, 2015 14:08
Show Gist options
  • Save notsobad/bc391672435c15421a7e to your computer and use it in GitHub Desktop.
Save notsobad/bc391672435c15421a7e to your computer and use it in GitHub Desktop.
auto run pdb when there is a exception throw.
'''
original: http://code.activestate.com/recipes/65287/
Put this in python module dir, on mac: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
Run your script this way:
>> python -m autopdb /path/script.py
'''
import bdb
import sys
def info(type, value, tb):
if hasattr(sys, 'ps1') \
or not sys.stdin.isatty() \
or not sys.stdout.isatty() \
or not sys.stderr.isatty() \
or issubclass(type, bdb.BdbQuit) \
or issubclass(type, SyntaxError):
# we are in interactive mode or we don't have a tty-like
# device, so we call the default hook
sys.__excepthook__(type, value, tb)
else:
import traceback, pdb
# we are NOT in interactive mode, print the exception...
traceback.print_exception(type, value, tb)
print
# ...then start the debugger in post-mortem mode.
pdb.pm()
sys.excepthook = info
execfile(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment