diff options
author | Brandt Bucher <brandtbucher@microsoft.com> | 2022-09-20 19:22:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-20 19:22:24 (GMT) |
commit | dfc73b57247aac575c83055d960c03bdc28b51fd (patch) | |
tree | 6a997d0badcb1bfe45d6f4db41ef9d27e61145e0 /Lib | |
parent | 8563966be4f171ccf615105ef9d3a5aa65a1de68 (diff) | |
download | cpython-dfc73b57247aac575c83055d960c03bdc28b51fd.zip cpython-dfc73b57247aac575c83055d960c03bdc28b51fd.tar.gz cpython-dfc73b57247aac575c83055d960c03bdc28b51fd.tar.bz2 |
GH-95921: Fix positions for some chained comparisons (GH-96968)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_compile.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 3ed57c2..4b35cce 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1077,6 +1077,31 @@ if 1: 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, "<test>", "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): |