From 42407abe2450819b74b9ccb1fbe99edc78bbbb41 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Mon, 23 Jun 2014 10:23:50 -0700 Subject: [PATCH] inspect: Validate that __signature__ is None or an instance of Signature. Closes #21801. --- Lib/inspect.py | 4 ++++ Lib/test/test_inspect.py | 7 +++++++ Misc/NEWS | 3 +++ 3 files changed, 14 insertions(+) diff --git a/Lib/inspect.py b/Lib/inspect.py index 4ac76b1f51..b9cdcc2561 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -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: diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 7ad190b056..63bdb154cd 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -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): diff --git a/Misc/NEWS b/Misc/NEWS index b6042b28ef..428c8032d4 100644 --- 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 ----------------- -- 2.50.1