Skip to content

Instantly share code, notes, and snippets.

@whosaysni
Last active May 29, 2016 23:05
Show Gist options
  • Save whosaysni/83faa92f184cac4f63fbb8bb9d7a67d3 to your computer and use it in GitHub Desktop.
Save whosaysni/83faa92f184cac4f63fbb8bb9d7a67d3 to your computer and use it in GitHub Desktop.
# coding: utf-8
import sys
from inspect import getinnerframes
from functools import wraps
from pprint import pprint
def inspect_on_failure(msg_fmt):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except:
ty, vl, tb = sys.exc_info()
frames = (getinnerframes(tb))
p, q, r, s, t, u = frames[-1]
params = dict(p.f_globals)
params.update(p.f_locals)
if msg_fmt:
print(msg_fmt % params)
raise
return wrapper
return decorator
@inspect_on_failure('Failure: %(bar)s and %(baz)s')
def foo(bar, baz):
qux = 1, 2, 5
quux = 'throw it now'
raise ValueError('Holy!')
try:
foo('cave', 'rabbit')
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment