Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-98390: Fix source locations of boolean sub-expressions #98396

Merged
merged 2 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,32 @@ def test_multiline_expression(self):
self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
line=1, end_line=3, column=0, end_column=1)

def test_multiline_boolean_expression(self):
snippet = """\
if (a or
(b and not c) or
not (
d > 0)):
x = 42
"""

compiled_code, _ = self.check_positions_against_ast(snippet)
# jump if a is true:
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
line=1, end_line=1, column=4, end_column=5, occurrence=1)
# jump if b is false:
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_FALSE',
line=2, end_line=2, column=5, end_column=6, occurrence=1)
# jump if c is false:
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_FALSE',
line=2, end_line=2, column=15, end_column=16, occurrence=2)
# compare d and 0
self.assertOpcodeSourcePositionIs(compiled_code, 'COMPARE_OP',
line=4, end_line=4, column=8, end_column=13, occurrence=1)
# jump if comparison it True
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
line=4, end_line=4, column=8, end_column=13, occurrence=2)

def test_very_long_line_end_offset(self):
# Make sure we get the correct column offset for offsets
# too large to store in a byte.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix location of sub-expressions of boolean expressions, by reducing their scope to that of the sub-expression.
2 changes: 1 addition & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2953,7 +2953,7 @@ compiler_jump_if(struct compiler *c, location *ploc,

/* general implementation */
VISIT(c, expr, e);
ADDOP_JUMP(c, *ploc, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
return 1;
}

Expand Down