]> 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:23:50 +0000 (10:23 -0700)
committerYury Selivanov <yselivanov@sprymix.com>
Mon, 23 Jun 2014 17:23:50 +0000 (10:23 -0700)
Closes #21801.

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

index 4ac76b1f51f582bc5f2e11a2d92992fb0acf76f0..b9cdcc25618a3138734f0ae0864576e3ab34b47b 100644 (file)
@@ -1939,6 +1939,10 @@ def _signature_from_callable(obj, *,
         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 7ad190b0562838b806f97243a32ba210d247ab98..63bdb154cdf60a690202a796546f39d3571daa82 100644 (file)
@@ -3136,6 +3136,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 b6042b28efc454f15ac692e77c2a5cece2059b45..428c8032d4bf6f40c85e0cac4c3d290aca29766b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -498,6 +498,9 @@ Library
 - Issue #11571: Ensure that the turtle window becomes the topmost window
   when launched on OS X.
 
+- Issue #21801: Validate that __signature__ is None or an instance of Signature.
+
+
 Extension Modules
 -----------------