[Python-bugs-list] [ python-Bugs-533291 ] __reduce__ does not work as documented
noreply@sourceforge.net
noreply@sourceforge.net
Fri, 22 Mar 2002 08:59:09 -0800
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: Python Library
Group: Python 2.2.1 candidate
Status: Open
Resolution: None
Priority: 5
Submitted By: Neil Schemenauer (nascheme)
Assigned to: Guido van Rossum (gvanrossum)
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: 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