From 42fab48739d40c72f3854476ce2a95fd18365505 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 20 Sep 2022 10:41:36 -0700 Subject: [PATCH 1/3] Add failing regression test --- Lib/test/test_compile.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 3ed57c2a5d2763..4b35cce5612a6e 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1077,6 +1077,31 @@ def aug(): check_op_count(aug, "STORE_SLICE", 1) check_op_count(aug, "BUILD_SLICE", 0) + def test_compare_positions(self): + for opname, op in [ + ("COMPARE_OP", "<"), + ("COMPARE_OP", "<="), + ("COMPARE_OP", ">"), + ("COMPARE_OP", ">="), + ("CONTAINS_OP", "in"), + ("CONTAINS_OP", "not in"), + ("IS_OP", "is"), + ("IS_OP", "is not"), + ]: + expr = f'a {op} b {op} c' + expected_positions = 2 * [(2, 2, 0, len(expr))] + for source in [ + f"\\\n{expr}", f'if \\\n{expr}: x', f"x if \\\n{expr} else y" + ]: + code = compile(source, "", "exec") + actual_positions = [ + instruction.positions + for instruction in dis.get_instructions(code) + if instruction.opname == opname + ] + with self.subTest(source): + self.assertEqual(actual_positions, expected_positions) + @requires_debug_ranges() class TestSourcePositions(unittest.TestCase): From 699db55d891669fb8cdc2a9e6c11373a27fdf9b7 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 20 Sep 2022 11:05:09 -0700 Subject: [PATCH 2/3] Fix positions for jumping comparisons --- Python/compile.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Python/compile.c b/Python/compile.c index 777190526d2575..9527bf0b57fdfc 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2905,6 +2905,7 @@ compiler_jump_if(struct compiler *c, expr_ty e, jump_target_label next, int cond return 1; } case Compare_kind: { + SET_LOC(c, e); Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; if (n > 0) { if (!check_compare(c, e)) { From db40cd20933b3d04f852271708918875833f66da Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 20 Sep 2022 11:06:53 -0700 Subject: [PATCH 3/3] blurb add --- .../2022-09-20-11-06-45.gh-issue-95921.dkcRQn.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-09-20-11-06-45.gh-issue-95921.dkcRQn.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-09-20-11-06-45.gh-issue-95921.dkcRQn.rst b/Misc/NEWS.d/next/Core and Builtins/2022-09-20-11-06-45.gh-issue-95921.dkcRQn.rst new file mode 100644 index 00000000000000..0c8b704c9510a6 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-09-20-11-06-45.gh-issue-95921.dkcRQn.rst @@ -0,0 +1,2 @@ +Fix overly-broad source position information for chained comparisons used as +branching conditions.