Skip to content

Commit

Permalink
pythongh-107265: Fix code_hash for ENTER_EXECUTOR case
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Aug 21, 2023
1 parent 4fdf3fd commit ecef768
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
6 changes: 4 additions & 2 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2341,7 +2341,7 @@ def long_loop():
long_loop()
self.assertEqual(opt.get_count(), 10)

def test_code_richcompare(self):
def test_code_restore_for_ENTER_EXECUTOR(self):
def testfunc(x):
i = 0
while i < x:
Expand All @@ -2350,7 +2350,9 @@ def testfunc(x):
opt = _testinternalcapi.get_counter_optimizer()
with temporary_optimizer(opt):
testfunc(1000)
self.assertEqual(testfunc.__code__, testfunc.__code__.replace())
code, replace_code = testfunc.__code__, testfunc.__code__.replace()
self.assertEqual(code, replace_code)
self.assertEqual(hash(code), hash(replace_code))


def get_first_executor(func):
Expand Down
17 changes: 14 additions & 3 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1884,9 +1884,20 @@ code_hash(PyCodeObject *co)
SCRAMBLE_IN(Py_SIZE(co));
for (int i = 0; i < Py_SIZE(co); i++) {
int deop = _Py_GetBaseOpcode(co, i);
SCRAMBLE_IN(deop);
SCRAMBLE_IN(_PyCode_CODE(co)[i].op.arg);
i += _PyOpcode_Caches[deop];
if (deop == ENTER_EXECUTOR) {
// Assume that deopt of ENTER_EXECUTOR will be ENTER_EXECUTOR.
const int exec_index = _PyCode_CODE(co)[i].op.arg;
_PyExecutorObject *exec = co->co_executors->executors[exec_index];
assert(exec != NULL);
SCRAMBLE_IN(exec->vm_data.opcode);
SCRAMBLE_IN(exec->vm_data.oparg);
i += _PyOpcode_Caches[exec->vm_data.opcode];
}
else {
SCRAMBLE_IN(deop);
SCRAMBLE_IN(_PyCode_CODE(co)[i].op.arg);
i += _PyOpcode_Caches[deop];
}
}
if ((Py_hash_t)uhash == -1) {
return -2;
Expand Down

0 comments on commit ecef768

Please sign in to comment.