From bd8ab7e1c0a1d6d181ca6594e06d69a6ab703eca Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Thu, 15 Jul 2021 16:41:06 -0400 Subject: [PATCH 1/3] Fix error type in inspect.getsource() in interactive session --- Lib/inspect.py | 2 ++ Lib/test/test_inspect.py | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 89b2e722be8fad..e532913ea5d6c9 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -781,6 +781,8 @@ def getfile(object): module = sys.modules.get(object.__module__) if getattr(module, '__file__', None): return module.__file__ + if object.__module__ == '__main__': + raise OSError('source code not available') raise TypeError('{!r} is a built-in class'.format(object)) if ismethod(object): object = object.__func__ diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 69f17f2477a2f2..f1c9a7d5509543 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -585,6 +585,15 @@ def monkey(filename, module_globals=None): def test_getsource_on_code_object(self): self.assertSourceEqual(mod.eggs.__code__, 12, 18) +class TestGetsourceInteractive(unittest.TestCase): + def tearDown(self): + mod.ParrotDroppings.__module__ = mod + + def test_getclasses_interactive(self): + mod.ParrotDroppings.__module__ = '__main__' + with self.assertRaises(OSError) as e: + inspect.getsource(mod.ParrotDroppings) + class TestGettingSourceOfToplevelFrames(GetSourceBase): fodderModule = mod @@ -4301,7 +4310,8 @@ def test_main(): TestBoundArguments, TestSignaturePrivateHelpers, TestSignatureDefinitions, TestIsDataDescriptor, TestGetClosureVars, TestUnwrap, TestMain, TestReload, - TestGetCoroutineState, TestGettingSourceOfToplevelFrames + TestGetCoroutineState, TestGettingSourceOfToplevelFrames, + TestGetsourceInteractive, ) if __name__ == "__main__": From cd2700954be78fbe58ffe5cd3b3c5336a4e002b9 Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Thu, 15 Jul 2021 16:51:48 -0400 Subject: [PATCH 2/3] news entry --- .../next/Library/2021-07-15-16-51-32.bpo-44648.2o49TB.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-07-15-16-51-32.bpo-44648.2o49TB.rst diff --git a/Misc/NEWS.d/next/Library/2021-07-15-16-51-32.bpo-44648.2o49TB.rst b/Misc/NEWS.d/next/Library/2021-07-15-16-51-32.bpo-44648.2o49TB.rst new file mode 100644 index 00000000000000..f7171c3c84c5ea --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-07-15-16-51-32.bpo-44648.2o49TB.rst @@ -0,0 +1,3 @@ +Fixed wrong error being thrown by :func:`inspect.getsource` when examining a +class in the interactive session. Instead of :exc:`TypeError`, it should be +:exc:`OSError` with appropriate error message. From d230183c3d97b66a4e56e56accf759239d5986e1 Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Fri, 16 Jul 2021 11:32:34 -0400 Subject: [PATCH 3/3] fix unit test to check for the right error msg --- Lib/test/test_inspect.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index f1c9a7d5509543..c1252c32ab2504 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -588,10 +588,15 @@ def test_getsource_on_code_object(self): class TestGetsourceInteractive(unittest.TestCase): def tearDown(self): mod.ParrotDroppings.__module__ = mod + sys.modules['__main__'] = self.main def test_getclasses_interactive(self): + self.main = sys.modules['__main__'] + class MockModule: + __file__ = None + sys.modules['__main__'] = MockModule mod.ParrotDroppings.__module__ = '__main__' - with self.assertRaises(OSError) as e: + with self.assertRaisesRegex(OSError, 'source code not available') as e: inspect.getsource(mod.ParrotDroppings) class TestGettingSourceOfToplevelFrames(GetSourceBase):