]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-123942: add missing test for docstring-handling code in ast_opt.c (#123943)
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Wed, 11 Sep 2024 14:39:53 +0000 (15:39 +0100)
committerGitHub <noreply@github.com>
Wed, 11 Sep 2024 14:39:53 +0000 (15:39 +0100)
Lib/test/test_compile.py

index 658ef11f7bf44b86565b6335afde2422e8a0a807..7d6ddba1adc87efdc835f10b812dcc21b989c611 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