dis — Disassembler for Python bytecode — Python 3.11.18 documentation
Navigation index modules | next | previous | Python » 3.11.18 Documentation » The Python Standard Library » Python Language Services » dis — Disassembler for Python bytecode | Theme | dis — Disassembler for Python bytecode ¶ Source code: Lib/dis.py The dis module supports the analysis of CPython bytecode by disassembling it. The CPython bytecode which this module takes as an input is defined in the file Include/opcode.h and used by the compiler and the interpreter.
CPython implementation detail: 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. Changed in version 3.6: Use 2 bytes for each instruction. Previously the number of bytes varied by instruction. Changed in version 3.10: The argument of jump, exception handling and loop instructions is now the instruction offset rather than the byte offset.
Changed in version 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 . Example: Given the function myfunc() : def myfunc ( alist ): return len ( alist ) the following command can be used to display the disassembly of myfunc() : >>> dis . dis ( myfunc ) 2 0 RESUME 0 3 2 LOAD_GLOBAL 1 (NULL + len) 14 LOAD_FAST 0 (alist) 16 PRECALL 1 20 CALL 1 30 RETURN_VALUE (The “2” is a line number).
Command-line interface ¶ The dis module can be invoked as a script from the command line: python -m dis [ -h ] [ -C ] [ infile ] The following options are accepted: -h , --help ¶ Display usage and exit. -C , --show-caches ¶ Show inline caches. If infile is specified, its disassembled code will be written to stdout. Otherwise, disassembly is performed on compiled source code recieved from stdin. Bytecode analysis ¶ New in version 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 ) ¶ 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 a Bytecode instance yields the bytecode operations as Instruction instances. 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 means dis() 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. classmethod from_traceback ( tb , * , show_caches = False ) ¶ Construct a Bytecode instance 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() .
Changed in version 3.7: This can now handle coroutine and asynchronous generator objects. Changed in version 3.11: Added the show_caches and adaptive parameters. Example: >>> bytecode = dis . Bytecode ( myfunc ) >>> for instr in bytecode : ... print ( instr . opname ) ... RESUME LOAD_GLOBAL LOAD_FAST PRECALL CALL RETURN_VALUE Analysis functions ¶ 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.
New in version 3.2. Changed in version 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.stdout if file is not specified). This is a convenient shorthand for print(code_info(x), file=file) , intended for interactive exploration at the interpreter prompt.
New in version 3.2. Changed in version 3.4: Added file parameter. dis. dis ( x = None , * , file = None , depth = None , show_caches = False , adaptive = 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 (the code of comprehensions, generator expressions and nested functions, and the code used for building nested classes). 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.stdout otherwise. The maximal depth of recursion is limited by depth unless it is None . depth=0 means 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.
Changed in version 3.4: Added file parameter. Changed in version 3.7: Implemented recursive disassembling and added depth parameter. Changed in version 3.7: This can now handle coroutine and asynchronous generator objects. Changed in version 3.11: Added the show_caches and adaptive parameters. dis. distb ( tb = None , * , file = None , show_caches = False , adaptive = 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.stdout otherwise. Changed in version 3.4: Added file parameter. Changed in version 3.11: Added the show_caches and adaptive parameters. dis. disassemble ( code , lasti = -1 , * , file = None , show_caches = False , adaptive = False ) ¶ dis. disco ( code , lasti = -1 , * , file = None , show_caches = False , adaptive = False ) ¶ Disassemble a code object, indicating the last instruction if lasti was provided.
The output is divided in the following columns: the line number, for the first instruction of each line the curre
The rest of this page is on the live site.