Skip to content

Commit 47695e3

Browse files
authored
bpo-44622: Set line number of END_ASYNC_FOR to match that of iterator. (GH-27160) (GH-27163)
(cherry picked from commit f333ab0)
1 parent 2ce8af3 commit 47695e3

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Lib/test/test_compile.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,10 +906,20 @@ def return_genexp():
906906
genexp_lines = [None, 1, 3, 1]
907907

908908
genexp_code = return_genexp.__code__.co_consts[1]
909-
code_lines = [None if line is None else line-return_genexp.__code__.co_firstlineno
909+
code_lines = [ None if line is None else line-return_genexp.__code__.co_firstlineno
910910
for (_, _, line) in genexp_code.co_lines() ]
911911
self.assertEqual(genexp_lines, code_lines)
912912

913+
def test_line_number_implicit_return_after_async_for(self):
914+
915+
async def test(aseq):
916+
async for i in aseq:
917+
body
918+
919+
expected_lines = [None, 1, 2, 1]
920+
code_lines = [ None if line is None else line-test.__code__.co_firstlineno
921+
for (_, _, line) in test.__code__.co_lines() ]
922+
self.assertEqual(expected_lines, code_lines)
913923

914924
def test_big_dict_literal(self):
915925
# The compiler has a flushing point in "compiler_dict" that calls compiles

Python/compile.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2923,7 +2923,9 @@ compiler_async_for(struct compiler *c, stmt_ty s)
29232923
/* Except block for __anext__ */
29242924
compiler_use_next_block(c, except);
29252925

2926-
c->u->u_lineno = -1;
2926+
/* Use same line number as the iterator,
2927+
* as the END_ASYNC_FOR succeeds the `for`, not the body. */
2928+
SET_LOC(c, s->v.AsyncFor.iter);
29272929
ADDOP(c, END_ASYNC_FOR);
29282930

29292931
/* `else` block */

0 commit comments

Comments
 (0)