Skip to content

Commit

Permalink
[3.13] gh-123942: add missing test for docstring-handling code in ast…
Browse files Browse the repository at this point in the history
…_opt.c (GH-123943) (#123955)

gh-123942: add missing test for docstring-handling code in ast_opt.c (GH-123943)
(cherry picked from commit 6e23c89)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
  • Loading branch information
miss-islington and iritkatriel authored Sep 30, 2024
1 parent 40b27d0 commit 793cb77
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,32 @@ def unused_code_at_end():
'RETURN_CONST',
list(dis.get_instructions(unused_code_at_end))[-1].opname)

@support.cpython_only
def test_docstring(self):
src = textwrap.dedent("""
def with_docstring():
"docstring"
def with_fstring():
f"not docstring"
def with_const_expression():
"also" + " not docstring"
""")

for opt in [0, 1, 2]:
with self.subTest(opt=opt):
code = compile(src, "<test>", "exec", optimize=opt)
ns = {}
exec(code, ns)

if opt < 2:
self.assertEqual(ns['with_docstring'].__doc__, "docstring")
else:
self.assertIsNone(ns['with_docstring'].__doc__)
self.assertIsNone(ns['with_fstring'].__doc__)
self.assertIsNone(ns['with_const_expression'].__doc__)

@support.cpython_only
def test_docstring_omitted(self):
# See gh-115347
Expand Down

0 comments on commit 793cb77

Please sign in to comment.