]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-123942: add missing test for docstring-handling code in ast_opt.c (GH-12394...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 30 Sep 2024 01:21:57 +0000 (03:21 +0200)
committerGitHub <noreply@github.com>
Mon, 30 Sep 2024 01:21:57 +0000 (18:21 -0700)
gh-123942: add missing test for docstring-handling code in ast_opt.c (GH-123943)
(cherry picked from commit 6e23c89fcdd02b08fa6e9fa70d6e90763ddfc327)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Lib/test/test_compile.py

index b7e4d8478225710e33df595c04e8f48b8b706243..9c6bc31ff4922ef25cadb52ac9ad4f19e8250189 100644 (file)
@@ -870,6 +870,32 @@ class TestSpecifics(unittest.TestCase):
             '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