Skip to content

Commit c051d55

Browse files
authored
gh-98390: Fix source locations of boolean sub-expressions (GH-98396)
1 parent debacd9 commit c051d55

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

Lib/test/test_compile.py

+26
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,32 @@ def test_multiline_expression(self):
12071207
self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
12081208
line=1, end_line=3, column=0, end_column=1)
12091209

1210+
def test_multiline_boolean_expression(self):
1211+
snippet = """\
1212+
if (a or
1213+
(b and not c) or
1214+
not (
1215+
d > 0)):
1216+
x = 42
1217+
"""
1218+
1219+
compiled_code, _ = self.check_positions_against_ast(snippet)
1220+
# jump if a is true:
1221+
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
1222+
line=1, end_line=1, column=4, end_column=5, occurrence=1)
1223+
# jump if b is false:
1224+
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_FALSE',
1225+
line=2, end_line=2, column=5, end_column=6, occurrence=1)
1226+
# jump if c is false:
1227+
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_FALSE',
1228+
line=2, end_line=2, column=15, end_column=16, occurrence=2)
1229+
# compare d and 0
1230+
self.assertOpcodeSourcePositionIs(compiled_code, 'COMPARE_OP',
1231+
line=4, end_line=4, column=8, end_column=13, occurrence=1)
1232+
# jump if comparison it True
1233+
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
1234+
line=4, end_line=4, column=8, end_column=13, occurrence=2)
1235+
12101236
def test_very_long_line_end_offset(self):
12111237
# Make sure we get the correct column offset for offsets
12121238
# too large to store in a byte.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix location of sub-expressions of boolean expressions, by reducing their scope to that of the sub-expression.

Python/compile.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2953,7 +2953,7 @@ compiler_jump_if(struct compiler *c, location *ploc,
29532953

29542954
/* general implementation */
29552955
VISIT(c, expr, e);
2956-
ADDOP_JUMP(c, *ploc, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2956+
ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
29572957
return 1;
29582958
}
29592959

0 commit comments

Comments
 (0)