[Python-checkins] bpo-44622: Set line number of END_ASYNC_FOR to match that of iterator. (GH-27160)

markshannon webhook-mailer at python.org
Thu Jul 15 09:38:05 EDT 2021


https://github.com/python/cpython/commit/f333ab0f2edec26a769ed558263ac662e5475451
commit: f333ab0f2edec26a769ed558263ac662e5475451
branch: main
author: Mark Shannon <mark at hotpy.org>
committer: markshannon <mark at hotpy.org>
date: 2021-07-15T14:37:57+01:00
summary:

bpo-44622: Set line number of END_ASYNC_FOR to match that of iterator. (GH-27160)

files:
M Lib/test/test_compile.py
M Python/compile.c

diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index c994741176dc2..29bfd7145f0a8 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -943,10 +943,20 @@ def return_genexp():
         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
diff --git a/Python/compile.c b/Python/compile.c
index 50ff9b0666414..e21c7a594fe49 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -3002,7 +3002,9 @@ compiler_async_for(struct compiler *c, stmt_ty s)
     /* Except block for __anext__ */
     compiler_use_next_block(c, except);
 
-    UNSET_LOC(c);
+    /* 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 */



More information about the Python-checkins mailing list