gh-124022: Fix bug where class docstring is removed in interactive mode (#124023)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Irit Katriel 2024-09-13 15:06:06 +01:00 committed by GitHub
parent cfe6074d1f
commit a9594a34c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 37 additions and 12 deletions

View file

@ -902,6 +902,28 @@ class TestSpecifics(unittest.TestCase):
self.assertIsNone(ns['with_fstring'].__doc__)
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
@ -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