[ python-Bugs-651701 ] Bozo getstate w/ slots overrides base
SourceForge.net
noreply at sourceforge.net
Mon Nov 24 00:28:01 EST 2003
Bugs item #651701, was opened at 2002-12-10 23:10
Message generated for change (Comment added) made by astraw
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=651701&group_id=5470
Category: Type/class unification
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Peter Fein (pafein)
Assigned to: Nobody/Anonymous (nobody)
Summary: Bozo getstate w/ slots overrides base
Initial Comment:
The bozo __getstate__ set automatically on classes
defining __slots__ overrides any __getstate__ defined
in a base class. This makes writing a mixin to
implement this method impossible.
----------------------------------------------------------------------
Comment By: Andrew Straw (astraw)
Date: 2003-11-24 05:28
Message:
Logged In: YES
user_id=210276
I'm experiencing this behavior on Python 2.2.2 and 2.2.3. I don't
get it on 2.2(.0) or 2.3(.x).
Here's a script that demonstrates: (Sorry SF doesn't seem to
have a nice attachment feature for comments.)
import sys
print 'Python version',sys.version
print
def recursive_base_class_finder(klass):
"""A function to find all base classes."""
result = [klass]
for base_class in klass.__bases__:
for base_base_class in
recursive_base_class_finder(base_class):
result.append(base_base_class)
# Make only a single copy of each class found
result2 = []
for r in result:
if r not in result2:
result2.append(r)
return result2
class A(object):
__slots__ = ['a']
def __getstate__(self):
"""support for being pickled"""
result = {}
classes = recursive_base_class_finder(self.__class__)
for klass in classes:
if hasattr(klass,'__slots__'):
for attr in klass.__slots__:
if hasattr(self,attr):
result[attr] = getattr(self,attr)
return result
def __setstate__(self,dict):
for attr in dict.keys():
setattr(self,attr,dict[attr])
def __init__(self):
self.a='a'
class B(A):
__slots__ = ['b']
def __init__(self):
A.__init__(self)
self.b = 'b'
a1=A()
b1=B()
import pickle
ap = pickle.dumps(a1)
bp = pickle.dumps(b1)
a2 = pickle.loads(ap)
b2 = pickle.loads(bp)
if a2.a == 'a':
print 'pickling/unpickling ok'
else:
print 'WARNING: pickling/unpickling failed'
if b2.a == 'a' and b2.b == 'b':
print 'pickling/unpickling on derived class ok'
else:
print 'WARNING: pickling/unpickling on dericed class failed'
try:
a2.xx = 'x'
failed = 0
except:
failed = 1
if not failed:
print "WARNING: assigned to attribute not in __slots__"
try:
b2.xx = 'x'
failed = 0
except:
failed = 1
if not failed:
print "WARNING: assigned to attribute in derived class not in
__slots__"
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2003-07-12 01:36
Message:
Logged In: YES
user_id=80475
Peter, is this still an issue?
Can you attach a script demonstrating the problem?
----------------------------------------------------------------------
Comment By: Neal Norwitz (nnorwitz)
Date: 2003-05-22 22:15
Message:
Logged In: YES
user_id=33168
There was an issue with that, IIRC. There was a discussion
on python-dev a while ago which Guido talked about the
issues. I don't remember when or what the issues were.
Probably about 3-6 months ago. I think the code in this
area may have changed, so it would be beneficial to test
with 2.3b1.
----------------------------------------------------------------------
Comment By: Peter Fein (pafein)
Date: 2003-05-22 22:09
Message:
Logged In: YES
user_id=639329
This issue is that although the base class B defines a
__getstate__, the class C2 overrides it b/c it contains
__slots__. Basically, I'm suggesting that base classes
should be checked for getstate instead of only looking at
the class that defines slots before adding the bozo'd version.
Unfortunately, I don't have a 2.3* version to play with at
the moment. Please let me know if this clarifies things.
----------------------------------------------------------------------
Comment By: Neal Norwitz (nnorwitz)
Date: 2003-05-22 21:58
Message:
Logged In: YES
user_id=33168
What do you expect to be printed? I'm not sure what you
want changed. Have you tested with 2.3b1? Does this meet
your needs?
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=651701&group_id=5470
More information about the Python-bugs-list
mailing list