Skip to content

Commit 161a4dd

Browse files
committed
Issue python#28919: Simplify _copy_func_details() in unittest.mock
Patch by Jiajun Huang.
1 parent e660327 commit 161a4dd

1 file changed

Lines changed: 8 additions & 18 deletions

File tree

‎Lib/unittest/mock.py‎

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,16 @@ def checksig(_mock_self, *args, **kwargs):
104104

105105

106106
def _copy_func_details(func, funcopy):
107-
funcopy.__name__ = func.__name__
108-
funcopy.__doc__ = func.__doc__
109-
try:
110-
funcopy.__text_signature__ = func.__text_signature__
111-
except AttributeError:
112-
pass
113107
# we explicitly don't copy func.__dict__ into this copy as it would
114108
# expose original attributes that should be mocked
115-
try:
116-
funcopy.__module__ = func.__module__
117-
except AttributeError:
118-
pass
119-
try:
120-
funcopy.__defaults__ = func.__defaults__
121-
except AttributeError:
122-
pass
123-
try:
124-
funcopy.__kwdefaults__ = func.__kwdefaults__
125-
except AttributeError:
126-
pass
109+
for attribute in (
110+
'__name__', '__doc__', '__text_signature__',
111+
'__module__', '__defaults__', '__kwdefaults__',
112+
):
113+
try:
114+
setattr(funcopy, attribute, getattr(func, attribute))
115+
except AttributeError:
116+
pass
127117

128118

129119
def _callable(obj):

0 commit comments

Comments
 (0)