From: Guido van Rossum Date: Thu, 6 Jun 2002 17:55:35 +0000 (+0000) Subject: Backport: X-Git-Tag: v2.2.2b1~340 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=56b36cdca14b10bc95a7840993dffa0eae4029cc;p=thirdparty%2FPython%2Fcpython.git Backport: Fix from SF patch 565085: copy._reduction doesn't __setstate__. Straightforward fix. --- diff --git a/Lib/copy.py b/Lib/copy.py index cf0b1af36712..bfe06e342dc6 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -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 diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 5048b823f81b..5ef3f8c41a49 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -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__":