From bc4c6645edf8724f58cacee45d3b945a15443469 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Tue, 18 Oct 2022 13:20:29 +0100 Subject: [PATCH 1/2] gh-98390: Fix source locations of boolean sub-expressions --- Lib/test/test_compile.py | 26 ++++++++++++++++++++++++++ Python/compile.c | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 8bf8470ff16feb..36837bfa1fc1b0 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -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. diff --git a/Python/compile.c b/Python/compile.c index 5fbf6fe10d132e..4d5b41aa13003c 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -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; } From 23c3e99a21f4710963dbdd0e7160554cf488444e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 18 Oct 2022 14:11:34 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-10-18-14-11-32.gh-issue-98390.H1sxJu.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-10-18-14-11-32.gh-issue-98390.H1sxJu.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-10-18-14-11-32.gh-issue-98390.H1sxJu.rst b/Misc/NEWS.d/next/Core and Builtins/2022-10-18-14-11-32.gh-issue-98390.H1sxJu.rst new file mode 100644 index 00000000000000..6dac72b905c968 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-10-18-14-11-32.gh-issue-98390.H1sxJu.rst @@ -0,0 +1 @@ +Fix location of sub-expressions of boolean expressions, by reducing their scope to that of the sub-expression.