Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42407ab

Browse files
committed
inspect: Validate that __signature__ is None or an instance of Signature.
Closes #21801.
1 parent 289cae4 commit 42407ab

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Lib/inspect.py

+4
Original file line numberDiff line numberDiff line change
@@ -1939,6 +1939,10 @@ def _signature_from_callable(obj, *,
19391939
pass
19401940
else:
19411941
if sig is not None:
1942+
if not isinstance(sig, Signature):
1943+
raise TypeError(
1944+
'unexpected object {!r} in __signature__ '
1945+
'attribute'.format(sig))
19421946
return sig
19431947

19441948
try:

Lib/test/test_inspect.py

+7
Original file line numberDiff line numberDiff line change
@@ -3136,6 +3136,13 @@ def test_only_source(self):
31363136
self.assertEqual(lines[:-1], inspect.getsource(module).splitlines())
31373137
self.assertEqual(err, b'')
31383138

3139+
def test_custom_getattr(self):
3140+
def foo():
3141+
pass
3142+
foo.__signature__ = 42
3143+
with self.assertRaises(TypeError):
3144+
inspect.signature(foo)
3145+
31393146
@unittest.skipIf(ThreadPoolExecutor is None,
31403147
'threads required to test __qualname__ for source files')
31413148
def test_qualname_source(self):

Misc/NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ Library
498498
- Issue #11571: Ensure that the turtle window becomes the topmost window
499499
when launched on OS X.
500500

501+
- Issue #21801: Validate that __signature__ is None or an instance of Signature.
502+
503+
501504
Extension Modules
502505
-----------------
503506

0 commit comments

Comments
 (0)