]> granicus.if.org Git - python/commitdiff
inspect: Validate that __signature__ is None or an instance of Signature.
authorYury Selivanov <yselivanov@sprymix.com>
Mon, 23 Jun 2014 17:21:04 +0000 (10:21 -0700)
committerYury Selivanov <yselivanov@sprymix.com>
Mon, 23 Jun 2014 17:21:04 +0000 (10:21 -0700)
Closes #21801.

Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS

index 4c3e33df7a04c2d05161e32d9a91cad16e68f6a3..f6e1b4789f1c14bfbe0de83a0c77bec864dc457a 100644 (file)
@@ -1912,6 +1912,10 @@ def _signature_internal(obj, follow_wrapper_chains=True, skip_bound_arg=True):
         pass
     else:
         if sig is not None:
+            if not isinstance(sig, Signature):
+                raise TypeError(
+                    'unexpected object {!r} in __signature__ '
+                    'attribute'.format(sig))
             return sig
 
     try:
index 1ede3b5097adae86346f23ce1dab0c6e46060549..da0572d17a032df654ee59177755f464c48b254e 100644 (file)
@@ -3048,6 +3048,13 @@ class TestMain(unittest.TestCase):
         self.assertEqual(lines[:-1], inspect.getsource(module).splitlines())
         self.assertEqual(err, b'')
 
+    def test_custom_getattr(self):
+        def foo():
+            pass
+        foo.__signature__ = 42
+        with self.assertRaises(TypeError):
+            inspect.signature(foo)
+
     @unittest.skipIf(ThreadPoolExecutor is None,
             'threads required to test __qualname__ for source files')
     def test_qualname_source(self):
index 128937ef8743dc5294c7c535e604cccf12d4d399..52c1e0e0aee66ea0cafb8d9c851ac9601dd49ad0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -105,6 +105,8 @@ Library
 - Issue #21538: The plistlib module now supports loading of binary plist files
   when reference or offset size is not a power of two.
 
+- Issue #21801: Validate that __signature__ is None or an instance of Signature.
+
 Build
 -----