[Python-Dev] Pickle implementation questions

"Martin v. Löwis" martin at v.loewis.de
Fri Jun 30 22:12:57 CEST 2006


Bruce Christensen wrote:
> 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..

You really should ignore the existance of copy_reg._reduce_ex. It's an
implementation detail - it could have been implemented just as well in
C directly, in which case you couldn't as easily replace it with a
different function. It might be implemented that way in the next
release, or the entire __reduce_ex__ implementation might be lifted
to Python some day.

> One (hopefully) last question: is object.__reduce(_ex)__ really implemented in
> object?

Sure.

> 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) {}

You don't get a stack frame for C functions (normally, anyway):
there is no file/line number information available.

The reduce thing you are seeing really comes from

   # Check for a __reduce_ex__ method, fall back to __reduce__
   reduce = getattr(obj, "__reduce_ex__", None)

Regards,
Martin


More information about the Python-Dev mailing list