Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a48cb8f

Browse files
committed
Fix from SF patch 565085: copy._reduction doesn't __setstate__.
Straightforward fix. Will backport to 2.2. If there's ever a new 2.1 release, this could be backported there too (since it's an issue with anything that's got both a __reduce__ and a __setstate__).
1 parent 3e3583c commit a48cb8f

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lib/test/test_descr.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,6 +3071,34 @@ class MyStr(str):
30713071
except:
30723072
raise TestFailed, "string subclass allowed as exception"
30733073

3074+
def copy_setstate():
3075+
if verbose:
3076+
print "Testing that copy.*copy() correctly uses __setstate__..."
3077+
import copy
3078+
class C(object):
3079+
def __init__(self, foo=None):
3080+
self.foo = foo
3081+
self.__foo = foo
3082+
def setfoo(self, foo=None):
3083+
self.foo = foo
3084+
def getfoo(self):
3085+
return self.__foo
3086+
def __getstate__(self):
3087+
return [self.foo]
3088+
def __setstate__(self, lst):
3089+
assert len(lst) == 1
3090+
self.__foo = self.foo = lst[0]
3091+
a = C(42)
3092+
a.setfoo(24)
3093+
vereq(a.foo, 24)
3094+
vereq(a.getfoo(), 42)
3095+
b = copy.copy(a)
3096+
vereq(b.foo, 24)
3097+
vereq(b.getfoo(), 24)
3098+
b = copy.deepcopy(a)
3099+
vereq(b.foo, 24)
3100+
vereq(b.getfoo(), 24)
3101+
30743102
def do_this_first():
30753103
if verbose:
30763104
print "Testing SF bug 551412 ..."
@@ -3153,6 +3181,7 @@ def test_main():
31533181
imulbug()
31543182
docdescriptor()
31553183
string_exceptions()
3184+
copy_setstate()
31563185
if verbose: print "All OK"
31573186

31583187
if __name__ == "__main__":

0 commit comments

Comments
 (0)