]> granicus.if.org Git - python/commitdiff
Issue #1515: Enable use of deepcopy() with instance methods. Patch by Robert Collins.
authorAntoine Pitrou <solipsis@pitrou.net>
Sat, 28 Nov 2009 15:55:58 +0000 (15:55 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Sat, 28 Nov 2009 15:55:58 +0000 (15:55 +0000)
Lib/copy.py
Lib/test/test_copy.py
Misc/ACKS
Misc/NEWS

index d3db93d08d125ec103829ea505ddbf1ce4f88486..bbbd5b3313ccf06d50ed9e06fc746bace87c4c28 100644 (file)
@@ -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.
 
index ccbd2318606beabbe264f43f26881121f1140e35..823e9b36a32a4f4683095e9e17cca2ab955f5cc0 100644 (file)
@@ -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
 
index 61d297ddae251e38b766018530b365b4c7ddbd29..f3ef2e7a1d4fec002854a8c9b621284dca20c85a 100644 (file)
--- 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
index e915e29536de2dbf61d3fe1e26dc524801e3994b..2d6b7f0b0224012a6df77f48ac39477c77641d0f 100644 (file)
--- 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