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: Pickle fails to restore new-style class instance in a cycle
Type: Stage:
Components: Interpreter Core Versions: Python 2.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, pje, rhettinger, staschuk
Priority: normal Keywords:

Created on 2003-06-06 21:13 by pje, last changed 2022-04-10 16:09 by admin. This issue is now closed.

Messages (6)
msg16280 - (view) Author: PJ Eby (pje) * (Python committer) Date: 2003-06-06 21:13
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   
msg16281 - (view) Author: Steven Taschuk (staschuk) Date: 2003-06-08 20:23
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.
msg16282 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-07-01 03:00
Logged In: YES 
user_id=80475

Another datapoint:  This above script runs fine in Py2.3b2.
msg16283 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-05-31 11:34
Logged In: YES 
user_id=1188172

Runs fine in Py2.4.1 too, so should it be considered fixed?
msg16284 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-08-31 22:04
Logged In: YES 
user_id=1188172

Runs find in 2.5cvs too.
msg16285 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-10-01 13:38
Logged In: YES 
user_id=1188172

I think we can consider this fixed now.
History
Date User Action Args
2022-04-10 16:09:03adminsetgithub: 38602
2003-06-06 21:13:25pjecreate