Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1fc0d2b

Browse files
committed
Merged revisions 76571 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r76571 | antoine.pitrou | 2009-11-28 16:55:58 +0100 (sam., 28 nov. 2009) | 3 lines Issue #1515: Enable use of deepcopy() with instance methods. Patch by Robert Collins. ........
1 parent 38478d8 commit 1fc0d2b

4 files changed

Lines changed: 19 additions & 0 deletions

File tree

Lib/copy.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ def _deepcopy_dict(x, memo):
238238
if PyStringMap is not None:
239239
d[PyStringMap] = _deepcopy_dict
240240

241+
def _deepcopy_method(x, memo): # Copy instance methods
242+
return type(x)(x.__func__, deepcopy(x.__self__, memo))
243+
_deepcopy_dispatch[types.MethodType] = _deepcopy_method
244+
241245
def _keep_alive(x, memo):
242246
"""Keeps a reference to the object x in the memo.
243247

Lib/test/test_copy.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,17 @@ def __init__(self, i):
677677
del d
678678
self.assertEqual(len(v), 1)
679679

680+
def test_deepcopy_bound_method(self):
681+
class Foo(object):
682+
def m(self):
683+
pass
684+
f = Foo()
685+
f.b = f.m
686+
g = copy.deepcopy(f)
687+
self.assertEqual(g.m, g.b)
688+
self.assertTrue(g.b.__self__ is g)
689+
g.b()
690+
680691

681692
def global_foo(x, y): return x+y
682693

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Josh Cogliati
144144
Dave Cole
145145
Benjamin Collar
146146
Jeffery Collins
147+
Robert Collins
147148
Paul Colomiets
148149
Matt Conway
149150
David M. Cooke

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ C-API
140140
Library
141141
-------
142142

143+
- Issue #1515: Enable use of deepcopy() with instance methods. Patch by
144+
Robert Collins.
145+
143146
- Issue #7403: logging: Fixed possible race condition in lock creation.
144147

145148
- Issue #6845: Add restart support for binary upload in ftplib. The

0 commit comments

Comments
 (0)