From e38c5536e1e6a1669b568453516df4f7f55a4227 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sun, 20 Aug 2023 20:05:26 +0900 Subject: [PATCH 1/2] gh-107265: Fix code_richcompare to lookup actual opcode when ENTER_EXECUTOR --- Objects/codeobject.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 4d6efe938f45d6..0864fdd77d06de 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1781,8 +1781,23 @@ 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; + } + 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; + } + 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; From fb0ab4e4f329933d15d4e1b5f938296de9b19f5c Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Mon, 21 Aug 2023 11:45:17 +0900 Subject: [PATCH 2/2] Add test and compare oparg too --- Lib/test/test_capi/test_misc.py | 11 +++++++++++ Objects/codeobject.c | 2 ++ 2 files changed, 13 insertions(+) diff --git a/Lib/test/test_capi/test_misc.py b/Lib/test/test_capi/test_misc.py index 18a0476122dabf..ea0504333bab00 100644 --- a/Lib/test/test_capi/test_misc.py +++ b/Lib/test/test_capi/test_misc.py @@ -2341,6 +2341,17 @@ def long_loop(): 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__ diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 0864fdd77d06de..c34905c3196063 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1786,6 +1786,7 @@ code_richcompare(PyObject *self, PyObject *other, int op) 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]; @@ -1794,6 +1795,7 @@ code_richcompare(PyObject *self, PyObject *other, int op) 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];