[Python-checkins] cpython: inspect.signature: Make sure that if a callable object has '_patialmethod'

yury.selivanov python-checkins at python.org
Wed Jan 29 18:19:15 CET 2014


http://hg.python.org/cpython/rev/e508c2b8fd44
changeset:   88816:e508c2b8fd44
user:        Yury Selivanov <yselivanov at sprymix.com>
date:        Wed Jan 29 12:18:59 2014 -0500
summary:
  inspect.signature: Make sure that if a callable object has '_patialmethod'
attribute, that attribute is an instance of 'functools.partialmethod'.

files:
  Lib/inspect.py           |  29 ++++++++++++++-------------
  Lib/test/test_inspect.py |   5 ++++
  2 files changed, 20 insertions(+), 14 deletions(-)


diff --git a/Lib/inspect.py b/Lib/inspect.py
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -1651,20 +1651,21 @@
     except AttributeError:
         pass
     else:
-        # Unbound partialmethod (see functools.partialmethod)
-        # This means, that we need to calculate the signature
-        # as if it's a regular partial object, but taking into
-        # account that the first positional argument
-        # (usually `self`, or `cls`) will not be passed
-        # automatically (as for boundmethods)
-
-        wrapped_sig = signature(partialmethod.func)
-        sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
-
-        first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
-        new_params = (first_wrapped_param,) + tuple(sig.parameters.values())
-
-        return sig.replace(parameters=new_params)
+        if isinstance(partialmethod, functools.partialmethod):
+            # Unbound partialmethod (see functools.partialmethod)
+            # This means, that we need to calculate the signature
+            # as if it's a regular partial object, but taking into
+            # account that the first positional argument
+            # (usually `self`, or `cls`) will not be passed
+            # automatically (as for boundmethods)
+
+            wrapped_sig = signature(partialmethod.func)
+            sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
+
+            first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
+            new_params = (first_wrapped_param,) + tuple(sig.parameters.values())
+
+            return sig.replace(parameters=new_params)
 
     if _signature_is_builtin(obj):
         return Signature.from_builtin(obj)
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -1983,6 +1983,11 @@
                            ('c', 1, ..., 'keyword_only')),
                           'spam'))
 
+    def test_signature_on_fake_partialmethod(self):
+        def foo(a): pass
+        foo._partialmethod = 'spam'
+        self.assertEqual(str(inspect.signature(foo)), '(a)')
+
     def test_signature_on_decorated(self):
         import functools
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list