diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2024-09-13 14:06:06 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-13 14:06:06 (GMT) |
commit | a9594a34c62487961be86c0925daaa43bb467bb9 (patch) | |
tree | e7674482ca96b3e045ab3d5d9a7e56bbba5da0f0 /Lib/test/test_compile.py | |
parent | cfe6074d1fa81cf0684fbf8a623616441a1966e7 (diff) | |
download | cpython-a9594a34c62487961be86c0925daaa43bb467bb9.zip cpython-a9594a34c62487961be86c0925daaa43bb467bb9.tar.gz cpython-a9594a34c62487961be86c0925daaa43bb467bb9.tar.bz2 |
gh-124022: Fix bug where class docstring is removed in interactive mode (#124023)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Lib/test/test_compile.py')
-rw-r--r-- | Lib/test/test_compile.py | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index f22761f..736eff3 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -903,6 +903,28 @@ class TestSpecifics(unittest.TestCase): self.assertIsNone(ns['with_const_expression'].__doc__) @support.cpython_only + def test_docstring_interactive_mode(self): + srcs = [ + """def with_docstring(): + "docstring" + """, + """class with_docstring: + "docstring" + """, + ] + + for opt in [0, 1, 2]: + for src in srcs: + with self.subTest(opt=opt, src=src): + code = compile(textwrap.dedent(src), "<test>", "single", optimize=opt) + ns = {} + exec(code, ns) + if opt < 2: + self.assertEqual(ns['with_docstring'].__doc__, "docstring") + else: + self.assertIsNone(ns['with_docstring'].__doc__) + + @support.cpython_only def test_docstring_omitted(self): # See gh-115347 src = textwrap.dedent(""" @@ -919,12 +941,13 @@ class TestSpecifics(unittest.TestCase): return h """) for opt in [-1, 0, 1, 2]: - with self.subTest(opt=opt): - code = compile(src, "<test>", "exec", optimize=opt) - output = io.StringIO() - with contextlib.redirect_stdout(output): - dis.dis(code) - self.assertNotIn('NOP' , output.getvalue()) + for mode in ["exec", "single"]: + with self.subTest(opt=opt, mode=mode): + code = compile(src, "<test>", mode, optimize=opt) + output = io.StringIO() + with contextlib.redirect_stdout(output): + dis.dis(code) + self.assertNotIn('NOP', output.getvalue()) def test_dont_merge_constants(self): # Issue #25843: compile() must not merge constants which are equal |