From: Yury Selivanov Date: Mon, 23 Jun 2014 17:21:04 +0000 (-0700) Subject: inspect: Validate that __signature__ is None or an instance of Signature. X-Git-Tag: v3.4.2rc1~333 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c0f964fd55b0c5dbc8af895394258784735f1189;p=python inspect: Validate that __signature__ is None or an instance of Signature. Closes #21801. --- diff --git a/Lib/inspect.py b/Lib/inspect.py index 4c3e33df7a..f6e1b4789f 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -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: diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 1ede3b5097..da0572d17a 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -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): diff --git a/Misc/NEWS b/Misc/NEWS index 128937ef87..52c1e0e0ae 100644 --- 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 -----