New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
problem with pickling newstyle class instances #39916
Comments
My python version: Python 2.3.3 (#1, Jan 25 2004, 21:45:01) Please try the example. That will explain a lot. The class subitem:
def __init__(self, parent):
self.parent = parent
if parent != None:
parent.append(self)
class group(subitem, list):
def __init__(self, parent):
subitem.__init__(self, parent)
g = group(None)
s = subitem(g)
import cPickle
print cPickle.dumps(g)
import pickle
print pickle.dumps(g) |
Logged In: YES Verified in 2.5cvs. |
Please assign this bug to me. Note that neither cPickle or pickle is able to load the stream generated >>> g = group(None)
>>> subitem(g)
>>> g[0].parent is g
True
>>> gp = cPickle.loads(cPickle.dumps(g))
>>> gp[0].parent is gp
False I don't think that will be easy to fix, but I will try to see what I can do. |
Confirmed on trunk. |
This is still a problem with 2.6 and 2.7. I don't know how to test this with py3k as cPickle doesn't exist, can someone advise, thanks. |
I fixed this while working on PEP-3154 [http://hg.python.org/features/pep-3154-alexandre/rev/eed9142d664f]. The relevant piece is @@ -420,7 +424,13 @@ class _Pickler:
write(REDUCE)
if obj is not None:
- self.memoize(obj)
+ # If the object is already in the memo, this means it is
+ # recursive. In this case, throw away everything we put on the
+ # stack, and fetch the object back from the memo.
+ if id(obj) in self.memo:
+ write(POP + self.get(self.memo[id(obj)][0]))
+ else:
+ self.memoize(obj)
# More new special cases (that work with older protocols as
# well): when __reduce__ returns a tuple with 4 or 5 items, It would be pretty easy to backport this to 2.7 and 3.3. It is also good to mention that that only protocol 0 and 1 are affected. |
Here is a patch that backports recursive objects handling to 2.7. |
Could you please make a review Alexandre? |
Updated patch addresses Alexandre's comments. |
Here is revised patch that uses different approach to tests. cPickleFastPicklerTests overridden old recursive tests to check that they raises an exception. The patch extends this to new recursive tests. |
New changeset 9ad1fd251ddf by Serhiy Storchaka in branch '2.7': New changeset 2071d16ed5e6 by Serhiy Storchaka in branch '3.4': New changeset f33ce913220b by Serhiy Storchaka in branch '3.5': New changeset 2c81a883d8ca by Serhiy Storchaka in branch 'default': |
Thank you for your review Alexandre. In 3.x old tests test_recursive_set and test_recursive_frozenset now are implemented in test_recursive_set_and_inst and test_recursive_frozenset_and_inst. Instead new test_recursive_set now tests protocol 4 ability of pickling recursive sets itself. |
New changeset 77184a429dae by Serhiy Storchaka in branch '2.7': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: