Content-Length: 293054 | pFad | http://github.com/python/cpython/commit/aced809dc484744c282be10799169bbdd50d65e2

C5 [3.10] GH-95921: Fix positions for some chained comparisons (GH-96968… · python/cpython@aced809 · GitHub
Skip to content

Commit

Permalink
[3.10] GH-95921: Fix positions for some chained comparisons (GH-96968) (
Browse files Browse the repository at this point in the history
GH-96974)

(cherry picked from commit dfc73b5)

Automerge-Triggered-By: GH:brandtbucher
  • Loading branch information
brandtbucher authored Sep 20, 2022
1 parent 21b5af9 commit aced809
Show file tree
Hide file tree
Showing 6 changed files with 1,666 additions and 1,632 deletions.
32 changes: 32 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix overly-broad source position information for chained comparisons used as
branching conditions.
1 change: 1 addition & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2691,6 +2691,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)) {
Expand Down
Loading

0 comments on commit aced809

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/cpython/commit/aced809dc484744c282be10799169bbdd50d65e2

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy