summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/copy.py4
-rw-r--r--Lib/test/test_copy.py11
2 files changed, 15 insertions, 0 deletions
diff --git a/Lib/copy.py b/Lib/copy.py
index 2646350..789488c 100644
--- a/Lib/copy.py
+++ b/Lib/copy.py
@@ -238,6 +238,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.__func__, deepcopy(x.__self__, memo))
+_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 2af2109..2df0deb 100644
--- a/Lib/test/test_copy.py
+++ b/Lib/test/test_copy.py
@@ -677,6 +677,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.__self__ is g)
+ g.b()
+
def global_foo(x, y): return x+y