This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: __reduce__ does not work as documented
Type: Stage:
Components: Documentation Versions: Python 2.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: fdrake, gvanrossum, nascheme, rhettinger
Priority: normal Keywords:

Created on 2002-03-21 20:47 by nascheme, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
copy_reduce.diff nascheme, 2002-03-22 15:10
pickle.diff rhettinger, 2002-05-16 03:14 Patch deprecating None form of __reduce__
Messages (9)
msg9891 - (view) Author: Neil Schemenauer (nascheme) * (Python committer) Date: 2002-03-21 20:47
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())

msg9892 - (view) Author: Neil Schemenauer (nascheme) * (Python committer) Date: 2002-03-22 15:10
Logged In: YES 
user_id=35752

Added trivial patch to fix copy.py module.
msg9893 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-03-22 16:59
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. :-)
msg9894 - (view) Author: Neil Schemenauer (nascheme) * (Python committer) Date: 2002-03-22 17:09
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.
msg9895 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-03-22 17:36
Logged In: YES 
user_id=6380

So be it.
msg9896 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-05-16 03:14
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.
msg9897 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2002-05-21 15:42
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.
msg9898 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-05-21 17:23
Logged In: YES 
user_id=80475

Committed libpickle.tex 1.36 and pickle.py 1.62.
Closing patch.
msg9899 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-05-21 18:18
Logged In: YES 
user_id=6380

Wouldn't it be better if the warning came on the pickling
side instead of on the unpickling side?
History
Date User Action Args
2022-04-10 16:05:08adminsetgithub: 36308
2002-03-21 20:47:05naschemecreate