Message187247
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 |
|
Date |
User |
Action |
Args |
2013-04-18 13:43:51 | sbt | set | recipients:
+ sbt, fmitha, slinkp |
2013-04-18 13:43:51 | sbt | set | messageid: <1366292631.58.0.653712223772.issue13831@psf.upfronthosting.co.za> |
2013-04-18 13:43:51 | sbt | link | issue13831 messages |
2013-04-18 13:43:51 | sbt | create | |
|