Commit 42407abe authored by Yury Selivanov's avatar Yury Selivanov

inspect: Validate that __signature__ is None or an instance of Signature.

Closes #21801.
parent 289cae4f
......@@ -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:
......
......@@ -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):
......
......@@ -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
-----------------
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment