Skip to content

Commit d610d82

Browse files
authored
gh-112383: teach dis how to interpret ENTER_EXECUTOR (#117171)
1 parent 6c83352 commit d610d82

File tree

7 files changed

+120
-50
lines changed

7 files changed

+120
-50
lines changed

Diff for: Lib/dis.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
_specialized_opmap,
1818
)
1919

20+
from _opcode import get_executor
21+
2022
__all__ = ["code_info", "dis", "disassemble", "distb", "disco",
2123
"findlinestarts", "findlabels", "show_code",
2224
"get_instructions", "Instruction", "Bytecode"] + _opcodes_all
@@ -205,7 +207,27 @@ def _deoptop(op):
205207
return _all_opmap[deoptmap[name]] if name in deoptmap else op
206208

207209
def _get_code_array(co, adaptive):
208-
return co._co_code_adaptive if adaptive else co.co_code
210+
if adaptive:
211+
code = co._co_code_adaptive
212+
res = []
213+
found = False
214+
for i in range(0, len(code), 2):
215+
op, arg = code[i], code[i+1]
216+
if op == ENTER_EXECUTOR:
217+
try:
218+
ex = get_executor(co, i)
219+
except ValueError:
220+
ex = None
221+
222+
if ex:
223+
op, arg = ex.get_opcode(), ex.get_oparg()
224+
found = True
225+
226+
res.append(op.to_bytes())
227+
res.append(arg.to_bytes())
228+
return code if not found else b''.join(res)
229+
else:
230+
return co.co_code
209231

210232
def code_info(x):
211233
"""Formatted details of methods, functions, or code."""
@@ -514,8 +536,6 @@ def offset_from_jump_arg(self, op, arg, offset):
514536
argval = offset + 2 + signed_arg*2
515537
caches = _get_cache_size(_all_opname[deop])
516538
argval += 2 * caches
517-
if deop == ENTER_EXECUTOR:
518-
argval += 2
519539
return argval
520540
return None
521541

@@ -680,8 +700,7 @@ def _parse_exception_table(code):
680700

681701
def _is_backward_jump(op):
682702
return opname[op] in ('JUMP_BACKWARD',
683-
'JUMP_BACKWARD_NO_INTERRUPT',
684-
'ENTER_EXECUTOR')
703+
'JUMP_BACKWARD_NO_INTERRUPT')
685704

686705
def _get_instructions_bytes(code, linestarts=None, line_offset=0, co_positions=None,
687706
original_code=None, arg_resolver=None):

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

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import contextlib
2-
import opcode
32
import sys
43
import textwrap
54
import unittest
65
import gc
76
import os
87

8+
import _opcode
99
import _testinternalcapi
1010

1111
from test.support import script_helper, requires_specialization
@@ -115,13 +115,11 @@ def testfunc(x):
115115
def get_first_executor(func):
116116
code = func.__code__
117117
co_code = code.co_code
118-
JUMP_BACKWARD = opcode.opmap["JUMP_BACKWARD"]
119118
for i in range(0, len(co_code), 2):
120-
if co_code[i] == JUMP_BACKWARD:
121-
try:
122-
return _testinternalcapi.get_executor(code, i)
123-
except ValueError:
124-
pass
119+
try:
120+
return _opcode.get_executor(code, i)
121+
except ValueError:
122+
pass
125123
return None
126124

127125

@@ -760,17 +758,16 @@ def test_promote_globals_to_constants(self):
760758
result = script_helper.run_python_until_end('-c', textwrap.dedent("""
761759
import _testinternalcapi
762760
import opcode
761+
import _opcode
763762
764763
def get_first_executor(func):
765764
code = func.__code__
766765
co_code = code.co_code
767-
JUMP_BACKWARD = opcode.opmap["JUMP_BACKWARD"]
768766
for i in range(0, len(co_code), 2):
769-
if co_code[i] == JUMP_BACKWARD:
770-
try:
771-
return _testinternalcapi.get_executor(code, i)
772-
except ValueError:
773-
pass
767+
try:
768+
return _opcode.get_executor(code, i)
769+
except ValueError:
770+
pass
774771
return None
775772
776773
def get_opnames(ex):

Diff for: Lib/test/test_dis.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -1201,19 +1201,10 @@ def test_call_specialize(self):
12011201
@cpython_only
12021202
@requires_specialization
12031203
def test_loop_quicken(self):
1204-
import _testinternalcapi
12051204
# Loop can trigger a quicken where the loop is located
1206-
self.code_quicken(loop_test, 1)
1205+
self.code_quicken(loop_test, 4)
12071206
got = self.get_disassembly(loop_test, adaptive=True)
12081207
expected = dis_loop_test_quickened_code
1209-
if _testinternalcapi.get_optimizer():
1210-
# We *may* see ENTER_EXECUTOR in the disassembly. This is a
1211-
# temporary hack to keep the test working until dis is able to
1212-
# handle the instruction correctly (GH-112383):
1213-
got = got.replace(
1214-
"ENTER_EXECUTOR 16",
1215-
"JUMP_BACKWARD 16 (to L1)",
1216-
)
12171208
self.do_disassembly_compare(got, expected)
12181209

12191210
@cpython_only
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`dis` module's handling of ``ENTER_EXECUTOR`` instructions.

Diff for: Modules/_opcode.c

+23
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,28 @@ _opcode_get_intrinsic2_descs_impl(PyObject *module)
347347
return list;
348348
}
349349

350+
/*[clinic input]
351+
352+
_opcode.get_executor
353+
354+
code: object
355+
offset: int
356+
357+
Return the executor object at offset in code if exists, None otherwise.
358+
[clinic start generated code]*/
359+
360+
static PyObject *
361+
_opcode_get_executor_impl(PyObject *module, PyObject *code, int offset)
362+
/*[clinic end generated code: output=c035c7a47b16648f input=85eff93ea7aac282]*/
363+
{
364+
if (!PyCode_Check(code)) {
365+
PyErr_Format(PyExc_TypeError,
366+
"expected a code object, not '%.100s'",
367+
Py_TYPE(code)->tp_name);
368+
return NULL;
369+
}
370+
return (PyObject *)PyUnstable_GetExecutor((PyCodeObject *)code, offset);
371+
}
350372

351373
static PyMethodDef
352374
opcode_functions[] = {
@@ -363,6 +385,7 @@ opcode_functions[] = {
363385
_OPCODE_GET_NB_OPS_METHODDEF
364386
_OPCODE_GET_INTRINSIC1_DESCS_METHODDEF
365387
_OPCODE_GET_INTRINSIC2_DESCS_METHODDEF
388+
_OPCODE_GET_EXECUTOR_METHODDEF
366389
{NULL, NULL, 0, NULL}
367390
};
368391

Diff for: Modules/_testinternalcapi.c

-21
Original file line numberDiff line numberDiff line change
@@ -991,26 +991,6 @@ get_optimizer(PyObject *self, PyObject *Py_UNUSED(ignored))
991991
return opt;
992992
}
993993

994-
static PyObject *
995-
get_executor(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
996-
{
997-
998-
if (!_PyArg_CheckPositional("get_executor", nargs, 2, 2)) {
999-
return NULL;
1000-
}
1001-
PyObject *code = args[0];
1002-
PyObject *offset = args[1];
1003-
long ioffset = PyLong_AsLong(offset);
1004-
if (ioffset == -1 && PyErr_Occurred()) {
1005-
return NULL;
1006-
}
1007-
if (!PyCode_Check(code)) {
1008-
PyErr_SetString(PyExc_TypeError, "first argument must be a code object");
1009-
return NULL;
1010-
}
1011-
return (PyObject *)PyUnstable_GetExecutor((PyCodeObject *)code, ioffset);
1012-
}
1013-
1014994
static PyObject *
1015995
add_executor_dependency(PyObject *self, PyObject *args)
1016996
{
@@ -1836,7 +1816,6 @@ static PyMethodDef module_functions[] = {
18361816
{"iframe_getlasti", iframe_getlasti, METH_O, NULL},
18371817
{"get_optimizer", get_optimizer, METH_NOARGS, NULL},
18381818
{"set_optimizer", set_optimizer, METH_O, NULL},
1839-
{"get_executor", _PyCFunction_CAST(get_executor), METH_FASTCALL, NULL},
18401819
{"new_counter_optimizer", new_counter_optimizer, METH_NOARGS, NULL},
18411820
{"new_uop_optimizer", new_uop_optimizer, METH_NOARGS, NULL},
18421821
{"add_executor_dependency", add_executor_dependency, METH_VARARGS, NULL},

Diff for: Modules/clinic/_opcode.c.h

+61-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)