Richard Oudkerk added the comment:
Pickling an exception (assuming it works) does not capture the traceback.
Doing so would be difficult/impossible since the traceback refers to a linked
list of frames, and each frame has references to lots of other stuff like the
code object, the global dict, local dict, builtin dict, ... I certainly do not
know how to make traceback objects pickle compatible.
But you could wrap any exception raised by your function in another exception
whose representation contains a formatted traceback of the original exception.
E.g.
class WrapException(Exception):
def __init__(self):
exc_type, exc_value, exc_tb = sys.exc_info()
self.exception = exc_value
self.formatted = ''.join(traceback.format_exception(exc_type,
exc_value, exc_tb))
def __str__(self):
return '%s\nOriginal traceback:\n%s' % (Exception.__str__(self),
self.formatted)
def go():
try:
1/0
except Exception:
raise WrapException()
Then raising an unpickled WrapException instance gives the original traceback
>>> try: go()
... except Exception as e: exc = e
...
>>> raise pickle.loads(pickle.dumps(exc))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.WrapException:
Original traceback:
Traceback (most recent call last):
File "<stdin>", line 3, in go
ZeroDivisionError: integer division or modulo by zero
----------
nosy: +sbt
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue13831>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com