Skip to content

Commit 3be1a44

Browse files
authored
bpo-46724: Use JUMP_ABSOLUTE for all backward jumps. (GH-31326)
* Make sure all backward jumps use JUMP_ABSOLUTE. * Add news. * Fix up news item. * Make test use consistent style.
1 parent 12360aa commit 3be1a44

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Lib/test/test_compile.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,16 @@ def if_else_break():
10081008
elif instr.opname in HANDLED_JUMPS:
10091009
self.assertNotEqual(instr.arg, (line + 1)*INSTR_SIZE)
10101010

1011+
def test_no_wraparound_jump(self):
1012+
# See https://bugs.python.org/issue46724
1013+
1014+
def while_not_chained(a, b, c):
1015+
while not (a < b < c):
1016+
pass
1017+
1018+
for instr in dis.Bytecode(while_not_chained):
1019+
self.assertNotEqual(instr.opname, "EXTENDED_ARG")
1020+
10111021
@requires_debug_ranges()
10121022
class TestSourcePositions(unittest.TestCase):
10131023
# Ensure that compiled code snippets have correct line and column numbers
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make sure that all backwards jumps use the ``JUMP_ABSOLUTE`` instruction, rather
2+
than ``JUMP_FORWARD`` with an argument of ``(2**32)+offset``.

Python/compile.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7534,6 +7534,11 @@ normalize_jumps(struct assembler *a)
75347534
last->i_opcode = JUMP_FORWARD;
75357535
}
75367536
}
7537+
if (last->i_opcode == JUMP_FORWARD) {
7538+
if (last->i_target->b_visited == 1) {
7539+
last->i_opcode = JUMP_ABSOLUTE;
7540+
}
7541+
}
75377542
}
75387543
}
75397544

0 commit comments

Comments
 (0)