[Python-checkins] bpo-46724: Use `JUMP_ABSOLUTE` for all backward jumps. (GH-31326)

markshannon webhook-mailer at python.org
Tue Feb 15 04:35:29 EST 2022


https://github.com/python/cpython/commit/3be1a443ca8e7d4ba85f95b78df5c4122cae9ede
commit: 3be1a443ca8e7d4ba85f95b78df5c4122cae9ede
branch: main
author: Mark Shannon <mark at hotpy.org>
committer: markshannon <mark at hotpy.org>
date: 2022-02-15T09:35:16Z
summary:

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.

files:
A Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst
M Lib/test/test_compile.py
M Python/compile.c

diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index 096501513c14b..7ebe837fce1b5 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -1008,6 +1008,16 @@ def if_else_break():
             elif instr.opname in HANDLED_JUMPS:
                 self.assertNotEqual(instr.arg, (line + 1)*INSTR_SIZE)
 
+    def test_no_wraparound_jump(self):
+        # See https://bugs.python.org/issue46724
+
+        def while_not_chained(a, b, c):
+            while not (a < b < c):
+                pass
+
+        for instr in dis.Bytecode(while_not_chained):
+            self.assertNotEqual(instr.opname, "EXTENDED_ARG")
+
 @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-02-14-14-44-06.bpo-46724.jym_K6.rst b/Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst
new file mode 100644
index 0000000000000..7324182677abf
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst	
@@ -0,0 +1,2 @@
+Make sure that all backwards jumps use the ``JUMP_ABSOLUTE`` instruction, rather
+than ``JUMP_FORWARD`` with an argument of ``(2**32)+offset``.
diff --git a/Python/compile.c b/Python/compile.c
index 1cf20d3a36ac1..786ef4e8ad3e2 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -7534,6 +7534,11 @@ normalize_jumps(struct assembler *a)
                 last->i_opcode = JUMP_FORWARD;
             }
         }
+        if (last->i_opcode == JUMP_FORWARD) {
+            if (last->i_target->b_visited == 1) {
+                last->i_opcode = JUMP_ABSOLUTE;
+            }
+        }
     }
 }
 



More information about the Python-checkins mailing list