Author sbt
Recipients fmitha, sbt, slinkp
Date 2013-04-18.13:43:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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():
    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>
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:51sbtsetrecipients: + sbt, fmitha, slinkp
2013-04-18 13:43:51sbtsetmessageid: <>
2013-04-18 13:43:51sbtlinkissue13831 messages
2013-04-18 13:43:51sbtcreate