]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-107265: Fix code_richcompare for ENTER_EXECUTOR case (gh-108165)
authorDong-hee Na <donghee.na@python.org>
Mon, 21 Aug 2023 05:50:09 +0000 (14:50 +0900)
committerGitHub <noreply@github.com>
Mon, 21 Aug 2023 05:50:09 +0000 (05:50 +0000)
Lib/test/test_capi/test_misc.py
Objects/codeobject.c

index 18a0476122dabfae6cca81da300c9513f2ea3632..ea0504333bab00efbe144401393eb2a0f0977c75 100644 (file)
@@ -2341,6 +2341,17 @@ class TestOptimizerAPI(unittest.TestCase):
             long_loop()
             self.assertEqual(opt.get_count(), 10)
 
+    def test_code_richcompare(self):
+        def testfunc(x):
+            i = 0
+            while i < x:
+                i += 1
+
+        opt = _testinternalcapi.get_counter_optimizer()
+        with temporary_optimizer(opt):
+            testfunc(1000)
+            self.assertEqual(testfunc.__code__, testfunc.__code__.replace())
+
 
 def get_first_executor(func):
     code = func.__code__
index 4d6efe938f45d6c43fd9c908d868d3bf416c3726..c34905c31960635782620db41bba6381e3b0b472 100644 (file)
@@ -1781,8 +1781,25 @@ code_richcompare(PyObject *self, PyObject *other, int op)
     for (int i = 0; i < Py_SIZE(co); i++) {
         _Py_CODEUNIT co_instr = _PyCode_CODE(co)[i];
         _Py_CODEUNIT cp_instr = _PyCode_CODE(cp)[i];
+
+        if (co_instr.op.code == ENTER_EXECUTOR) {
+            const int exec_index = co_instr.op.arg;
+            _PyExecutorObject *exec = co->co_executors->executors[exec_index];
+            co_instr.op.code = exec->vm_data.opcode;
+            co_instr.op.arg = exec->vm_data.oparg;
+        }
+        assert(co_instr.op.code != ENTER_EXECUTOR);
         co_instr.op.code = _PyOpcode_Deopt[co_instr.op.code];
+
+        if (cp_instr.op.code == ENTER_EXECUTOR) {
+            const int exec_index = cp_instr.op.arg;
+            _PyExecutorObject *exec = cp->co_executors->executors[exec_index];
+            cp_instr.op.code = exec->vm_data.opcode;
+            cp_instr.op.arg = exec->vm_data.oparg;
+        }
+        assert(cp_instr.op.code != ENTER_EXECUTOR);
         cp_instr.op.code = _PyOpcode_Deopt[cp_instr.op.code];
+
         eq = co_instr.cache == cp_instr.cache;
         if (!eq) {
             goto unequal;