Oops. Need to check not only that HAVE_DECL_ISINF is defined, but also
[python.git] / Doc / library / dis.rst
blob3dd528b23df015fd81c82a18ba18e691dae8f2c1
2 :mod:`dis` --- Disassembler for Python bytecode
3 ===============================================
5 .. module:: dis
6    :synopsis: Disassembler for Python bytecode.
9 The :mod:`dis` module supports the analysis of Python :term:`bytecode` by disassembling
10 it.  Since there is no Python assembler, this module defines the Python assembly
11 language.  The Python bytecode which this module takes as an input is defined
12 in the file  :file:`Include/opcode.h` and used by the compiler and the
13 interpreter.
15 Example: Given the function :func:`myfunc`::
17    def myfunc(alist):
18        return len(alist)
20 the following command can be used to get the disassembly of :func:`myfunc`::
22    >>> dis.dis(myfunc)
23      2           0 LOAD_GLOBAL              0 (len)
24                  3 LOAD_FAST                0 (alist)
25                  6 CALL_FUNCTION            1
26                  9 RETURN_VALUE
28 (The "2" is a line number).
30 The :mod:`dis` module defines the following functions and constants:
33 .. function:: dis([bytesource])
35    Disassemble the *bytesource* object. *bytesource* can denote either a module, a
36    class, a method, a function, or a code object.   For a module, it disassembles
37    all functions.  For a class, it disassembles all methods.  For a single code
38    sequence, it prints one line per bytecode instruction.  If no object is
39    provided, it disassembles the last traceback.
42 .. function:: distb([tb])
44    Disassembles the top-of-stack function of a traceback, using the last traceback
45    if none was passed.  The instruction causing the exception is indicated.
48 .. function:: disassemble(code[, lasti])
50    Disassembles a code object, indicating the last instruction if *lasti* was
51    provided.  The output is divided in the following columns:
53    #. the line number, for the first instruction of each line
54    #. the current instruction, indicated as ``-->``,
55    #. a labelled instruction, indicated with ``>>``,
56    #. the address of the instruction,
57    #. the operation code name,
58    #. operation parameters, and
59    #. interpretation of the parameters in parentheses.
61    The parameter interpretation recognizes local and global variable names,
62    constant values, branch targets, and compare operators.
65 .. function:: disco(code[, lasti])
67    A synonym for :func:`disassemble`.  It is more convenient to type, and kept
68    for compatibility with earlier Python releases.
71 .. function:: findlinestarts(code)
73    This generator function uses the ``co_firstlineno`` and ``co_lnotab``
74    attributes of the code object *code* to find the offsets which are starts of
75    lines in the source code.  They are generated as ``(offset, lineno)`` pairs.
78 .. function:: findlabels(code)
80    Detect all offsets in the code object *code* which are jump targets, and
81    return a list of these offsets.
84 .. data:: opname
86    Sequence of operation names, indexable using the bytecode.
89 .. data:: opmap
91    Dictionary mapping bytecodes to operation names.
94 .. data:: cmp_op
96    Sequence of all compare operation names.
99 .. data:: hasconst
101    Sequence of bytecodes that have a constant parameter.
104 .. data:: hasfree
106    Sequence of bytecodes that access a free variable.
109 .. data:: hasname
111    Sequence of bytecodes that access an attribute by name.
114 .. data:: hasjrel
116    Sequence of bytecodes that have a relative jump target.
119 .. data:: hasjabs
121    Sequence of bytecodes that have an absolute jump target.
124 .. data:: haslocal
126    Sequence of bytecodes that access a local variable.
129 .. data:: hascompare
131    Sequence of bytecodes of Boolean operations.
134 .. _bytecodes:
136 Python Bytecode Instructions
137 ----------------------------
139 The Python compiler currently generates the following bytecode instructions.
142 .. opcode:: STOP_CODE ()
144    Indicates end-of-code to the compiler, not used by the interpreter.
147 .. opcode:: NOP ()
149    Do nothing code.  Used as a placeholder by the bytecode optimizer.
152 .. opcode:: POP_TOP ()
154    Removes the top-of-stack (TOS) item.
157 .. opcode:: ROT_TWO ()
159    Swaps the two top-most stack items.
162 .. opcode:: ROT_THREE ()
164    Lifts second and third stack item one position up, moves top down to position
165    three.
168 .. opcode:: ROT_FOUR ()
170    Lifts second, third and forth stack item one position up, moves top down to
171    position four.
174 .. opcode:: DUP_TOP ()
176    Duplicates the reference on top of the stack.
178 Unary Operations take the top of the stack, apply the operation, and push the
179 result back on the stack.
182 .. opcode:: UNARY_POSITIVE ()
184    Implements ``TOS = +TOS``.
187 .. opcode:: UNARY_NEGATIVE ()
189    Implements ``TOS = -TOS``.
192 .. opcode:: UNARY_NOT ()
194    Implements ``TOS = not TOS``.
197 .. opcode:: UNARY_CONVERT ()
199    Implements ``TOS = `TOS```.
202 .. opcode:: UNARY_INVERT ()
204    Implements ``TOS = ~TOS``.
207 .. opcode:: GET_ITER ()
209    Implements ``TOS = iter(TOS)``.
211 Binary operations remove the top of the stack (TOS) and the second top-most
212 stack item (TOS1) from the stack.  They perform the operation, and put the
213 result back on the stack.
216 .. opcode:: BINARY_POWER ()
218    Implements ``TOS = TOS1 ** TOS``.
221 .. opcode:: BINARY_MULTIPLY ()
223    Implements ``TOS = TOS1 * TOS``.
226 .. opcode:: BINARY_DIVIDE ()
228    Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is not
229    in effect.
232 .. opcode:: BINARY_FLOOR_DIVIDE ()
234    Implements ``TOS = TOS1 // TOS``.
237 .. opcode:: BINARY_TRUE_DIVIDE ()
239    Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is in
240    effect.
243 .. opcode:: BINARY_MODULO ()
245    Implements ``TOS = TOS1 % TOS``.
248 .. opcode:: BINARY_ADD ()
250    Implements ``TOS = TOS1 + TOS``.
253 .. opcode:: BINARY_SUBTRACT ()
255    Implements ``TOS = TOS1 - TOS``.
258 .. opcode:: BINARY_SUBSCR ()
260    Implements ``TOS = TOS1[TOS]``.
263 .. opcode:: BINARY_LSHIFT ()
265    Implements ``TOS = TOS1 << TOS``.
268 .. opcode:: BINARY_RSHIFT ()
270    Implements ``TOS = TOS1 >> TOS``.
273 .. opcode:: BINARY_AND ()
275    Implements ``TOS = TOS1 & TOS``.
278 .. opcode:: BINARY_XOR ()
280    Implements ``TOS = TOS1 ^ TOS``.
283 .. opcode:: BINARY_OR ()
285    Implements ``TOS = TOS1 | TOS``.
287 In-place operations are like binary operations, in that they remove TOS and
288 TOS1, and push the result back on the stack, but the operation is done in-place
289 when TOS1 supports it, and the resulting TOS may be (but does not have to be)
290 the original TOS1.
293 .. opcode:: INPLACE_POWER ()
295    Implements in-place ``TOS = TOS1 ** TOS``.
298 .. opcode:: INPLACE_MULTIPLY ()
300    Implements in-place ``TOS = TOS1 * TOS``.
303 .. opcode:: INPLACE_DIVIDE ()
305    Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
306    division`` is not in effect.
309 .. opcode:: INPLACE_FLOOR_DIVIDE ()
311    Implements in-place ``TOS = TOS1 // TOS``.
314 .. opcode:: INPLACE_TRUE_DIVIDE ()
316    Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
317    division`` is in effect.
320 .. opcode:: INPLACE_MODULO ()
322    Implements in-place ``TOS = TOS1 % TOS``.
325 .. opcode:: INPLACE_ADD ()
327    Implements in-place ``TOS = TOS1 + TOS``.
330 .. opcode:: INPLACE_SUBTRACT ()
332    Implements in-place ``TOS = TOS1 - TOS``.
335 .. opcode:: INPLACE_LSHIFT ()
337    Implements in-place ``TOS = TOS1 << TOS``.
340 .. opcode:: INPLACE_RSHIFT ()
342    Implements in-place ``TOS = TOS1 >> TOS``.
345 .. opcode:: INPLACE_AND ()
347    Implements in-place ``TOS = TOS1 & TOS``.
350 .. opcode:: INPLACE_XOR ()
352    Implements in-place ``TOS = TOS1 ^ TOS``.
355 .. opcode:: INPLACE_OR ()
357    Implements in-place ``TOS = TOS1 | TOS``.
359 The slice opcodes take up to three parameters.
362 .. opcode:: SLICE+0 ()
364    Implements ``TOS = TOS[:]``.
367 .. opcode:: SLICE+1 ()
369    Implements ``TOS = TOS1[TOS:]``.
372 .. opcode:: SLICE+2 ()
374    Implements ``TOS = TOS1[:TOS]``.
377 .. opcode:: SLICE+3 ()
379    Implements ``TOS = TOS2[TOS1:TOS]``.
381 Slice assignment needs even an additional parameter.  As any statement, they put
382 nothing on the stack.
385 .. opcode:: STORE_SLICE+0 ()
387    Implements ``TOS[:] = TOS1``.
390 .. opcode:: STORE_SLICE+1 ()
392    Implements ``TOS1[TOS:] = TOS2``.
395 .. opcode:: STORE_SLICE+2 ()
397    Implements ``TOS1[:TOS] = TOS2``.
400 .. opcode:: STORE_SLICE+3 ()
402    Implements ``TOS2[TOS1:TOS] = TOS3``.
405 .. opcode:: DELETE_SLICE+0 ()
407    Implements ``del TOS[:]``.
410 .. opcode:: DELETE_SLICE+1 ()
412    Implements ``del TOS1[TOS:]``.
415 .. opcode:: DELETE_SLICE+2 ()
417    Implements ``del TOS1[:TOS]``.
420 .. opcode:: DELETE_SLICE+3 ()
422    Implements ``del TOS2[TOS1:TOS]``.
425 .. opcode:: STORE_SUBSCR ()
427    Implements ``TOS1[TOS] = TOS2``.
430 .. opcode:: DELETE_SUBSCR ()
432    Implements ``del TOS1[TOS]``.
434 Miscellaneous opcodes.
437 .. opcode:: PRINT_EXPR ()
439    Implements the expression statement for the interactive mode.  TOS is removed
440    from the stack and printed.  In non-interactive mode, an expression statement is
441    terminated with ``POP_STACK``.
444 .. opcode:: PRINT_ITEM ()
446    Prints TOS to the file-like object bound to ``sys.stdout``.  There is one such
447    instruction for each item in the :keyword:`print` statement.
450 .. opcode:: PRINT_ITEM_TO ()
452    Like ``PRINT_ITEM``, but prints the item second from TOS to the file-like object
453    at TOS.  This is used by the extended print statement.
456 .. opcode:: PRINT_NEWLINE ()
458    Prints a new line on ``sys.stdout``.  This is generated as the last operation of
459    a :keyword:`print` statement, unless the statement ends with a comma.
462 .. opcode:: PRINT_NEWLINE_TO ()
464    Like ``PRINT_NEWLINE``, but prints the new line on the file-like object on the
465    TOS.  This is used by the extended print statement.
468 .. opcode:: BREAK_LOOP ()
470    Terminates a loop due to a :keyword:`break` statement.
473 .. opcode:: CONTINUE_LOOP (target)
475    Continues a loop due to a :keyword:`continue` statement.  *target* is the
476    address to jump to (which should be a ``FOR_ITER`` instruction).
479 .. opcode:: LIST_APPEND (i)
481    Calls ``list.append(TOS[-i], TOS)``.  Used to implement list comprehensions.
482    While the appended value is popped off, the list object remains on the
483    stack so that it is available for further iterations of the loop.
486 .. opcode:: LOAD_LOCALS ()
488    Pushes a reference to the locals of the current scope on the stack. This is used
489    in the code for a class definition: After the class body is evaluated, the
490    locals are passed to the class definition.
493 .. opcode:: RETURN_VALUE ()
495    Returns with TOS to the caller of the function.
498 .. opcode:: YIELD_VALUE ()
500    Pops ``TOS`` and yields it from a :term:`generator`.
503 .. opcode:: IMPORT_STAR ()
505    Loads all symbols not starting with ``'_'`` directly from the module TOS to the
506    local namespace. The module is popped after loading all names. This opcode
507    implements ``from module import *``.
510 .. opcode:: EXEC_STMT ()
512    Implements ``exec TOS2,TOS1,TOS``.  The compiler fills missing optional
513    parameters with ``None``.
516 .. opcode:: POP_BLOCK ()
518    Removes one block from the block stack.  Per frame, there is a  stack of blocks,
519    denoting nested loops, try statements, and such.
522 .. opcode:: END_FINALLY ()
524    Terminates a :keyword:`finally` clause.  The interpreter recalls whether the
525    exception has to be re-raised, or whether the function returns, and continues
526    with the outer-next block.
529 .. opcode:: BUILD_CLASS ()
531    Creates a new class object.  TOS is the methods dictionary, TOS1 the tuple of
532    the names of the base classes, and TOS2 the class name.
535 .. opcode:: WITH_CLEANUP ()
537    Cleans up the stack when a :keyword:`with` statement block exits.  On top of
538    the stack are 1--3 values indicating how/why the finally clause was entered:
540    * TOP = ``None``
541    * (TOP, SECOND) = (``WHY_{RETURN,CONTINUE}``), retval
542    * TOP = ``WHY_*``; no retval below it
543    * (TOP, SECOND, THIRD) = exc_info()
545    Under them is EXIT, the context manager's :meth:`__exit__` bound method.
547    In the last case, ``EXIT(TOP, SECOND, THIRD)`` is called, otherwise
548    ``EXIT(None, None, None)``.
550    EXIT is removed from the stack, leaving the values above it in the same
551    order. In addition, if the stack represents an exception, *and* the function
552    call returns a 'true' value, this information is "zapped", to prevent
553    ``END_FINALLY`` from re-raising the exception.  (But non-local gotos should
554    still be resumed.)
556    .. XXX explain the WHY stuff!
559 All of the following opcodes expect arguments.  An argument is two bytes, with
560 the more significant byte last.
562 .. opcode:: STORE_NAME (namei)
564    Implements ``name = TOS``. *namei* is the index of *name* in the attribute
565    :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
566    or ``STORE_GLOBAL`` if possible.
569 .. opcode:: DELETE_NAME (namei)
571    Implements ``del name``, where *namei* is the index into :attr:`co_names`
572    attribute of the code object.
575 .. opcode:: UNPACK_SEQUENCE (count)
577    Unpacks TOS into *count* individual values, which are put onto the stack
578    right-to-left.
581 .. opcode:: DUP_TOPX (count)
583    Duplicate *count* items, keeping them in the same order. Due to implementation
584    limits, *count* should be between 1 and 5 inclusive.
587 .. opcode:: STORE_ATTR (namei)
589    Implements ``TOS.name = TOS1``, where *namei* is the index of name in
590    :attr:`co_names`.
593 .. opcode:: DELETE_ATTR (namei)
595    Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
598 .. opcode:: STORE_GLOBAL (namei)
600    Works as ``STORE_NAME``, but stores the name as a global.
603 .. opcode:: DELETE_GLOBAL (namei)
605    Works as ``DELETE_NAME``, but deletes a global name.
608 .. opcode:: LOAD_CONST (consti)
610    Pushes ``co_consts[consti]`` onto the stack.
613 .. opcode:: LOAD_NAME (namei)
615    Pushes the value associated with ``co_names[namei]`` onto the stack.
618 .. opcode:: BUILD_TUPLE (count)
620    Creates a tuple consuming *count* items from the stack, and pushes the resulting
621    tuple onto the stack.
624 .. opcode:: BUILD_LIST (count)
626    Works as ``BUILD_TUPLE``, but creates a list.
629 .. opcode:: BUILD_MAP (count)
631    Pushes a new dictionary object onto the stack.  The dictionary is pre-sized
632    to hold *count* entries.
635 .. opcode:: LOAD_ATTR (namei)
637    Replaces TOS with ``getattr(TOS, co_names[namei])``.
640 .. opcode:: COMPARE_OP (opname)
642    Performs a Boolean operation.  The operation name can be found in
643    ``cmp_op[opname]``.
646 .. opcode:: IMPORT_NAME (namei)
648    Imports the module ``co_names[namei]``.  TOS and TOS1 are popped and provide
649    the *fromlist* and *level* arguments of :func:`__import__`.  The module
650    object is pushed onto the stack.  The current namespace is not affected:
651    for a proper import statement, a subsequent ``STORE_FAST`` instruction
652    modifies the namespace.
655 .. opcode:: IMPORT_FROM (namei)
657    Loads the attribute ``co_names[namei]`` from the module found in TOS. The
658    resulting object is pushed onto the stack, to be subsequently stored by a
659    ``STORE_FAST`` instruction.
662 .. opcode:: JUMP_FORWARD (delta)
664    Increments bytecode counter by *delta*.
667 .. opcode:: JUMP_IF_TRUE (delta)
669    If TOS is true, increment the bytecode counter by *delta*.  TOS is left on the
670    stack.
673 .. opcode:: JUMP_IF_FALSE (delta)
675    If TOS is false, increment the bytecode counter by *delta*.  TOS is not
676    changed.
679 .. opcode:: JUMP_ABSOLUTE (target)
681    Set bytecode counter to *target*.
684 .. opcode:: FOR_ITER (delta)
686    ``TOS`` is an :term:`iterator`.  Call its :meth:`next` method.  If this
687    yields a new value, push it on the stack (leaving the iterator below it).  If
688    the iterator indicates it is exhausted ``TOS`` is popped, and the bytecode
689    counter is incremented by *delta*.
692 .. opcode:: LOAD_GLOBAL (namei)
694    Loads the global named ``co_names[namei]`` onto the stack.
697 .. opcode:: SETUP_LOOP (delta)
699    Pushes a block for a loop onto the block stack.  The block spans from the
700    current instruction with a size of *delta* bytes.
703 .. opcode:: SETUP_EXCEPT (delta)
705    Pushes a try block from a try-except clause onto the block stack. *delta* points
706    to the first except block.
709 .. opcode:: SETUP_FINALLY (delta)
711    Pushes a try block from a try-except clause onto the block stack. *delta* points
712    to the finally block.
714 .. opcode:: STORE_MAP ()
716    Store a key and value pair in a dictionary.  Pops the key and value while leaving
717    the dictionary on the stack.
719 .. opcode:: LOAD_FAST (var_num)
721    Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
724 .. opcode:: STORE_FAST (var_num)
726    Stores TOS into the local ``co_varnames[var_num]``.
729 .. opcode:: DELETE_FAST (var_num)
731    Deletes local ``co_varnames[var_num]``.
734 .. opcode:: LOAD_CLOSURE (i)
736    Pushes a reference to the cell contained in slot *i* of the cell and free
737    variable storage.  The name of the variable is  ``co_cellvars[i]`` if *i* is
738    less than the length of *co_cellvars*.  Otherwise it is  ``co_freevars[i -
739    len(co_cellvars)]``.
742 .. opcode:: LOAD_DEREF (i)
744    Loads the cell contained in slot *i* of the cell and free variable storage.
745    Pushes a reference to the object the cell contains on the stack.
748 .. opcode:: STORE_DEREF (i)
750    Stores TOS into the cell contained in slot *i* of the cell and free variable
751    storage.
754 .. opcode:: SET_LINENO (lineno)
756    This opcode is obsolete.
759 .. opcode:: RAISE_VARARGS (argc)
761    Raises an exception. *argc* indicates the number of parameters to the raise
762    statement, ranging from 0 to 3.  The handler will find the traceback as TOS2,
763    the parameter as TOS1, and the exception as TOS.
766 .. opcode:: CALL_FUNCTION (argc)
768    Calls a function.  The low byte of *argc* indicates the number of positional
769    parameters, the high byte the number of keyword parameters. On the stack, the
770    opcode finds the keyword parameters first.  For each keyword argument, the value
771    is on top of the key.  Below the keyword parameters, the positional parameters
772    are on the stack, with the right-most parameter on top.  Below the parameters,
773    the function object to call is on the stack.  Pops all function arguments, and
774    the function itself off the stack, and pushes the return value.
777 .. opcode:: MAKE_FUNCTION (argc)
779    Pushes a new function object on the stack.  TOS is the code associated with the
780    function.  The function object is defined to have *argc* default parameters,
781    which are found below TOS.
784 .. opcode:: MAKE_CLOSURE (argc)
786    Creates a new function object, sets its *func_closure* slot, and pushes it on
787    the stack.  TOS is the code associated with the function, TOS1 the tuple
788    containing cells for the closure's free variables.  The function also has
789    *argc* default parameters, which are found below the cells.
792 .. opcode:: BUILD_SLICE (argc)
794    .. index:: builtin: slice
796    Pushes a slice object on the stack.  *argc* must be 2 or 3.  If it is 2,
797    ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
798    pushed. See the :func:`slice` built-in function for more information.
801 .. opcode:: EXTENDED_ARG (ext)
803    Prefixes any opcode which has an argument too big to fit into the default two
804    bytes.  *ext* holds two additional bytes which, taken together with the
805    subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
806    most-significant bytes.
809 .. opcode:: CALL_FUNCTION_VAR (argc)
811    Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
812    on the stack contains the variable argument list, followed by keyword and
813    positional arguments.
816 .. opcode:: CALL_FUNCTION_KW (argc)
818    Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
819    on the stack contains the keyword arguments dictionary,  followed by explicit
820    keyword and positional arguments.
823 .. opcode:: CALL_FUNCTION_VAR_KW (argc)
825    Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``.  The top
826    element on the stack contains the keyword arguments dictionary, followed by the
827    variable-arguments tuple, followed by explicit keyword and positional arguments.
830 .. opcode:: HAVE_ARGUMENT ()
832    This is not really an opcode.  It identifies the dividing line between opcodes
833    which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
834    HAVE_ARGUMENT``.