From 733ccf4b885c1824f491d089b4bd2ab05e4d59b8 Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Tue, 20 Sep 2022 12:22:24 -0700 Subject: [PATCH] [3.11] GH-95921: Fix positions for some chained comparisons (GH-96968). (cherry picked from commit dfc73b57247aac575c83055d960c03bdc28b51fd) Co-authored-by: Brandt Bucher --- Lib/test/test_compile.py | 26 +++++++++++++++++++ ...2-09-20-11-06-45.gh-issue-95921.dkcRQn.rst | 2 ++ Python/compile.c | 1 + 3 files changed, 29 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/Lib/test/test_compile.py b/Lib/test/test_compile.py index cd4d58acd4778e..23f84b48fac830 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1039,6 +1039,32 @@ def while_not_chained(a, b, c): for instr in dis.Bytecode(while_not_chained): self.assertNotEqual(instr.opname, "EXTENDED_ARG") + 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): # Ensure that compiled code snippets have correct line and column numbers 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. diff --git a/Python/compile.c b/Python/compile.c index 32fd58e6c4fd9d..f4555b35ab95a1 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2930,6 +2930,7 @@ compiler_jump_if(struct compiler *c, expr_ty e, basicblock *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)) {