Skip to content

Commit 6e23c89

Browse files
authored
gh-123942: add missing test for docstring-handling code in ast_opt.c (#123943)
1 parent c8d1dbe commit 6e23c89

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Lib/test/test_compile.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,32 @@ def unused_code_at_end():
870870
'RETURN_CONST',
871871
list(dis.get_instructions(unused_code_at_end))[-1].opname)
872872

873+
@support.cpython_only
874+
def test_docstring(self):
875+
src = textwrap.dedent("""
876+
def with_docstring():
877+
"docstring"
878+
879+
def with_fstring():
880+
f"not docstring"
881+
882+
def with_const_expression():
883+
"also" + " not docstring"
884+
""")
885+
886+
for opt in [0, 1, 2]:
887+
with self.subTest(opt=opt):
888+
code = compile(src, "<test>", "exec", optimize=opt)
889+
ns = {}
890+
exec(code, ns)
891+
892+
if opt < 2:
893+
self.assertEqual(ns['with_docstring'].__doc__, "docstring")
894+
else:
895+
self.assertIsNone(ns['with_docstring'].__doc__)
896+
self.assertIsNone(ns['with_fstring'].__doc__)
897+
self.assertIsNone(ns['with_const_expression'].__doc__)
898+
873899
@support.cpython_only
874900
def test_docstring_omitted(self):
875901
# See gh-115347

0 commit comments

Comments
 (0)