You can capture a variable f_localsin a frame in a trace :
import sys
import functools
def capturelocals(func):
@functools.wraps(func)
def wrapperfunc(*args, **kw):
try:
return func(*args, **kw)
except Exception:
_, _, tb = sys.exc_info()
try:
while tb.tb_next is not None:
tb = tb.tb_next
locals = tb.tb_frame.f_locals
print locals
finally:
del tb
raise
return wrapperfunc
, :
>>> @capturelocals
... def foobar():
... foo = 'bar'
... spam = 'eggs'
... raise ValueError('Bam!')
...
>>> foobar()
{'foo': 'bar', 'spam': 'eggs'}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in wrapperfunc
File "<stdin>", line 5, in foobar
ValueError: Bam!