]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44808: fixes test for interactive inspect getsource of a class (GH-27571)
authorandrei kulakov <andrei.avk@gmail.com>
Tue, 3 Aug 2021 12:47:30 +0000 (08:47 -0400)
committerGitHub <noreply@github.com>
Tue, 3 Aug 2021 12:47:30 +0000 (14:47 +0200)
Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Lib/test/test_inspect.py

index b0c42a2dcf6ce6771ea03f37b92377366b554c98..e87311ac0230fea121ee68ef37fa825a288870b8 100644 (file)
@@ -586,19 +586,15 @@ class TestRetrievingSourceCode(GetSourceBase):
         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