Skip to content

Commit 82e5c28

Browse files
authored
bpo-44297: Fix missing line number in generator expressions (GH-26801)
* Make sure that line number is set when entering comprehension scope in compiler.
1 parent 5d2b3a0 commit 82e5c28

File tree

4 files changed

+971
-952
lines changed

4 files changed

+971
-952
lines changed

Lib/test/test_compile.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,21 @@ def aug_store_attr():
895895
for (_, _, line) in func.__code__.co_lines() ]
896896
self.assertEqual(lines, code_lines)
897897

898+
def test_line_number_genexp(self):
899+
900+
def return_genexp():
901+
return (1
902+
for
903+
x
904+
in
905+
y)
906+
genexp_lines = [None, 1, 3, 1]
907+
908+
genexp_code = return_genexp.__code__.co_consts[1]
909+
code_lines = [None if line is None else line-return_genexp.__code__.co_firstlineno
910+
for (_, _, line) in genexp_code.co_lines() ]
911+
self.assertEqual(genexp_lines, code_lines)
912+
898913

899914
def test_big_dict_literal(self):
900915
# The compiler has a flushing point in "compiler_dict" that calls compiles
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Make sure that the line number is set when entering a comprehension scope.
2+
Ensures that backtraces inclusing generator expressions show the correct
3+
line number.

Python/compile.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4900,6 +4900,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
49004900
{
49014901
goto error;
49024902
}
4903+
SET_LOC(c, e);
49034904

49044905
is_async_generator = c->u->u_ste->ste_coroutine;
49054906

0 commit comments

Comments
 (0)