Suppose I have the following:
def foo():
bar()
def bar():
baz()
def baz():
raise ValueError("hello")
foo()
unsurprisingly, the trace is
$ python x.py
Traceback (most recent call last):
File "x.py", line 10, in <module>
foo()
File "x.py", line 2, in foo
bar()
File "x.py", line 5, in bar
baz()
File "x.py", line 8, in baz
raise ValueError("hello")
ValueError: hello
Now suppose I want the traceback to look like this
Traceback (most recent call last):
File "x.py", line 10, in <module>
foo()
ValueError: hello
That is, I want to be able to remove the topmost two entries in the stack trace, and I want to do it within baz()
, that is, at the raising of the exception, either by mangling the stack, or by crafting the raised exception in a special way.
Is it possible?
Edit: one option would be to raise inside baz and catch immediately, then re-raise with a trimmed traceback, but I don't know how to do it.
Edit 2:
what I need is this, in pseudocode
def baz():
raise ValueError, "hello", <traceback built from current frame stack, except the last two>