functools.partial has no func_defaults on Py2
also simplify _copy_func_details to match upstream.
diff --git a/mock/mock.py b/mock/mock.py
index 7f7625f..0aaae50 100644
--- a/mock/mock.py
+++ b/mock/mock.py
@@ -206,29 +206,21 @@
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
if six.PY2:
- funcopy.func_defaults = func.func_defaults
- return
+ try:
+ funcopy.func_defaults = func.func_defaults
+ except AttributeError:
+ pass
def _callable(obj):
diff --git a/mock/tests/testhelpers.py b/mock/tests/testhelpers.py
index 2bfe2c8..ca31db3 100644
--- a/mock/tests/testhelpers.py
+++ b/mock/tests/testhelpers.py
@@ -1,6 +1,7 @@
# Copyright (C) 2007-2012 Michael Foord & the mock team
# E-mail: fuzzyman AT voidspace DOT org DOT uk
# http://www.voidspace.org.uk/python/mock/
+import socket
import six
import time
@@ -898,6 +899,10 @@
mocked(4, 5, 6)
mocked.assert_called_once_with(4, 5, 6)
+ def test_autospec_socket(self):
+ sock_class = create_autospec(socket.socket)
+ self.assertRaises(TypeError, sock_class, foo=1)
+
class TestCallList(unittest.TestCase):