diff options
author | Yury Selivanov <yselivanov@sprymix.com> | 2014-02-21 23:30:53 (GMT) |
---|---|---|
committer | Yury Selivanov <yselivanov@sprymix.com> | 2014-02-21 23:30:53 (GMT) |
commit | 8dfb4576a9a74a35f71a2f68a7236f2f0b1a7e63 (patch) | |
tree | fba3299799c958d3ff6247ce9c90d29c18d60673 /Lib | |
parent | e2df3ea682e4b51ed6357ed9e105b2618616e839 (diff) | |
download | cpython-8dfb4576a9a74a35f71a2f68a7236f2f0b1a7e63.zip cpython-8dfb4576a9a74a35f71a2f68a7236f2f0b1a7e63.tar.gz cpython-8dfb4576a9a74a35f71a2f68a7236f2f0b1a7e63.tar.bz2 |
inspect.signature: Check for function-like objects before builtins. Issue #17159
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/inspect.py | 8 | ||||
-rw-r--r-- | Lib/test/test_inspect.py | 16 |
2 files changed, 20 insertions, 4 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index b85fbcc..c7a2cf8 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1911,15 +1911,15 @@ def _signature_internal(obj, follow_wrapper_chains=True, skip_bound_arg=True): return sig.replace(parameters=new_params) - if _signature_is_builtin(obj): - return _signature_from_builtin(Signature, obj, - skip_bound_arg=skip_bound_arg) - if isfunction(obj) or _signature_is_functionlike(obj): # If it's a pure Python function, or an object that is duck type # of a Python function (Cython functions, for instance), then: return Signature.from_function(obj) + if _signature_is_builtin(obj): + return _signature_from_builtin(Signature, obj, + skip_bound_arg=skip_bound_arg) + if isinstance(obj, functools.partial): wrapped_sig = _signature_internal(obj.func, follow_wrapper_chains, diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 95c1b32..0dc7451 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -14,6 +14,7 @@ import sys import types import unicodedata import unittest +import unittest.mock try: from concurrent.futures import ThreadPoolExecutor @@ -1836,6 +1837,21 @@ class TestSignatureObject(unittest.TestCase): ('kwargs', ..., ..., "var_keyword")), ...)) + # Test with cython-like builtins: + _orig_isdesc = inspect.ismethoddescriptor + def _isdesc(obj): + if hasattr(obj, '_builtinmock'): + return True + return _orig_isdesc(obj) + + with unittest.mock.patch('inspect.ismethoddescriptor', _isdesc): + builtin_func = funclike(func) + # Make sure that our mock setup is working + self.assertFalse(inspect.ismethoddescriptor(builtin_func)) + builtin_func._builtinmock = True + self.assertTrue(inspect.ismethoddescriptor(builtin_func)) + self.assertEqual(inspect.signature(builtin_func), sig_func) + def test_signature_functionlike_class(self): # We only want to duck type function-like objects, # not classes. |