| The :mod:`dis` module defines the following functions and constants: |
| |
| |
| .. function:: dis([bytesource]) |
| |
| 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 |
n | sequence, it prints one line per byte code instruction. If no object is |
n | sequence, it prints one line per bytecode instruction. If no object is |
| provided, it disassembles the last traceback. |
| |
| |
| .. function:: distb([tb]) |
| |
| Disassembles the top-of-stack function of a traceback, using the last traceback |
| if none was passed. The instruction causing the exception is indicated. |
| |
| |
| .. function:: disassemble(code[, lasti]) |
| |
| Disassembles a code object, indicating the last instruction if *lasti* was |
| provided. The output is divided in the following columns: |
| |
n | #. the line number, for the first instruction of each line |
n | #. the line number, for the first instruction of each line |
| |
| #. the current instruction, indicated as ``-->``, |
| #. the current instruction, indicated as ``-->``, |
| |
| #. a labelled instruction, indicated with ``>>``, |
| #. a labelled instruction, indicated with ``>>``, |
| |
| #. the address of the instruction, |
| #. the address of the instruction, |
| |
| #. the operation code name, |
| #. the operation code name, |
| |
| #. operation parameters, and |
| #. operation parameters, and |
| |
| #. interpretation of the parameters in parentheses. |
| #. interpretation of the parameters in parentheses. |
| |
| The parameter interpretation recognizes local and global variable names, |
| constant values, branch targets, and compare operators. |
| |
| |
| .. function:: disco(code[, lasti]) |
| |
| A synonym for disassemble. It is more convenient to type, and kept for |
| compatibility with earlier Python releases. |
| |
| |
| .. data:: opname |
| |
n | Sequence of operation names, indexable using the byte code. |
n | Sequence of operation names, indexable using the bytecode. |
| |
| |
| .. data:: opmap |
| |
n | Dictionary mapping byte codes to operation names. |
n | Dictionary mapping bytecodes to operation names. |
| |
| |
| .. data:: cmp_op |
| |
| Sequence of all compare operation names. |
| |
| |
| .. data:: hasconst |
| |
n | Sequence of byte codes that have a constant parameter. |
n | Sequence of bytecodes that have a constant parameter. |
| |
| |
| .. data:: hasfree |
| |
n | Sequence of byte codes that access a free variable. |
n | Sequence of bytecodes that access a free variable. |
| |
| |
| .. data:: hasname |
| |
n | Sequence of byte codes that access an attribute by name. |
n | Sequence of bytecodes that access an attribute by name. |
| |
| |
| .. data:: hasjrel |
| |
n | Sequence of byte codes that have a relative jump target. |
n | Sequence of bytecodes that have a relative jump target. |
| |
| |
| .. data:: hasjabs |
| |
n | Sequence of byte codes that have an absolute jump target. |
n | Sequence of bytecodes that have an absolute jump target. |
| |
| |
| .. data:: haslocal |
| |
n | Sequence of byte codes that access a local variable. |
n | Sequence of bytecodes that access a local variable. |
| |
| |
| .. data:: hascompare |
| |
n | Sequence of byte codes of Boolean operations. |
n | Sequence of bytecodes of Boolean operations. |
| |
| |
| .. _bytecodes: |
| |
n | Python Byte Code Instructions |
n | Python Bytecode Instructions |
| ----------------------------- |
| ---------------------------- |
| |
n | The Python compiler currently generates the following byte code instructions. |
n | The Python compiler currently generates the following bytecode instructions. |
| |
| |
| .. opcode:: STOP_CODE () |
| |
| Indicates end-of-code to the compiler, not used by the interpreter. |
| |
| |
| .. opcode:: NOP () |
| with the outer-next block. |
| |
| |
| .. opcode:: BUILD_CLASS () |
| |
| Creates a new class object. TOS is the methods dictionary, TOS1 the tuple of |
| the names of the base classes, and TOS2 the class name. |
| |
n | |
| .. opcode:: WITH_CLEANUP () |
| |
| Cleans up the stack when a :keyword:`with` statement block exits. On top of |
| the stack are 1--3 values indicating how/why the finally clause was entered: |
| |
| * TOP = ``None`` |
| * (TOP, SECOND) = (``WHY_{RETURN,CONTINUE}``), retval |
| * TOP = ``WHY_*``; no retval below it |
| * (TOP, SECOND, THIRD) = exc_info() |
| |
| Under them is EXIT, the context manager's :meth:`__exit__` bound method. |
| |
| In the last case, ``EXIT(TOP, SECOND, THIRD)`` is called, otherwise |
| ``EXIT(None, None, None)``. |
| |
| EXIT is removed from the stack, leaving the values above it in the same |
| order. In addition, if the stack represents an exception, *and* the function |
| call returns a 'true' value, this information is "zapped", to prevent |
| ``END_FINALLY`` from re-raising the exception. (But non-local gotos should |
| still be resumed.) |
| |
| .. XXX explain the WHY stuff! |
| |
| |
| All of the following opcodes expect arguments. An argument is two bytes, with |
| the more significant byte last. |
| |
n | |
| .. opcode:: STORE_NAME (namei) |
| |
| Implements ``name = TOS``. *namei* is the index of *name* in the attribute |
n | :attr:`co_names` of the code object. The compiler tries to use ``STORE_LOCAL`` |
n | :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST`` |
| or ``STORE_GLOBAL`` if possible. |
| |
| |
| .. opcode:: DELETE_NAME (namei) |
| |
| Implements ``del name``, where *namei* is the index into :attr:`co_names` |
| attribute of the code object. |
| |
| |
| .. opcode:: UNPACK_SEQUENCE (count) |
| |
n | Unpacks TOS into *count* individual values, which are put onto the stack right- |
n | Unpacks TOS into *count* individual values, which are put onto the stack |
| to-left. |
| right-to-left. |
| |
| .. % \begin{opcodedesc}{UNPACK_LIST}{count} |
| .. % This opcode is obsolete. |
| .. % \end{opcodedesc} |
| .. % \begin{opcodedesc}{UNPACK_ARG}{count} |
| .. % This opcode is obsolete. |
| .. % \end{opcodedesc} |
| |
| |
| .. opcode:: DUP_TOPX (count) |
| |
| Duplicate *count* items, keeping them in the same order. Due to implementation |
| limits, *count* should be between 1 and 5 inclusive. |
| |
| |
| tuple onto the stack. |
| |
| |
| .. opcode:: BUILD_LIST (count) |
| |
| Works as ``BUILD_TUPLE``, but creates a list. |
| |
| |
n | .. opcode:: BUILD_MAP (zero) |
n | .. opcode:: BUILD_MAP (count) |
| |
n | Pushes a new empty dictionary object onto the stack. The argument is ignored |
n | Pushes a new dictionary object onto the stack. The dictionary is pre-sized |
| and set to zero by the compiler. |
| to hold *count* entries. |
| |
| |
| .. opcode:: LOAD_ATTR (namei) |
| |
| Replaces TOS with ``getattr(TOS, co_names[namei])``. |
| |
| |
| .. opcode:: COMPARE_OP (opname) |
| |
| Performs a Boolean operation. The operation name can be found in |
| ``cmp_op[opname]``. |
| |
| |
| .. opcode:: IMPORT_NAME (namei) |
| |
n | Imports the module ``co_names[namei]``. The module object is pushed onto the |
n | Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide |
| stack. The current namespace is not affected: for a proper import statement, a |
| the *fromlist* and *level* arguments of :func:`__import__`. The module |
| subsequent ``STORE_FAST`` instruction modifies the namespace. |
| 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. |
| |
| |
| .. opcode:: IMPORT_FROM (namei) |
| |
| Loads the attribute ``co_names[namei]`` from the module found in TOS. The |
| resulting object is pushed onto the stack, to be subsequently stored by a |
| ``STORE_FAST`` instruction. |
| |
| |
| .. opcode:: JUMP_FORWARD (delta) |
| |
n | Increments byte code counter by *delta*. |
n | Increments bytecode counter by *delta*. |
| |
| |
| .. opcode:: JUMP_IF_TRUE (delta) |
| |
n | If TOS is true, increment the byte code counter by *delta*. TOS is left on the |
n | If TOS is true, increment the bytecode counter by *delta*. TOS is left on the |
| stack. |
| |
| |
| .. opcode:: JUMP_IF_FALSE (delta) |
| |
n | If TOS is false, increment the byte code counter by *delta*. TOS is not |
n | If TOS is false, increment the bytecode counter by *delta*. TOS is not |
| changed. |
| |
| |
| .. opcode:: JUMP_ABSOLUTE (target) |
| |
n | Set byte code counter to *target*. |
n | Set bytecode counter to *target*. |
| |
| |
| .. opcode:: FOR_ITER (delta) |
| |
n | ``TOS`` is an iterator. Call its :meth:`next` method. If this yields a new |
n | ``TOS`` is an :term:`iterator`. Call its :meth:`next` method. If this |
| value, push it on the stack (leaving the iterator below it). If the iterator |
| yields a new value, push it on the stack (leaving the iterator below it). If |
| indicates it is exhausted ``TOS`` is popped, and the byte code counter is |
| the iterator indicates it is exhausted ``TOS`` is popped, and the bytecode |
| incremented by *delta*. |
| counter is incremented by *delta*. |
| |
| .. % \begin{opcodedesc}{FOR_LOOP}{delta} |
| .. % This opcode is obsolete. |
| .. % \end{opcodedesc} |
| .. % \begin{opcodedesc}{LOAD_LOCAL}{namei} |
| .. % This opcode is obsolete. |
| .. % \end{opcodedesc} |
| |
| |
| .. opcode:: LOAD_GLOBAL (namei) |
| |
| Loads the global named ``co_names[namei]`` onto the stack. |
n | |
| .. % \begin{opcodedesc}{SET_FUNC_ARGS}{argc} |
| .. % This opcode is obsolete. |
| .. % \end{opcodedesc} |
| |
| |
| .. opcode:: SETUP_LOOP (delta) |
| |
| Pushes a block for a loop onto the block stack. The block spans from the |
| current instruction with a size of *delta* bytes. |
| |
| |
| |
| .. opcode:: CALL_FUNCTION (argc) |
| |
| 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, |
n | the function object to call is on the stack. |
n | 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. |
| |
| |
| .. opcode:: MAKE_FUNCTION (argc) |
| |
| Pushes a new function object on the stack. TOS is the code associated with the |
| function. The function object is defined to have *argc* default parameters, |
| which are found below TOS. |
| |
| |
| .. opcode:: MAKE_CLOSURE (argc) |
| |
| Creates a new function object, sets its *func_closure* slot, and pushes it on |
n | the stack. TOS is the code associated with the function. If the code object has |
n | the stack. TOS is the code associated with the function, TOS1 the tuple |
| N free variables, the next N items on the stack are the cells for these |
| containing cells for the closure's free variables. The function also has |
| variables. The function also has *argc* default parameters, where are found |
| *argc* default parameters, which are found below the cells. |
| before the cells. |
| |
| |
| .. opcode:: BUILD_SLICE (argc) |
| |
| .. index:: builtin: slice |
| |
| Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2, |
| ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is |
t | pushed. See the ``slice()`` built-in function for more information. |
t | pushed. See the :func:`slice` built-in function for more information. |
| |
| |
| .. opcode:: EXTENDED_ARG (ext) |
| |
| 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. |