Skip to content

Commit f333ab0

Browse files
authored
bpo-44622: Set line number of END_ASYNC_FOR to match that of iterator. (pythonGH-27160)
1 parent b83861f commit f333ab0

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
@@ -943,10 +943,20 @@ def return_genexp():
943943
genexp_lines = [None, 1, 3, 1]
944944

945945
genexp_code = return_genexp.__code__.co_consts[1]
946-
code_lines = [None if line is None else line-return_genexp.__code__.co_firstlineno
946+
code_lines = [ None if line is None else line-return_genexp.__code__.co_firstlineno
947947
for (_, _, line) in genexp_code.co_lines() ]
948948
self.assertEqual(genexp_lines, code_lines)
949949

950+
def test_line_number_implicit_return_after_async_for(self):
951+
952+
async def test(aseq):
953+
async for i in aseq:
954+
body
955+
956+
expected_lines = [None, 1, 2, 1]
957+
code_lines = [ None if line is None else line-test.__code__.co_firstlineno
958+
for (_, _, line) in test.__code__.co_lines() ]
959+
self.assertEqual(expected_lines, code_lines)
950960

951961
def test_big_dict_literal(self):
952962
# 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
@@ -3002,7 +3002,9 @@ compiler_async_for(struct compiler *c, stmt_ty s)
30023002
/* Except block for __anext__ */
30033003
compiler_use_next_block(c, except);
30043004

3005-
UNSET_LOC(c);
3005+
/* Use same line number as the iterator,
3006+
* as the END_ASYNC_FOR succeeds the `for`, not the body. */
3007+
SET_LOC(c, s->v.AsyncFor.iter);
30063008
ADDOP(c, END_ASYNC_FOR);
30073009

30083010
/* `else` block */

0 commit comments

Comments
 (0)