]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-98390: Fix source locations of boolean sub-expressions (GH-98396)
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Tue, 18 Oct 2022 16:18:38 +0000 (17:18 +0100)
committerGitHub <noreply@github.com>
Tue, 18 Oct 2022 16:18:38 +0000 (17:18 +0100)
Lib/test/test_compile.py
Misc/NEWS.d/next/Core and Builtins/2022-10-18-14-11-32.gh-issue-98390.H1sxJu.rst [new file with mode: 0644]
Python/compile.c

index f5c0c76478323a1edc873d0ef0fd1d651515e1d4..a5434d6fd512ed34c8618798336bf2e836e1710e 100644 (file)
@@ -1207,6 +1207,32 @@ f(
         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/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 (file)
index 0000000..6dac72b
--- /dev/null
@@ -0,0 +1 @@
+Fix location of sub-expressions of boolean expressions, by reducing their scope to that of the sub-expression.
index 5fbf6fe10d132ea214323f4f8ad1e63bf56bd233..4d5b41aa13003ca60681487ba36fab3f7c78ca07 100644 (file)
@@ -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;
 }