Skip to content

Instantly share code, notes, and snippets.

@dcragusa
Created September 10, 2018 10:38
Show Gist options
  • Save dcragusa/9c00c5e55d116961c5aa8bd51865fcd9 to your computer and use it in GitHub Desktop.
Save dcragusa/9c00c5e55d116961c5aa8bd51865fcd9 to your computer and use it in GitHub Desktop.
Convenience module that when imported automatically drops you into pdb when an exception is hit
# From https://stackoverflow.com/a/242531/2864129
# Put in pythonpath and 'import debug'
import sys
def info(type_, value, tb):
if hasattr(sys, 'ps1') or not sys.stderr.isatty() or type_ is KeyboardInterrupt:
# we are in interactive mode / we don't have a tty-like device / user-triggered, 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)
pdb.post_mortem(tb)
sys.excepthook = info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment