Skip to content
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

Bug in Pickle protocol involving __setstate__ #46547

Closed
gpk mannequin opened this issue Mar 15, 2008 · 3 comments
Closed

Bug in Pickle protocol involving __setstate__ #46547

gpk mannequin opened this issue Mar 15, 2008 · 3 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@gpk
Copy link
Mannequin

gpk mannequin commented Mar 15, 2008

BPO 2294
Nosy @birkenfeld

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:

assignee = None
closed_at = <Date 2008-03-15.20:16:59.413>
created_at = <Date 2008-03-15.18:58:33.318>
labels = ['invalid', 'type-bug', 'library']
title = 'Bug in Pickle protocol involving __setstate__'
updated_at = <Date 2008-03-15.20:16:59.412>
user = 'https://bugs.python.org/gpk'

bugs.python.org fields:

activity = <Date 2008-03-15.20:16:59.412>
actor = 'georg.brandl'
assignee = 'none'
closed = True
closed_date = <Date 2008-03-15.20:16:59.413>
closer = 'georg.brandl'
components = ['Library (Lib)']
creation = <Date 2008-03-15.18:58:33.318>
creator = 'gpk'
dependencies = []
files = []
hgrepos = []
issue_num = 2294
keywords = []
message_count = 3.0
messages = ['63559', '63561', '63564']
nosy_count = 3.0
nosy_names = ['gpk', 'georg.brandl', 'QuantumTim']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue2294'
versions = ['Python 2.5']

@gpk
Copy link
Mannequin Author

gpk mannequin commented Mar 15, 2008

If we have a hierarchy of classes, and we use
__getstate__/setstate, the wrong class'
__setstate__ gets called.

Possibly, this is a documentation problem, but here goes:

Take two classes, A and B, where B is the child of A.

Construct a B. Pickle it. Unpickle it, and you find
that the __setstate__ function for A is called with the result
produced by B.__getstate__().

This is wrong.

An example follows:

import pickle as P


class A(object):
        def __init__(self, a):
                print 'A.__init__'
                self.a = a

        def __getstate__(self):
                print 'A.__getstate'
                return self.a

        def __setstate__(self, upstate):
                print 'A.__setstate', upstate
                self.a = upstate

class B(A):
        def __init__(self, a, b):
                print 'B.__init__'
                A.__init__(self, a)
                self.b = b

        def __getstate__(self):
                print 'B.__getstate'
                return (A.__getstate__(self), self.b)

        def __setstate(self, upstate):
        # This never gets called!
                print 'B.__setstate', upstate
                A.__setstate__(self, upstate[0])
                self.b = upstate[1]


        def __repr__(self):
                return '<B a=%d b=%d>' % (self.a, self.b)


q = B(1,2)
print '---'
r = P.loads(P.dumps(q, 0))
print 'q=', q
print 'r=', r

Now, run it:

$ python foo.py
B.__init__
A.__init__

B.__getstate
A.__getstate
A.__setstate (1, 2)
q= <B a=1 b=2, h=46912504218064>
r= Traceback (most recent call last):
  File "foo.py", line 44, in <module>
    print 'r=', r
  File "foo.py", line 37, in __repr__
    return '<B a=%d b=%d>' % (self.a, self.b)
AttributeError: 'B' object has no attribute 'b'
$

Note that this problem doesn't get noticed in the
common case where you simply pass __dict__ around
from __getstate__ to __setstate__. However, it
exists in many other use cases.

@gpk gpk mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Mar 15, 2008
@QuantumTim
Copy link
Mannequin

QuantumTim mannequin commented Mar 15, 2008

You've missed off the two underscores after the name __setstate__ :p

@birkenfeld
Copy link
Member

Closing as invalid.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant