]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44622: Set line number of END_ASYNC_FOR to match that of iterator. (GH-27160...
authorMark Shannon <mark@hotpy.org>
Thu, 15 Jul 2021 14:54:38 +0000 (15:54 +0100)
committerGitHub <noreply@github.com>
Thu, 15 Jul 2021 14:54:38 +0000 (16:54 +0200)
(cherry picked from commit f333ab0f2edec26a769ed558263ac662e5475451)

Lib/test/test_compile.py
Python/compile.c

index 6dc1c383f8f2c9acb79ba19cc4cd37deb57b3acf..99ba4873cab6021841f6460228e9780624b55aac 100644 (file)
@@ -906,10 +906,20 @@ if 1:
         genexp_lines = [None, 1, 3, 1]
 
         genexp_code = return_genexp.__code__.co_consts[1]
-        code_lines = [None if line is None else line-return_genexp.__code__.co_firstlineno
+        code_lines = [ None if line is None else line-return_genexp.__code__.co_firstlineno
                       for (_, _, line) in genexp_code.co_lines() ]
         self.assertEqual(genexp_lines, code_lines)
 
+    def test_line_number_implicit_return_after_async_for(self):
+
+        async def test(aseq):
+            async for i in aseq:
+                body
+
+        expected_lines = [None, 1, 2, 1]
+        code_lines = [ None if line is None else line-test.__code__.co_firstlineno
+                      for (_, _, line) in test.__code__.co_lines() ]
+        self.assertEqual(expected_lines, code_lines)
 
     def test_big_dict_literal(self):
         # The compiler has a flushing point in "compiler_dict" that calls compiles
index 0508ea1df0ef04f791375eb78c706fa2ab21a8c7..9b7ce1939e92703e27153bf8430e80376d6dd806 100644 (file)
@@ -2923,7 +2923,9 @@ compiler_async_for(struct compiler *c, stmt_ty s)
     /* Except block for __anext__ */
     compiler_use_next_block(c, except);
 
-    c->u->u_lineno = -1;
+    /* Use same line number as the iterator,
+     * as the END_ASYNC_FOR succeeds the `for`, not the body. */
+    SET_LOC(c, s->v.AsyncFor.iter);
     ADDOP(c, END_ASYNC_FOR);
 
     /* `else` block */