[Python-bugs-list] [ python-Bugs-750328 ] Pickle fails to restore new-style class instance in a cycle

SourceForge.net noreply@sourceforge.net
Sun, 08 Jun 2003 13:23:41 -0700


Bugs item #750328, was opened at 2003-06-06 15:13
Message generated for change (Comment added) made by staschuk
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=750328&group_id=5470

Category: Type/class unification
Group: Python 2.2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Phillip J. Eby (pje)
Assigned to: Nobody/Anonymous (nobody)
Summary: Pickle fails to restore new-style class instance in a cycle

Initial Comment:
Here is code to demonstrate the problem.  All asserts
succeed except the last, showing that pickle and
cPickle both handle a "classic" cycle correctly, but
only cPickle handles new-style cycles correctly.  It
would appear that the problem is that the pure-Python
pickle isn't putting the object into its 'memo' until
*after* the object's state has been pickled.  Thus, the
identity is not preserved on unpickling.  This may be
true for other object types that use __reduce__, but I
have not verified this.


import pickle, cPickle

class X: pass

x = X()
x.x = x

x2 = cPickle.loads(cPickle.dumps(x))
assert x2.x is x2

x2 = pickle.loads(pickle.dumps(x))
assert x2.x is x2

class Y(object): pass

y = Y()
y.y = y

y2 = cPickle.loads(cPickle.dumps(y))
assert y2.y is y2

# this fails
y2 = pickle.loads(pickle.dumps(y))
assert y2.y is y2   


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

Comment By: Steven Taschuk (staschuk)
Date: 2003-06-08 14:23

Message:
Logged In: YES 
user_id=666873

Compare bug 702858, which observes the same behaviour for 
copy.deepcopy.

The common parts of pickle and copy really ought to be merged.

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

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