[Python-bugs-list] [ python-Bugs-533291 ] __reduce__ does not work as documented

noreply@sourceforge.net noreply@sourceforge.net
Tue, 21 May 2002 10:23:57 -0700


Bugs item #533291, was opened at 2002-03-21 15:47
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=533291&group_id=5470

Category: Documentation
Group: Python 2.3
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Neil Schemenauer (nascheme)
Assigned to: Raymond Hettinger (rhettinger)
Summary: __reduce__ does not work as documented

Initial Comment:
The documentation for __reduce__ says that the second
element of the returned tuple can either be a tuple
of arguments or None.  The copy module does not handle
the None case:

  class C(object):
    def __reduce__(self):
        return (C, None)

  import copy
  copy.copy(C())



----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2002-05-21 12:23

Message:
Logged In: YES 
user_id=80475

Committed libpickle.tex 1.36 and pickle.py 1.62.
Closing patch.

----------------------------------------------------------------------

Comment By: Fred L. Drake, Jr. (fdrake)
Date: 2002-05-21 10:42

Message:
Logged In: YES 
user_id=3066

Issues with the doc portion the patch:

- "/deprecated" should be spelled "\deprecated"

- Deprecations are whole paragraphs; don't include them
  inside parenthetical expressions.  Make them separate
  paragraphs which preceed or succeed the relevant
  material in the documentation.

Once appropriate corrections are made, I'm fine with the patch.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-05-15 22:14

Message:
Logged In: YES 
user_id=80475

See attached documentation and code patch for deprecating 
the None version.  If approved, re-assign to Raymond for 
commit.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-03-22 12:36

Message:
Logged In: YES 
user_id=6380

So be it.

----------------------------------------------------------------------

Comment By: Neil Schemenauer (nascheme)
Date: 2002-03-22 12:09

Message:
Logged In: YES 
user_id=35752

I would prefer that the None version be deprecated.  The
documentation for __reduce__ is confusing enough as it is.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-03-22 11:59

Message:
Logged In: YES 
user_id=6380

I'm not sure it's that simple. While pickling a C instance
indeed succeed, unpickling it raises a strange exception:

>>> pickle.loads(pickle.dumps(C()))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/home/guido/trunk/Lib/pickle.py", line 982, in loads
    return Unpickler(file).load()
  File "/home/guido/trunk/Lib/pickle.py", line 593, in load
    dispatch[key](self)
  File "/home/guido/trunk/Lib/pickle.py", line 842, in
load_reduce
    value = callable.__basicnew__()
AttributeError: type object 'C' has no attribute
'__basicnew__'
>>>

It appears that an argument tuple of None signals some
special Jim-Fulton-only behavior. __basicnew__ is a feature
of ExtensionClasses (similar to __new__ in Python 2.2), and
while ExtensionClasses work in 2.2, they're being
deprecated: Zope 2.x will continue to use Python 2.1.x, and
Zope 3 will require Python 2.2 or higher.

The copy module has never worked for ExtensionClass
instances (unless maybe the class defines a __copy__
method).

Maybe the right thing to do is to document 'None' as a
special case that one shouldn't use, and deprecate
__basicnew__?

(Hm, OTOH why don't I just approve your fix so we can stop
thinking about this. :-)

----------------------------------------------------------------------

Comment By: Neil Schemenauer (nascheme)
Date: 2002-03-22 10:10

Message:
Logged In: YES 
user_id=35752

Added trivial patch to fix copy.py module.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=533291&group_id=5470