From d16f57bf4db4417c8d7a038a0b61bb24461cd64a Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sat, 28 Nov 2009 15:55:58 +0000 Subject: Issue #1515: Enable use of deepcopy() with instance methods. Patch by Robert Collins. --- Lib/copy.py | 4 ++++ Lib/test/test_copy.py | 11 +++++++++++ Misc/ACKS | 1 + Misc/NEWS | 3 +++ 4 files changed, 19 insertions(+) diff --git a/Lib/copy.py b/Lib/copy.py index d3db93d..bbbd5b3 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -260,6 +260,10 @@ d[dict] = _deepcopy_dict if PyStringMap is not None: d[PyStringMap] = _deepcopy_dict +def _deepcopy_method(x, memo): # Copy instance methods + return type(x)(x.im_func, deepcopy(x.im_self, memo), x.im_class) +_deepcopy_dispatch[types.MethodType] = _deepcopy_method + def _keep_alive(x, memo): """Keeps a reference to the object x in the memo. diff --git a/Lib/test/test_copy.py b/Lib/test/test_copy.py index ccbd231..823e9b3 100644 --- a/Lib/test/test_copy.py +++ b/Lib/test/test_copy.py @@ -672,6 +672,17 @@ class TestCopy(unittest.TestCase): del d self.assertEqual(len(v), 1) + def test_deepcopy_bound_method(self): + class Foo(object): + def m(self): + pass + f = Foo() + f.b = f.m + g = copy.deepcopy(f) + self.assertEqual(g.m, g.b) + self.assertTrue(g.b.im_self is g) + g.b() + def global_foo(x, y): return x+y diff --git a/Misc/ACKS b/Misc/ACKS index 61d297d..f3ef2e7 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -145,6 +145,7 @@ Josh Cogliati Dave Cole Benjamin Collar Jeffery Collins +Robert Collins Paul Colomiets Matt Conway David M. Cooke diff --git a/Misc/NEWS b/Misc/NEWS index e915e29..2d6b7f0 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -483,6 +483,9 @@ Core and Builtins Library ------- +- Issue #1515: Enable use of deepcopy() with instance methods. Patch by + Robert Collins. + - Issue #7403: logging: Fixed possible race condition in lock creation. - Issue #6845: Add restart support for binary upload in ftplib. The -- cgit v0.12