Issue 1474680: pickling files works with protocol=2. (original) (raw)
Compare:
import pickle pickle.dumps(file('/etc/passwd')) Traceback (most recent call last): File "", line 1, in ? File "/usr/lib/python2.4/pickle.py", line 1386, in dumps Pickler(file, protocol, bin).dump(obj) File "/usr/lib/python2.4/pickle.py", line 231, in dump self.save(obj) File "/usr/lib/python2.4/pickle.py", line 313, in save rv = reduce(self.proto) File "/usr/lib/python2.4/copy_reg.py", line 69, in _reduce_ex raise TypeError, "can't pickle %s objects" % base.name TypeError: can't pickle file objects
with
pickle.dumps(file('/etc/passwd'), protocol=2) '\x80\x02c__builtin__\nfile\nq\x00)\x81q\x01.'
Similarly, the reduce method works for basic objects like str, int or dict with protocol=2, but doesn't work with protocol=1:
(1).reduce_ex(1) Traceback (most recent call last): File "", line 1, in ? File "/usr/lib/python2.4/copy_reg.py", line 69, in _reduce_ex raise TypeError, "can't pickle %s objects" % base.name TypeError: can't pickle int objects (1).reduce_ex(2) (<function __newobj__ at 0xb7e5117c>, (<type 'int'>, 1), None, None, None)
Antoine, I think the goal here is to disable pickling, not making it work.
Alexandre, is it ok for a class to define pickle magic methods just to raise an exception? Doesn’t it break expectations?
One question hasn’t been answered: When did the protocol change? Was it intentional or is it a bug?