]> granicus.if.org Git - python/commitdiff
Issue #28919: Simplify _copy_func_details() in unittest.mock
authorBerker Peksag <berker.peksag@gmail.com>
Thu, 15 Dec 2016 02:21:44 +0000 (05:21 +0300)
committerBerker Peksag <berker.peksag@gmail.com>
Thu, 15 Dec 2016 02:21:44 +0000 (05:21 +0300)
Patch by Jiajun Huang.

Lib/unittest/mock.py

index f1349198880c63c09769f13cb0e1a9be639a2682..367c1e19ce72ebfc5547bd521497654b4794a26a 100644 (file)
@@ -104,26 +104,16 @@ def _check_signature(func, mock, skipfirst, instance=False):
 
 
 def _copy_func_details(func, funcopy):
-    funcopy.__name__ = func.__name__
-    funcopy.__doc__ = func.__doc__
-    try:
-        funcopy.__text_signature__ = func.__text_signature__
-    except AttributeError:
-        pass
     # we explicitly don't copy func.__dict__ into this copy as it would
     # expose original attributes that should be mocked
-    try:
-        funcopy.__module__ = func.__module__
-    except AttributeError:
-        pass
-    try:
-        funcopy.__defaults__ = func.__defaults__
-    except AttributeError:
-        pass
-    try:
-        funcopy.__kwdefaults__ = func.__kwdefaults__
-    except AttributeError:
-        pass
+    for attribute in (
+        '__name__', '__doc__', '__text_signature__',
+        '__module__', '__defaults__', '__kwdefaults__',
+    ):
+        try:
+            setattr(funcopy, attribute, getattr(func, attribute))
+        except AttributeError:
+            pass
 
 
 def _callable(obj):