]> granicus.if.org Git - python/commitdiff
inspect.signature: Check for function-like objects before builtins. Issue #17159
authorYury Selivanov <yselivanov@sprymix.com>
Fri, 21 Feb 2014 23:30:53 +0000 (18:30 -0500)
committerYury Selivanov <yselivanov@sprymix.com>
Fri, 21 Feb 2014 23:30:53 +0000 (18:30 -0500)
Lib/inspect.py
Lib/test/test_inspect.py

index b85fbcca6d86dcded3f546eca378bc4af2e936d1..c7a2cf8833f00bf4f5642d6e6224aa71502333e5 100644 (file)
@@ -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,
index 95c1b324fb6bd58b5a52439c3936e8b053584c9e..0dc74512a259a09750df9a2879944edf2fc107cb 100644 (file)
@@ -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.