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.
Example: Given the function myfunc():
def myfunc(alist):
return len(alist)
the following command can be used to get the disassembly of myfunc():
>>> dis.dis(myfunc)
2 0 LOAD_GLOBAL 0 (len)
3 LOAD_FAST 0 (alist)
6 CALL_FUNCTION 1
9 RETURN_VALUE
(The “2” is a line number).
The dis module defines the following functions and constants:
Disassemble the bytesource object. bytesource can denote either a module, a class, a method, a function, or a code object. For a module, it disassembles all functions. For a class, it disassembles all methods. For a single code sequence, it prints one line per bytecode instruction. If no object is provided, it disassembles the last traceback.
Disassembles the top-of-stack function of a traceback, using the last traceback if none was passed. The instruction causing the exception is indicated.
Disassembles a code object, indicating the last instruction if lasti was provided. The output is divided in the following columns:
The parameter interpretation recognizes local and global variable names, constant values, branch targets, and compare operators.
A synonym for disassemble. It is more convenient to type, and kept for compatibility with earlier Python releases.
Sequence of operation names, indexable using the bytecode.
Dictionary mapping bytecodes to operation names.
Sequence of all compare operation names.
Sequence of bytecodes that have a constant parameter.
Sequence of bytecodes that access a free variable.
Sequence of bytecodes that access an attribute by name.
Sequence of bytecodes that have a relative jump target.
Sequence of bytecodes that have an absolute jump target.
Sequence of bytecodes that access a local variable.
Sequence of bytecodes of Boolean operations.
The Python compiler currently generates the following bytecode instructions.
Unary Operations take the top of the stack, apply the operation, and push the result back on the stack.
Binary operations remove the top of the stack (TOS) and the second top-most stack item (TOS1) from the stack. They perform the operation, and put the result back on the stack.
In-place operations are like binary operations, in that they remove TOS and TOS1, and push the result back on the stack, but the operation is done in-place when TOS1 supports it, and the resulting TOS may be (but does not have to be) the original TOS1.
The slice opcodes take up to three parameters.
Slice assignment needs even an additional parameter. As any statement, they put nothing on the stack.
Miscellaneous opcodes.
All of the following opcodes expect arguments. An argument is two bytes, with the more significant byte last.