Skip to content

Commit e2732d3

Browse files
bpo-32970: Improve disassembly of the MAKE_FUNCTION instruction. (GH-5937)
1 parent 3f7e9aa commit e2732d3

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

Lib/dis.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
classmethod, staticmethod, type)
1818

1919
FORMAT_VALUE = opmap['FORMAT_VALUE']
20+
FORMAT_VALUE_CONVERTERS = (
21+
(None, ''),
22+
(str, 'str'),
23+
(repr, 'repr'),
24+
(ascii, 'ascii'),
25+
)
26+
MAKE_FUNCTION = opmap['MAKE_FUNCTION']
27+
MAKE_FUNCTION_FLAGS = ('defaults', 'kwdefaults', 'annotations', 'closure')
28+
2029

2130
def _try_compile(source, name):
2231
"""Attempts to compile the given source, first as an expression and
@@ -339,12 +348,15 @@ def _get_instructions_bytes(code, varnames=None, names=None, constants=None,
339348
elif op in hasfree:
340349
argval, argrepr = _get_name_info(arg, cells)
341350
elif op == FORMAT_VALUE:
342-
argval = ((None, str, repr, ascii)[arg & 0x3], bool(arg & 0x4))
343-
argrepr = ('', 'str', 'repr', 'ascii')[arg & 0x3]
351+
argval, argrepr = FORMAT_VALUE_CONVERTERS[arg & 0x3]
352+
argval = (argval, bool(arg & 0x4))
344353
if argval[1]:
345354
if argrepr:
346355
argrepr += ', '
347356
argrepr += 'with format'
357+
elif op == MAKE_FUNCTION:
358+
argrepr = ', '.join(s for i, s in enumerate(MAKE_FUNCTION_FLAGS)
359+
if arg & (1<<i))
348360
yield Instruction(opname[op], op,
349361
arg, argval, argrepr,
350362
offset, starts_line, is_jump_target)

Lib/test/test_dis.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def foo(x):
348348
2 BUILD_TUPLE 1
349349
4 LOAD_CONST 1 (<code object foo at 0x..., file "%s", line %d>)
350350
6 LOAD_CONST 2 ('_h.<locals>.foo')
351-
8 MAKE_FUNCTION 8
351+
8 MAKE_FUNCTION 8 (closure)
352352
10 STORE_FAST 1 (foo)
353353
354354
%3d 12 LOAD_FAST 1 (foo)
@@ -365,7 +365,7 @@ def foo(x):
365365
2 BUILD_TUPLE 1
366366
4 LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
367367
6 LOAD_CONST 2 ('_h.<locals>.foo.<locals>.<listcomp>')
368-
8 MAKE_FUNCTION 8
368+
8 MAKE_FUNCTION 8 (closure)
369369
10 LOAD_DEREF 1 (y)
370370
12 GET_ITER
371371
14 CALL_FUNCTION 1
@@ -862,7 +862,7 @@ def jumpy():
862862
Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False),
863863
Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argrepr=repr(code_object_f), offset=8, starts_line=None, is_jump_target=False),
864864
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f', argrepr="'outer.<locals>.f'", offset=10, starts_line=None, is_jump_target=False),
865-
Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='', offset=12, starts_line=None, is_jump_target=False),
865+
Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=12, starts_line=None, is_jump_target=False),
866866
Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False),
867867
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=16, starts_line=7, is_jump_target=False),
868868
Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=18, starts_line=None, is_jump_target=False),
@@ -887,7 +887,7 @@ def jumpy():
887887
Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False),
888888
Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=12, starts_line=None, is_jump_target=False),
889889
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.<locals>.inner', argrepr="'outer.<locals>.f.<locals>.inner'", offset=14, starts_line=None, is_jump_target=False),
890-
Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='', offset=16, starts_line=None, is_jump_target=False),
890+
Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=16, starts_line=None, is_jump_target=False),
891891
Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=18, starts_line=None, is_jump_target=False),
892892
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=5, is_jump_target=False),
893893
Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved disassembly of the MAKE_FUNCTION instruction.

0 commit comments

Comments
 (0)