[Python-3000] PEP 344: attaching tracebacks to exceptions + pickle (original) (raw)

Collin Winter collinw at gmail.com
Fri Apr 13 05:16:26 CEST 2007


In 2.x, exceptions are pickle-able; tracebacks, however, are not. If tracebacks are attached to exceptions in Python 3 (as specified in PEP 344), exceptions will no longer be pickle-able.

There are of course options:

  1. Discard an exception's traceback attribute when pickling.

  2. Include traceback in the pickle, but in a more limited form. That is: the hard parts of exceptions (frames, code objects) aren't fully needed to, say, print out a traceback. The following is all you need:

class Code(object): def init(self, code): self.co_filename = code.co_filename self.co_name = code.co_name

class Frame(object): def init(self, frame): self.f_globals = {"file": frame.f_globals["file"]} self.f_code = Code(frame.f_code)

class Traceback(object): def init(self, tb): self.tb_frame = Frame(tb.tb_frame) self.tb_lineno = tb.tb_lineno if tb.tb_next is None: self.tb_next = None else: self.tb_next = Traceback(tb.tb_next)

When pickling an exception, traceback could be replaced with the limited Traceback implementation above.

  1. Reject the proposal to attach tracebacks to exceptions. (This is my least favorite.)

Thoughts?

Collin Winter



More information about the Python-3000 mailing list