]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Backport:
authorGuido van Rossum <guido@python.org>
Thu, 6 Jun 2002 17:55:35 +0000 (17:55 +0000)
committerGuido van Rossum <guido@python.org>
Thu, 6 Jun 2002 17:55:35 +0000 (17:55 +0000)
Fix from SF patch 565085: copy._reduction doesn't __setstate__.
Straightforward fix.

Lib/copy.py
Lib/test/test_descr.py

index cf0b1af36712203d31ce94d80798bcc360cc6fe7..bfe06e342dc6e4694d88d3f99f96856bbfe7fe09 100644 (file)
@@ -301,7 +301,10 @@ def _reconstruct(x, info, deep, memo=None):
     if state:
         if deep:
             state = deepcopy(state, memo)
-        y.__dict__.update(state)
+        if hasattr(y, '__setstate__'):
+            y.__setstate__(state)
+        else:
+            y.__dict__.update(state)
     return y
 
 del d
index 5048b823f81b9a0e727e4244675bffec8b68be76..5ef3f8c41a496e8458979257de7696471ed177ec 100644 (file)
@@ -2931,6 +2931,34 @@ def imulbug():
     y *= "foo"
     vereq(y, (x, "foo"))
 
+def copy_setstate():
+    if verbose:
+        print "Testing that copy.*copy() correctly uses __setstate__..."
+    import copy
+    class C(object):
+        def __init__(self, foo=None):
+            self.foo = foo
+            self.__foo = foo
+        def setfoo(self, foo=None):
+            self.foo = foo
+        def getfoo(self):
+            return self.__foo
+        def __getstate__(self):
+            return [self.foo]
+        def __setstate__(self, lst):
+            assert len(lst) == 1
+            self.__foo = self.foo = lst[0]
+    a = C(42)
+    a.setfoo(24)
+    vereq(a.foo, 24)
+    vereq(a.getfoo(), 42)
+    b = copy.copy(a)
+    vereq(b.foo, 24)
+    vereq(b.getfoo(), 24)
+    b = copy.deepcopy(a)
+    vereq(b.foo, 24)
+    vereq(b.getfoo(), 24)
+
 def test_main():
     class_docstrings()
     lists()
@@ -2990,6 +3018,7 @@ def test_main():
     pickleslots()
     docdescriptor()
     imulbug()
+    copy_setstate()
     if verbose: print "All OK"
 
 if __name__ == "__main__":