Is it possible to create a custom traceback in Python? I'm trying to write a function raise_from()
that imitates Python 3's raise ... from ...
.
def raise_from(exc, cause):
""" Raises the Exception *exc* from the calling stack-frame,
settings its ``__cause__`` to *cause*. """
exc.__cause__ = cause
try: raise Exception
except Exception:
tb = sys.exc_info()[2]
# Remove the last traceback entry.
prelast_tb = tb
while prelast_tb.tb_next:
prelast_tb = prelast_tb.tb_next
prelast_tb.tb_next = None
raise type(exc), exc, tb
Unfortunately, the attributes of the traceback
instance are read-only.