dis --- Python bytecode 的反組譯器¶
原始碼:Lib/dis.py
dis 模組支援反組譯分析 CPython bytecode。CPython bytecode 作為輸入的模組被定義於 Include/opcode.h 並且被編譯器和直譯器所使用。
CPython 實作細節: Bytecode is an implementation detail of the CPython interpreter. No guarantees are made that bytecode will not be added, removed, or changed between versions of Python. Use of this module should not be considered to work across Python VMs or Python releases.
在 3.6 版的變更: Use 2 bytes for each instruction. Previously the number of bytes varied by instruction.
在 3.10 版的變更: The argument of jump, exception handling and loop instructions is now the instruction offset rather than the byte offset.
在 3.11 版的變更: Some instructions are accompanied by one or more inline cache entries,
which take the form of CACHE instructions. These instructions
are hidden by default, but can be shown by passing show_caches=True to
any dis utility. Furthermore, the interpreter now adapts the
bytecode to specialize it for different runtime conditions. The
adaptive bytecode can be shown by passing adaptive=True.
在 3.12 版的變更: The argument of a jump is the offset of the target instruction relative
to the instruction that appears immediately after the jump instruction's
CACHE entries.
As a consequence, the presence of the CACHE instructions is
transparent for forward jumps but needs to be taken into account when
reasoning about backward jumps.
在 3.13 版的變更: The output shows logical labels rather than instruction offsets
for jump targets and exception handlers. The -O command line
option and the show_offsets argument were added.
Example: Given the function myfunc():
def myfunc(alist):
return len(alist)
可以使用以下指令來顯示 myfunc() 的反組譯:
>>> dis.dis(myfunc)
2 RESUME 0
3 LOAD_GLOBAL 1 (len + NULL)
LOAD_FAST_BORROW 0 (alist)
CALL 1
RETURN_VALUE
(The "2" is a line number).
命令列介面¶
The dis module can be invoked as a script from the command line:
python -m dis [-h] [-C] [-O] [-P] [-S] [infile]
可接受以下選項:
- -h, --help¶
Display usage and exit.
- -C, --show-caches¶
Show inline caches.
在 3.13 版被加入.
- -O, --show-offsets¶
Show offsets of instructions.
在 3.13 版被加入.
- -P, --show-positions¶
Show positions of instructions in the source code.
在 3.14 版被加入.
- -S, --specialized¶
Show specialized bytecode.
在 3.14 版被加入.
If infile is specified, its disassembled code will be written to stdout.
Otherwise, disassembly is performed on compiled source code received from stdin.
Bytecode analysis¶
在 3.4 版被加入.
The bytecode analysis API allows pieces of Python code to be wrapped in a
Bytecode object that provides easy access to details of the compiled
code.
- class dis.Bytecode(x, *, first_line=None, current_offset=None, show_caches=False, adaptive=False, show_offsets=False, show_positions=False)¶
Analyse the bytecode corresponding to a function, generator, asynchronous generator, coroutine, method, string of source code, or a code object (as returned by
compile()).This is a convenience wrapper around many of the functions listed below, most notably
get_instructions(), as iterating over aBytecodeinstance yields the bytecode operations asInstructioninstances.If first_line is not
None, it indicates the line number that should be reported for the first source line in the disassembled code. Otherwise, the source line information (if any) is taken directly from the disassembled code object.If current_offset is not
None, it refers to an instruction offset in the disassembled code. Setting this meansdis()will display a "current instruction" marker against the specified opcode.If show_caches is
True,dis()will display inline cache entries used by the interpreter to specialize the bytecode.If adaptive is
True,dis()will display specialized bytecode that may be different from the original bytecode.If show_offsets is
True,dis()will include instruction offsets in the output.If show_positions is
True,dis()will include instruction source code positions in the output.- classmethod from_traceback(tb, *, show_caches=False)¶
Construct a
Bytecodeinstance from the given traceback, setting current_offset to the instruction responsible for the exception.
- codeobj¶
The compiled code object.
- first_line¶
The first source line of the code object (if available)
- dis()¶
Return a formatted view of the bytecode operations (the same as printed by
dis.dis(), but returned as a multi-line string).
- info()¶
Return a formatted multi-line string with detailed information about the code object, like
code_info().
在 3.7 版的變更: This can now handle coroutine and asynchronous generator objects.
在 3.11 版的變更: 新增 show_caches 與 adaptive 參數。
在 3.13 版的變更: 新增 show_offsets 參數。
在 3.14 版的變更: 新增 show_positions 參數。
範例:
>>> bytecode = dis.Bytecode(myfunc)
>>> for instr in bytecode:
... print(instr.opname)
...
RESUME
LOAD_GLOBAL
LOAD_FAST_BORROW
CALL
RETURN_VALUE
分析函式¶
The dis module also defines the following analysis functions that convert
the input directly to the desired output. They can be useful if only a single
operation is being performed, so the intermediate analysis object isn't useful:
- dis.code_info(x)¶
Return a formatted multi-line string with detailed code object information for the supplied function, generator, asynchronous generator, coroutine, method, source code string or code object.
Note that the exact contents of code info strings are highly implementation dependent and they may change arbitrarily across Python VMs or Python releases.
在 3.2 版被加入.
在 3.7 版的變更: This can now handle coroutine and asynchronous generator objects.
- dis.show_code(x, *, file=None)¶
Print detailed code object information for the supplied function, method, source code string or code object to file (or
sys.stdoutif file is not specified).This is a convenient shorthand for
print(code_info(x), file=file), intended for interactive exploration at the interpreter prompt.在 3.2 版被加入.
在 3.4 版的變更: 新增 file 參數。
- dis.dis(x=None, *, file=None, depth=None, show_caches=False, adaptive=False, show_offsets=False, show_positions=False)¶
Disassemble the x object. x can denote either a module, a class, a method, a function, a generator, an asynchronous generator, a coroutine, a code object, a string of source code or a byte sequence of raw bytecode. For a module, it disassembles all functions. For a class, it disassembles all methods (including class and static methods). For a code object or sequence of raw bytecode, it prints one line per bytecode instruction. It also recursively disassembles nested code objects. These can include generator expressions, nested functions, the bodies of nested classes, and the code objects used for annotation scopes. Strings are first compiled to code objects with the
compile()built-in function before being disassembled. If no object is provided, this function disassembles the last traceback.The disassembly is written as text to the supplied file argument if provided and to
sys.stdoutotherwise.The maximal depth of recursion is limited by depth unless it is
None.depth=0means no recursion.If show_caches is
True, this function will display inline cache entries used by the interpreter to specialize the bytecode.If adaptive is
True, this function will display specialized bytecode that may be different from the original bytecode.在 3.4 版的變更: 新增 file 參數。
在 3.7 版的變更: Implemented recursive disassembling and added depth parameter.
在 3.7 版的變更: This can now handle coroutine and asynchronous generator objects.
在 3.11 版的變更: 新增 show_caches 與 adaptive 參數。
在 3.13 版的變更: 新增 show_offsets 參數。
在 3.14 版的變更: 新增 show_positions 參數。
- dis.distb(tb=None, *, file=None, show_caches=False, adaptive=False, show_offset=False, show_positions=False)¶
Disassemble the top-of-stack function of a traceback, using the last traceback if none was passed. The instruction causing the exception is indicated.
The disassembly is written as text to the supplied file argument if provided and to
sys.stdoutotherwise.在 3.4 版的變更: 新增 file 參數。
在 3.11 版的變更: 新增 show_caches 與 adaptive 參數。
在 3.13 版的變更: 新增 show_offsets 參數。
在 3.14 版的變更: 新增 show_positions 參數。
- dis.disassemble(code, lasti=-1, *, file=None, show_caches=False, adaptive=False, show_offsets=False, show_positions=False)¶
- dis.disco(code, lasti=-1, *, file=None, show_caches=False, adaptive=False, show_offsets=False, show_positions=False)¶
Disassemble a code object, indicating the last instruction if lasti was provided. The output is divided in the following columns:
the source code location of the instruction. Complete location information is shown if show_positions is true. Otherwise (the default) only the line number is displayed.
the current instruction, indicated as
-->,a labelled instruction, indicated with
>>,the address of the instruction,
the operation code name,
operation parameters, and
interpretation of the parameters in parentheses.
The parameter interpretation recognizes local and global variable names, constant values, branch targets, and compare operators.
The disassembly is written as text to the supplied file argument if provided and to
sys.stdoutotherwise.在 3.4 版的變更: 新增 file 參數。
在 3.11 版的變更: 新增 show_caches 與 adaptive 參數。
在 3.13 版的變更: 新增 show_offsets 參數。
在 3.14 版的變更: 新增 show_positions 參數。
- dis.get_instructions(x, *, first_line=None, show_caches=False, adaptive=False