Navigation

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.

Example: Given the function myfunc():

defmyfunc(alist):returnlen(alist)

the following command can be used to display the disassembly of
myfunc():

Analyse the bytecode corresponding to a function, 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.

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:

Disassemble the x object. x can denote either a module, a class, a
method, a function, 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. For a code object or sequence of raw bytecode,
it prints one line per bytecode instruction. 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.

Return an iterator over the instructions in the supplied function, method,
source code string or code object.

The iterator generates a series of Instruction named tuples giving
the details of each operation in the supplied code.

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.

This generator function uses the co_firstlineno and co_lnotab
attributes of the code object code to find the offsets which are starts of
lines in the source code. They are generated as (offset,lineno) pairs.

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.

For all of the SET_ADD, LIST_APPEND and MAP_ADD
instructions, while the added value or key/value pair is popped off, the
container object remains on the stack so that it is available for further
iterations of the loop.

Removes one block from the block stack. The popped block must be an exception
handler block, as implicitly created when entering an except handler. In
addition to popping extraneous values from the frame stack, the last three
popped values are used to restore the exception state.

This opcode performs several operations before a with block starts. First,
it loads __exit__() from the context manager and pushes it onto
the stack for later use by WITH_CLEANUP. Then,
__enter__() is called, and a finally block pointing to delta
is pushed. Finally, the result of calling the enter method is pushed onto
the stack. The next opcode will either ignore it (POP_TOP), or
store it in (a) variable(s) (STORE_FAST, STORE_NAME, or
UNPACK_SEQUENCE).

Cleans up the stack when a with statement block exits. TOS is the
context manager’s __exit__() bound method. Below TOS are 1–3 values
indicating how/why the finally clause was entered:

SECOND = None

(SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval

SECOND = WHY_*; no retval below it

(SECOND, THIRD, FOURTH) = exc_info()

In the last case, TOS(SECOND,THIRD,FOURTH) is called, otherwise
TOS(None,None,None). In addition, TOS is removed from the stack.

If the stack represents an exception, and the function call returns a
‘true’ value, this information is “zapped” and replaced with a single
WHY_SILENCED to prevent END_FINALLY from re-raising the
exception. (But non-local gotos will still be resumed.)

All of the following opcodes expect arguments. An argument is two bytes, with
the more significant byte last.

Implements assignment with a starred target: Unpacks an iterable in TOS into
individual values, where the total number of values can be smaller than the
number of items in the iterable: one the new values will be a list of all
leftover items.

The low byte of counts is the number of values before the list value, the
high byte of counts the number of values after it. The resulting values
are put onto the stack right-to-left.

Imports the module co_names[namei]. TOS and TOS1 are popped and provide
the fromlist and level arguments of __import__(). The module
object is pushed onto the stack. The current namespace is not affected: for
a proper import statement, a subsequent STORE_FAST instruction
modifies the namespace.

TOS is an iterator. Call its __next__() method. If
this yields a new value, push it on the stack (leaving the iterator below
it). If the iterator indicates it is exhausted TOS is popped, and the byte
code counter is incremented by delta.

Pushes a reference to the cell contained in slot i of the cell and free
variable storage. The name of the variable is co_cellvars[i] if i is
less than the length of co_cellvars. Otherwise it is co_freevars[i-len(co_cellvars)].

Calls a function. The low byte of argc indicates the number of positional
parameters, the high byte the number of keyword parameters. On the stack, the
opcode finds the keyword parameters first. For each keyword argument, the
value is on top of the key. Below the keyword parameters, the positional
parameters are on the stack, with the right-most parameter on top. Below the
parameters, the function object to call is on the stack. Pops all function
arguments, and the function itself off the stack, and pushes the return
value.

Creates a new function object, sets its __closure__ slot, and pushes it on
the stack. TOS is the qualified name of the function, TOS1 is the
code associated with the function, and TOS2 is the tuple containing cells for
the closure’s free variables. The function also has argc default
parameters, which are found below the cells.

Prefixes any opcode which has an argument too big to fit into the default two
bytes. ext holds two additional bytes which, taken together with the
subsequent opcode’s argument, comprise a four-byte argument, ext being the
two most-significant bytes.

Calls a function. argc is interpreted as in CALL_FUNCTION. The
top element on the stack contains the keyword arguments dictionary, followed
by the variable-arguments tuple, followed by explicit keyword and positional
arguments.

Sequence of bytecodes that access a free variable (note that ‘free’ in this
context refers to names in the current scope that are referenced by inner
scopes or names in outer scopes that are referenced from this scope. It does
not include references to global or builtin scopes).