Author dcjim
Recipients
Date 2007-06-25.14:43:03
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
Exceptions with required initialization arguments can't be unpickled:

>>> class E(Exception):
...     def __init__(self, x):
...         self.x = x
... 
>>> import pickle
>>> e = E(1)
>>> p = pickle.dumps(e, 1)
>>> pickle.loads(p)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python/2.5.1/lib/python2.5/pickle.py", line 1374, in loads
    return Unpickler(file).load()
  File "/usr/local/python/2.5.1/lib/python2.5/pickle.py", line 858, in load
    dispatch[key](self)
  File "/usr/local/python/2.5.1/lib/python2.5/pickle.py", line 1133, in load_reduce
    value = func(*args)
TypeError: __init__() takes exactly 2 arguments (1 given)

This is because __reduce__ defined in exceptions.c returns the type and the args variable, which an exception subclass might not populate.

Also, the reduce implementation doesn't properly serialize the message attribute.

I assume that the need for a custom reduce is due to the micro-optimization to store arge and message in C slots.  Is this really necessary?
History
Date User Action Args
2007-08-23 14:57:57adminlinkissue1742889 messages
2007-08-23 14:57:57admincreate