I have a few decorators which wrap many functions. When one of the functions throws an error my traceback is polluted with many decorator lines. Is there a way to modify a python decorator so it doesn't print itself in an exception traceback?
Here is some example code:
import functools
def dec(func):
@functools.wraps(func)
def wrap(*args, **kwargs):
return func(*args, **kwargs)
return wrap
@dec
def spam():
print('I sure hope nobody throws an Exception')
eggs()
@dec
def eggs():
raise Exception('Spanish Inquisition')
if __name__ == '__main__':
spam()
When I run this I get:
Traceback (most recent call last):
File "tmp.py", line 23, in <module>
spam()
File "tmp.py", line 7, in wrap
return func(*args, **kwargs)
File "tmp.py", line 14, in spam
eggs()
File "tmp.py", line 7, in wrap
return func(*args, **kwargs)
File "tmp.py", line 19, in eggs
raise Exception('Spanish Inquisition')
Exception: Spanish Inquisition
But what I really want is this:
Traceback (most recent call last):
File "tmp.py", line 23, in <module>
spam()
File "tmp.py", line 14, in spam
eggs()
File "tmp.py", line 19, in eggs
raise Exception('Spanish Inquisition')
Exception: Spanish Inquisition
How might I go about this?