summaryrefslogtreecommitdiff
path: root/mock/tests/testhelpers.py
diff options
context:
space:
mode:
authorAaron Gallagher <habnabit@google.com>2017-01-17 15:38:57 -0800
committerChris Withers <chris@withers.org>2018-11-29 21:34:59 +0000
commit6db8d961ac42ab6b4bd4e75e5925e6fa373135fc (patch)
treee69bec90107ab2767a15281851d23fb91aae5089 /mock/tests/testhelpers.py
parentc979e7515474ae26de92e7615e09d9877dc978af (diff)
downloadmock-6db8d961ac42ab6b4bd4e75e5925e6fa373135fc.tar.gz
Fix autospec's behavior on method-bound builtins.
Cython will, in the right circumstances, offer a MethodType instance where im_func is a builtin function. Any instance of MethodType is automatically assumed to be a python-defined function (more specifically, a function that has an inspectable signature), but _set_signature was still conservative in its assumptions. As a result _set_signature would return early with None instead of a mock since the im_func had no inspectable signature. This causes problems deeper inside mock, as _set_signature is assumed to _always_ return a mock, and nothing checked its return value. In similar corner cases, autospec will simply not check the spec of the function, so _set_signature is amended to now return early with the original, not-wrapped mock object.
Diffstat (limited to 'mock/tests/testhelpers.py')
-rw-r--r--mock/tests/testhelpers.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/mock/tests/testhelpers.py b/mock/tests/testhelpers.py
index eb38b21..2bfe2c8 100644
--- a/mock/tests/testhelpers.py
+++ b/mock/tests/testhelpers.py
@@ -3,6 +3,7 @@
# http://www.voidspace.org.uk/python/mock/
import six
+import time
import unittest
from mock import (
@@ -885,6 +886,18 @@ class SpecSignatureTest(unittest.TestCase):
mock_slot.assert_called_once_with(1, 2, 3)
mock_slot.abc.assert_called_once_with(4, 5, 6)
+ def test_autospec_on_bound_builtin_function(self):
+ meth = six.create_bound_method(time.ctime, time.time())
+ self.assertIsInstance(meth(), str)
+ mocked = create_autospec(meth)
+
+ # no signature, so no spec to check against
+ mocked()
+ mocked.assert_called_once_with()
+ mocked.reset_mock()
+ mocked(4, 5, 6)
+ mocked.assert_called_once_with(4, 5, 6)
+
class TestCallList(unittest.TestCase):