【发布时间】:2014-07-29 07:22:33
【问题描述】:
是否可以在 Python 中创建自定义回溯?我正在尝试编写一个模仿 Python 3 的 raise ... from ... 的函数 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
很遗憾,traceback 实例的属性是只读的。
【问题讨论】:
标签: python exception python-2.x traceback