[Python-3000] PEP 344: attaching tracebacks to exceptions + pickle
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.
3. Reject the proposal to attach tracebacks to exceptions. (This is my
least favorite.)
Thoughts?
Collin Winter
More information about the Python-3000
mailing list