Skip to content

Instantly share code, notes, and snippets.

Created July 24, 2012 09:38
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rctay/3169104 to your computer and use it in GitHub Desktop.
Save rctay/3169104 to your computer and use it in GitHub Desktop.
[python] start debugger on exception
# original:
# place in lib/python2.x/
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)
import traceback, pdb
# we are NOT in interactive mode, print the exception...
traceback.print_exception(type, value, tb)
# ...then start the debugger in post-mortem mode.
sys.excepthook = info
Copy link

ourway commented Jan 4, 2014

great. I am looking a way to implement this as a python argument. for example:
python -pm
Any idea?

Copy link

I test for an environment variable, which means I can just use:

AUTOPDB=true python

Not quite an argument, but easy to do on a once-off basis (or, indeed, set for a shell session).

Copy link

Or, you could put it into a module called '' in your path, and do it as:

python -mpm

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