Skip to content

Instantly share code, notes, and snippets.

@niwinz
Created October 2, 2012 12:30
Show Gist options
  • Save niwinz/3818659 to your computer and use it in GitHub Desktop.
Save niwinz/3818659 to your computer and use it in GitHub Desktop.
What is the simplest way of using Python pdb to inspect the cause of an unhandled exception?
import functools
import pdb
def debug_on(*exceptions):
if not exceptions:
exceptions = (AssertionError, )
def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
try:
return f(*args, **kwargs)
except exceptions:
pdb.post_mortem(sys.exc_info()[2])
return wrapper
return decorator
# example
@debug_on(TypeError)
def buggy_function()
....
raise TypeError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment