Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test and compare oparg too
  • Loading branch information
corona10 committed Aug 21, 2023
commit fb0ab4e4f329933d15d4e1b5f938296de9b19f5c
11 changes: 11 additions & 0 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
2 changes: 2 additions & 0 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing two code objects must not modify the code object.

The interaction between executor, the specializer and instrumentation is subtle and likely to break without care.
This will leak executors, as it flip-flops between JUMP_BACKWARDS and ENTER_EXECUTOR, or worse if an optimizer assumes that a single instruction will only be seen once.

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];
Expand All @@ -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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise

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];
Expand Down