Author sbt
Recipients fmitha, sbt, slinkp
Date 2013-04-18.13:43:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1366292631.58.0.653712223772.issue13831@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2013-04-18 13:43:51sbtsetrecipients: + sbt, fmitha, slinkp
2013-04-18 13:43:51sbtsetmessageid: <1366292631.58.0.653712223772.issue13831@psf.upfronthosting.co.za>
2013-04-18 13:43:51sbtlinkissue13831 messages
2013-04-18 13:43:51sbtcreate