[Python-checkins] bpo-44808: fixes test for interactive inspect getsource of a class (GH-27571)

ambv webhook-mailer at python.org
Tue Aug 3 08:47:54 EDT 2021


https://github.com/python/cpython/commit/58325971de0faf330c9c38269dae8315a0746e59
commit: 58325971de0faf330c9c38269dae8315a0746e59
branch: main
author: andrei kulakov <andrei.avk at gmail.com>
committer: ambv <lukasz at langa.pl>
date: 2021-08-03T14:47:30+02:00
summary:

bpo-44808: fixes test for interactive inspect getsource of a class (GH-27571)

Co-authored-by: Łukasz Langa <lukasz at langa.pl>

files:
M Lib/test/test_inspect.py

diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index b0c42a2dcf6ce..e87311ac0230f 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -586,19 +586,15 @@ def test_getsource_on_code_object(self):
         self.assertSourceEqual(mod.eggs.__code__, 12, 18)
 
 class TestGetsourceInteractive(unittest.TestCase):
-    def tearDown(self):
-        mod.ParrotDroppings.__module__ = self.mod
-        sys.modules['__main__'] = self.main
-
     def test_getclasses_interactive(self):
-        self.main = sys.modules['__main__']
-        self.mod = mod.ParrotDroppings.__module__
-        class MockModule:
-            __file__ = None
-        sys.modules['__main__'] = MockModule
-        mod.ParrotDroppings.__module__ = '__main__'
-        with self.assertRaisesRegex(OSError, 'source code not available') as e:
-            inspect.getsource(mod.ParrotDroppings)
+        # bpo-44648: simulate a REPL session;
+        # there is no `__file__` in the __main__ module
+        code = "import sys, inspect; \
+                assert not hasattr(sys.modules['__main__'], '__file__'); \
+                A = type('A', (), {}); \
+                inspect.getsource(A)"
+        _, _, stderr = assert_python_failure("-c", code, __isolated=True)
+        self.assertIn(b'OSError: source code not available', stderr)
 
 class TestGettingSourceOfToplevelFrames(GetSourceBase):
     fodderModule = mod



More information about the Python-checkins mailing list