[Python-Dev] Pickle implementation questions

Bruce Christensen t-bruch at microsoft.com
Fri Jun 30 21:20:45 CEST 2006


Tim Peters wrote:

> I hope you've read PEP 307:

I have. Thanks to you and Guido for writing it! It's been a huge help.

> The implementation is more like:
[snip]

Thanks! That helps a lot. PEP 307 and the pickle module docs describe the end
result pretty well, but they don't always make it clear where things are
implemented. I'm trying to make sure that I'm getting the right interaction
between object.__reduce(_ex)__, pickle, and copy_reg..

One (hopefully) last question: is object.__reduce(_ex)__ really implemented in
object? The tracebacks below would indicate that pickle directly implements the
behavior that the specs say is implemented in object. However, that could be
because frames from C code don't show up in tracebacks. I'm not familiar enough
with CPython to know for sure.

>>> import copy_reg
>>> def bomb(*args, **kwargs):
...     raise Exception('KABOOM! %r %r' % (args, kwargs))
...
>>> copy_reg._reduce_ex = bomb
>>> import pickle
>>> pickle.dumps(object())
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "C:\Python24\lib\pickle.py", line 1386, in dumps
    Pickler(file, protocol, bin).dump(obj)
  File "C:\Python24\lib\pickle.py", line 231, in dump
    self.save(obj)
  File "C:\Python24\lib\pickle.py", line 313, in save
    rv = reduce(self.proto)
  File "<stdin>", line 2, in bomb
Exception: KABOOM! (<object object at 0x01E3C448>, 0) {}

>>> class NewObj(object):
...     def __reduce__(self):
...             raise Exception("reducing NewObj")
...
>>> import pickle
>>> pickle.dumps(NewObj())
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "C:\Python24\lib\pickle.py", line 1386, in dumps
    Pickler(file, protocol, bin).dump(obj)
  File "C:\Python24\lib\pickle.py", line 231, in dump
    self.save(obj)
  File "C:\Python24\lib\pickle.py", line 313, in save
    rv = reduce(self.proto)
  File "<stdin>", line 3, in __reduce__
Exception: reducing NewObj

> It's documented in the Library Reference Manual, in the `copy_reg` docs:

Oops. :)

Again, thanks for the help.

--Bruce


More information about the Python-Dev mailing list