Skip to content

Commit 4fdf3fd

Browse files
authored
gh-107265: Fix code_richcompare for ENTER_EXECUTOR case (gh-108165)
1 parent 04f7875 commit 4fdf3fd

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Diff for: Lib/test/test_capi/test_misc.py

+11
Original file line numberDiff line numberDiff line change
@@ -2341,6 +2341,17 @@ def long_loop():
23412341
long_loop()
23422342
self.assertEqual(opt.get_count(), 10)
23432343

2344+
def test_code_richcompare(self):
2345+
def testfunc(x):
2346+
i = 0
2347+
while i < x:
2348+
i += 1
2349+
2350+
opt = _testinternalcapi.get_counter_optimizer()
2351+
with temporary_optimizer(opt):
2352+
testfunc(1000)
2353+
self.assertEqual(testfunc.__code__, testfunc.__code__.replace())
2354+
23442355

23452356
def get_first_executor(func):
23462357
code = func.__code__

Diff for: Objects/codeobject.c

+17
Original file line numberDiff line numberDiff line change
@@ -1781,8 +1781,25 @@ code_richcompare(PyObject *self, PyObject *other, int op)
17811781
for (int i = 0; i < Py_SIZE(co); i++) {
17821782
_Py_CODEUNIT co_instr = _PyCode_CODE(co)[i];
17831783
_Py_CODEUNIT cp_instr = _PyCode_CODE(cp)[i];
1784+
1785+
if (co_instr.op.code == ENTER_EXECUTOR) {
1786+
const int exec_index = co_instr.op.arg;
1787+
_PyExecutorObject *exec = co->co_executors->executors[exec_index];
1788+
co_instr.op.code = exec->vm_data.opcode;
1789+
co_instr.op.arg = exec->vm_data.oparg;
1790+
}
1791+
assert(co_instr.op.code != ENTER_EXECUTOR);
17841792
co_instr.op.code = _PyOpcode_Deopt[co_instr.op.code];
1793+
1794+
if (cp_instr.op.code == ENTER_EXECUTOR) {
1795+
const int exec_index = cp_instr.op.arg;
1796+
_PyExecutorObject *exec = cp->co_executors->executors[exec_index];
1797+
cp_instr.op.code = exec->vm_data.opcode;
1798+
cp_instr.op.arg = exec->vm_data.oparg;
1799+
}
1800+
assert(cp_instr.op.code != ENTER_EXECUTOR);
17851801
cp_instr.op.code = _PyOpcode_Deopt[cp_instr.op.code];
1802+
17861803
eq = co_instr.cache == cp_instr.cache;
17871804
if (!eq) {
17881805
goto unequal;

0 commit comments

Comments
 (0)