Splice decorator out of exception stack trace?

B

Ben Weaver

Hi,

Is there any way to "splice" a decorator out of an exception stack
trace? For example:
.... def internal(*args, **kwargs):
.... return proc(*args, **kwargs)
.... return internal
.... def foo():
.... assert False, 'simulate failure'
Traceback (most recent call last):
...
File "<...>", line 1, in <module>
File "<...>", line 3, in internal
File "<...>", line 3, in foo
AssertionError: simulate failure

I would like to know if it's possible to somehow end up with this
stack trace instead:

File "<...>", line 1, in <module>
File "<...>", line 3, in foo

Thanks in advance,

-Ben
 
C

Chris Rebert

Hi,

Is there any way to "splice" a decorator out of an exception stack
trace?  For example:

...     def internal(*args, **kwargs):
...         return proc(*args, **kwargs)
...     return internal

... def foo():
...     assert False, 'simulate failure'

Traceback (most recent call last):
 ...
 File "<...>", line 1, in <module>
 File "<...>", line 3, in internal
 File "<...>", line 3, in foo
AssertionError: simulate failure

I would like to know if it's possible to somehow end up with this
stack trace instead:

 File "<...>", line 1, in <module>
 File "<...>", line 3, in foo

You can probably do it by fiddling with the `traceback` module
(http://docs.python.org/library/traceback.html), although without any
context regarding your situation, it seems like it's more trouble than
it's worth to bother doing so.

Cheers,
Chris
 
P

Peter Otten

Ben said:
Hi,

Is there any way to "splice" a decorator out of an exception stack
trace? For example:

... def internal(*args, **kwargs):
... return proc(*args, **kwargs)
... return internal

... def foo():
... assert False, 'simulate failure'

Traceback (most recent call last):
...
File "<...>", line 1, in <module>
File "<...>", line 3, in internal
File "<...>", line 3, in foo
AssertionError: simulate failure

I would like to know if it's possible to somehow end up with this
stack trace instead:

File "<...>", line 1, in <module>
File "<...>", line 3, in foo

It is possible, but not a good idea. You are misleading developers hunting
for bugs.

import sys

def crash():
1/0

def deco(f):
def g(*args, **kw):
try:
print "entering f%s" % (args,)
f(*args, **kw)
except:
v, t, tb = sys.exc_info()
raise v, None, tb.tb_next
return g

@deco
def f(n):
if n:
f(n-1)
else:
crash()


f(5)

Ouput:

$ python doctored_traceback.py
entering f(5,)
entering f(4,)
entering f(3,)
entering f(2,)
entering f(1,)
entering f(0,)
Traceback (most recent call last):
File "doctored_traceback.py", line 24, in <module>
f(5)
File "doctored_traceback.py", line 19, in f
f(n-1)
File "doctored_traceback.py", line 19, in f
f(n-1)
File "doctored_traceback.py", line 19, in f
f(n-1)
File "doctored_traceback.py", line 19, in f
f(n-1)
File "doctored_traceback.py", line 19, in f
f(n-1)
File "doctored_traceback.py", line 21, in f
crash()
File "doctored_traceback.py", line 4, in crash
1/0
ZeroDivisionError

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top