diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2024-09-11 14:39:53 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-11 14:39:53 (GMT) |
commit | 6e23c89fcdd02b08fa6e9fa70d6e90763ddfc327 (patch) | |
tree | 6e3a24dca61e9c9f4f0b2cc77ef990d3b7166dd1 | |
parent | c8d1dbef5b770b647aa7ff45fd5b269bc7629d0b (diff) | |
download | cpython-6e23c89fcdd02b08fa6e9fa70d6e90763ddfc327.zip cpython-6e23c89fcdd02b08fa6e9fa70d6e90763ddfc327.tar.gz cpython-6e23c89fcdd02b08fa6e9fa70d6e90763ddfc327.tar.bz2 |
gh-123942: add missing test for docstring-handling code in ast_opt.c (#123943)
-rw-r--r-- | Lib/test/test_compile.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 658ef11..7d6ddba 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -871,6 +871,32 @@ class TestSpecifics(unittest.TestCase): 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 src = textwrap.dedent(""" |