Skip to content

Instantly share code, notes, and snippets.

@juancarlospaco
Last active August 15, 2021 14:13
Show Gist options
  • Save juancarlospaco/539b2fba6c5152c317c8 to your computer and use it in GitHub Desktop.
Save juancarlospaco/539b2fba6c5152c317c8 to your computer and use it in GitHub Desktop.
ipdb on Exception AutoMagically!, Launch pdb or ipdb when an exception happens automatically.
def pdb_on_exception(debugger="pdb", limit=100):
"""Install handler attach post-mortem pdb console on an exception."""
pass
def pdb_excepthook(exc_type, exc_val, exc_tb):
traceback.print_tb(exc_tb, limit=limit)
__import__(str(debugger).strip().lower()).post_mortem(exc_tb)
sys.excepthook = pdb_excepthook
ipdb_on_exception = lambda: pdb_on_exception("ipdb")
# ptpdb_on_exception = lambda: pdb_on_exception("ptpdb") # See https://github.com/jonathanslenders/ptpdb/issues/14
@juancarlospaco
Copy link
Author

> /home/juan/code/test.py (54) <module>

---> 54 ipdb_on_exception()
     55 0 / 0  # division by zero

ipdb> 

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