[Python-checkins] cpython (3.4): inspect: Validate that __signature__ is None or an instance of Signature.

yury.selivanov python-checkins at python.org
Mon Jun 23 19:24:02 CEST 2014


http://hg.python.org/cpython/rev/cc0f5d6ccb70
changeset:   91348:cc0f5d6ccb70
branch:      3.4
parent:      91346:389ef84c2823
user:        Yury Selivanov <yselivanov at sprymix.com>
date:        Mon Jun 23 10:21:04 2014 -0700
summary:
  inspect: Validate that __signature__ is None or an instance of Signature.

Closes #21801.

files:
  Lib/inspect.py           |  4 ++++
  Lib/test/test_inspect.py |  7 +++++++
  Misc/NEWS                |  2 ++
  3 files changed, 13 insertions(+), 0 deletions(-)


diff --git a/Lib/inspect.py b/Lib/inspect.py
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -1912,6 +1912,10 @@
         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:
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
@@ -3048,6 +3048,13 @@
         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):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -105,6 +105,8 @@
 - 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
 -----
 

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


More information about the Python-checkins mailing list