[3.10] GH-95921: Fix positions for some chained comparisons (GH-96968) (GH-96974)

(cherry picked from commit dfc73b5724)

Automerge-Triggered-By: GH:brandtbucher
This commit is contained in:
Brandt Bucher 2022-09-20 15:26:56 -07:00 committed by GitHub
parent 21b5af9072
commit aced809dc4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 1671 additions and 1637 deletions

View file

@ -984,6 +984,38 @@ def if_else_break():
elif instr.opname in HANDLED_JUMPS:
self.assertNotEqual(instr.arg, (line + 1)*INSTR_SIZE)
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_lines = 2 * [2]
for source in [
f"\\\n{expr}", f'if \\\n{expr}: x', f"x if \\\n{expr} else y"
]:
code = compile(source, "<test>", "exec")
all_lines = (
line
for start, stop, line in code.co_lines()
for _ in range(start, stop, 2)
)
actual_lines = [
line
for instruction, line in zip(
dis.get_instructions(code), all_lines, strict=True
)
if instruction.opname == opname
]
with self.subTest(source):
self.assertEqual(actual_lines, expected_lines)
class TestExpressionStackSize(unittest.TestCase):
# These tests check that the computed stack size for a code object