Skip to content

gh-120780: Show attribute name for LOAD_SPECIAL in dis output #120781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
_common_constants,
_intrinsic_1_descs,
_intrinsic_2_descs,
_special_method_names,
_specializations,
_specialized_opmap,
)
Expand Down Expand Up @@ -46,6 +47,7 @@
CALL_INTRINSIC_1 = opmap['CALL_INTRINSIC_1']
CALL_INTRINSIC_2 = opmap['CALL_INTRINSIC_2']
LOAD_COMMON_CONSTANT = opmap['LOAD_COMMON_CONSTANT']
LOAD_SPECIAL = opmap['LOAD_SPECIAL']
LOAD_FAST_LOAD_FAST = opmap['LOAD_FAST_LOAD_FAST']
STORE_FAST_LOAD_FAST = opmap['STORE_FAST_LOAD_FAST']
STORE_FAST_STORE_FAST = opmap['STORE_FAST_STORE_FAST']
Expand Down Expand Up @@ -609,6 +611,8 @@ def get_argval_argrepr(self, op, arg, offset):
argrepr = obj.__name__
else:
argrepr = repr(obj)
elif deop == LOAD_SPECIAL:
argrepr = _special_method_names[arg]
return argval, argrepr

def get_instructions(x, *, first_line=None, show_caches=None, adaptive=False):
Expand Down
1 change: 1 addition & 0 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

_intrinsic_1_descs = _opcode.get_intrinsic1_descs()
_intrinsic_2_descs = _opcode.get_intrinsic2_descs()
_special_method_names = _opcode.get_special_method_names()
_common_constants = [AssertionError, NotImplementedError]
_nb_ops = _opcode.get_nb_ops()

Expand Down
12 changes: 6 additions & 6 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,10 @@ def _with(c):

%4d LOAD_FAST 0 (c)
COPY 1
LOAD_SPECIAL 1
LOAD_SPECIAL 1 (__exit__)
SWAP 2
SWAP 3
LOAD_SPECIAL 0
LOAD_SPECIAL 0 (__enter__)
CALL 0
L1: POP_TOP

Expand Down Expand Up @@ -543,10 +543,10 @@ async def _asyncwith(c):

%4d LOAD_FAST 0 (c)
COPY 1
LOAD_SPECIAL 3
LOAD_SPECIAL 3 (__aexit__)
SWAP 2
SWAP 3
LOAD_SPECIAL 2
LOAD_SPECIAL 2 (__aenter__)
CALL 0
GET_AWAITABLE 1
LOAD_CONST 0 (None)
Expand Down Expand Up @@ -1738,10 +1738,10 @@ def _prepare_test_cases():
Instruction(opname='POP_TOP', opcode=29, arg=None, argval=None, argrepr='', offset=240, start_offset=240, starts_line=False, line_number=21, label=None, positions=None, cache_info=None),
Instruction(opname='LOAD_FAST', opcode=83, arg=0, argval='i', argrepr='i', offset=242, start_offset=242, starts_line=True, line_number=25, label=None, positions=None, cache_info=None),
Instruction(opname='COPY', opcode=58, arg=1, argval=1, argrepr='', offset=244, start_offset=244, starts_line=False, line_number=25, label=None, positions=None, cache_info=None),
Instruction(opname='LOAD_SPECIAL', opcode=91, arg=1, argval=1, argrepr='', offset=246, start_offset=246, starts_line=False, line_number=25, label=None, positions=None, cache_info=None),
Instruction(opname='LOAD_SPECIAL', opcode=91, arg=1, argval=1, argrepr='__exit__', offset=246, start_offset=246, starts_line=False, line_number=25, label=None, positions=None, cache_info=None),
Instruction(opname='SWAP', opcode=114, arg=2, argval=2, argrepr='', offset=248, start_offset=248, starts_line=False, line_number=25, label=None, positions=None, cache_info=None),
Instruction(opname='SWAP', opcode=114, arg=3, argval=3, argrepr='', offset=250, start_offset=250, starts_line=False, line_number=25, label=None, positions=None, cache_info=None),
Instruction(opname='LOAD_SPECIAL', opcode=91, arg=0, argval=0, argrepr='', offset=252, start_offset=252, starts_line=False, line_number=25, label=None, positions=None, cache_info=None),
Instruction(opname='LOAD_SPECIAL', opcode=91, arg=0, argval=0, argrepr='__enter__', offset=252, start_offset=252, starts_line=False, line_number=25, label=None, positions=None, cache_info=None),
Instruction(opname='CALL', opcode=50, arg=0, argval=0, argrepr='', offset=254, start_offset=254, starts_line=False, line_number=25, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('func_version', 2, b'\x00\x00\x00\x00')]),
Instruction(opname='STORE_FAST', opcode=109, arg=1, argval='dodgy', argrepr='dodgy', offset=262, start_offset=262, starts_line=False, line_number=25, label=None, positions=None, cache_info=None),
Instruction(opname='LOAD_GLOBAL', opcode=89, arg=3, argval='print', argrepr='print + NULL', offset=264, start_offset=264, starts_line=True, line_number=26, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('index', 1, b'\x00\x00'), ('module_keys_version', 1, b'\x00\x00'), ('builtin_keys_version', 1, b'\x00\x00')]),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show string value of LOAD_SPECIAL oparg in :mod:`dis` output.
28 changes: 28 additions & 0 deletions Modules/_opcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Python.h"
#include "compile.h"
#include "opcode.h"
#include "internal/pycore_ceval.h"
#include "internal/pycore_code.h"
#include "internal/pycore_compile.h"
#include "internal/pycore_intrinsics.h"
Expand Down Expand Up @@ -349,6 +350,32 @@ _opcode_get_intrinsic2_descs_impl(PyObject *module)

/*[clinic input]

_opcode.get_special_method_names

Return a list of special method names.
[clinic start generated code]*/

static PyObject *
_opcode_get_special_method_names_impl(PyObject *module)
/*[clinic end generated code: output=fce72614cd988d17 input=25f2115560bdf163]*/
{
PyObject *list = PyList_New(SPECIAL_MAX + 1);
if (list == NULL) {
return NULL;
}
for (int i=0; i <= SPECIAL_MAX; i++) {
PyObject *name = _Py_SpecialMethods[i].name;
if (name == NULL) {
Py_DECREF(list);
return NULL;
}
PyList_SET_ITEM(list, i, name);
}
return list;
}

/*[clinic input]

_opcode.get_executor

code: object
Expand Down Expand Up @@ -392,6 +419,7 @@ opcode_functions[] = {
_OPCODE_GET_INTRINSIC1_DESCS_METHODDEF
_OPCODE_GET_INTRINSIC2_DESCS_METHODDEF
_OPCODE_GET_EXECUTOR_METHODDEF
_OPCODE_GET_SPECIAL_METHOD_NAMES_METHODDEF
{NULL, NULL, 0, NULL}
};

Expand Down
20 changes: 19 additions & 1 deletion Modules/clinic/_opcode.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading