Skip to content

Commit 5832597

Browse files
akulakovambv
andauthored
bpo-44808: fixes test for interactive inspect getsource of a class (GH-27571)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
1 parent f7c23a9 commit 5832597

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

Lib/test/test_inspect.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -586,19 +586,15 @@ def test_getsource_on_code_object(self):
586586
self.assertSourceEqual(mod.eggs.__code__, 12, 18)
587587

588588
class TestGetsourceInteractive(unittest.TestCase):
589-
def tearDown(self):
590-
mod.ParrotDroppings.__module__ = self.mod
591-
sys.modules['__main__'] = self.main
592-
593589
def test_getclasses_interactive(self):
594-
self.main = sys.modules['__main__']
595-
self.mod = mod.ParrotDroppings.__module__
596-
class MockModule:
597-
__file__ = None
598-
sys.modules['__main__'] = MockModule
599-
mod.ParrotDroppings.__module__ = '__main__'
600-
with self.assertRaisesRegex(OSError, 'source code not available') as e:
601-
inspect.getsource(mod.ParrotDroppings)
590+
# bpo-44648: simulate a REPL session;
591+
# there is no `__file__` in the __main__ module
592+
code = "import sys, inspect; \
593+
assert not hasattr(sys.modules['__main__'], '__file__'); \
594+
A = type('A', (), {}); \
595+
inspect.getsource(A)"
596+
_, _, stderr = assert_python_failure("-c", code, __isolated=True)
597+
self.assertIn(b'OSError: source code not available', stderr)
602598

603599
class TestGettingSourceOfToplevelFrames(GetSourceBase):
604600
fodderModule = mod

0 commit comments

Comments
 (0)