2 :mod:`dis` --- Disassembler for Python bytecode
3 ===============================================
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
15 Example: Given the function :func:`myfunc`::
20 the following command can be used to get the disassembly of :func:`myfunc`::
23 2 0 LOAD_GLOBAL 0 (len)
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.
86 Sequence of operation names, indexable using the bytecode.
91 Dictionary mapping bytecodes to operation names.
96 Sequence of all compare operation names.
101 Sequence of bytecodes that have a constant parameter.
106 Sequence of bytecodes that access a free variable.
111 Sequence of bytecodes that access an attribute by name.
116 Sequence of bytecodes that have a relative jump target.
121 Sequence of bytecodes that have an absolute jump target.
126 Sequence of bytecodes that access a local variable.
131 Sequence of bytecodes of Boolean operations.
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.
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
168 .. opcode:: ROT_FOUR ()
170 Lifts second, third and forth stack item one position up, moves top down to
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
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
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)
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:: SETUP_WITH (delta)
537 This opcode performs several operations before a with block starts. First,
538 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
539 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
540 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
541 is pushed. Finally, the result of calling the enter method is pushed onto
542 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
543 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
544 :opcode:`UNPACK_SEQUENCE`).
547 .. opcode:: WITH_CLEANUP ()
549 Cleans up the stack when a :keyword:`with` statement block exits. On top of
550 the stack are 1--3 values indicating how/why the finally clause was entered:
553 * (TOP, SECOND) = (``WHY_{RETURN,CONTINUE}``), retval
554 * TOP = ``WHY_*``; no retval below it
555 * (TOP, SECOND, THIRD) = exc_info()
557 Under them is EXIT, the context manager's :meth:`__exit__` bound method.
559 In the last case, ``EXIT(TOP, SECOND, THIRD)`` is called, otherwise
560 ``EXIT(None, None, None)``.
562 EXIT is removed from the stack, leaving the values above it in the same
563 order. In addition, if the stack represents an exception, *and* the function
564 call returns a 'true' value, this information is "zapped", to prevent
565 ``END_FINALLY`` from re-raising the exception. (But non-local gotos should
568 .. XXX explain the WHY stuff!
571 All of the following opcodes expect arguments. An argument is two bytes, with
572 the more significant byte last.
574 .. opcode:: STORE_NAME (namei)
576 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
577 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
578 or ``STORE_GLOBAL`` if possible.
581 .. opcode:: DELETE_NAME (namei)
583 Implements ``del name``, where *namei* is the index into :attr:`co_names`
584 attribute of the code object.
587 .. opcode:: UNPACK_SEQUENCE (count)
589 Unpacks TOS into *count* individual values, which are put onto the stack
593 .. opcode:: DUP_TOPX (count)
595 Duplicate *count* items, keeping them in the same order. Due to implementation
596 limits, *count* should be between 1 and 5 inclusive.
599 .. opcode:: STORE_ATTR (namei)
601 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
605 .. opcode:: DELETE_ATTR (namei)
607 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
610 .. opcode:: STORE_GLOBAL (namei)
612 Works as ``STORE_NAME``, but stores the name as a global.
615 .. opcode:: DELETE_GLOBAL (namei)
617 Works as ``DELETE_NAME``, but deletes a global name.
620 .. opcode:: LOAD_CONST (consti)
622 Pushes ``co_consts[consti]`` onto the stack.
625 .. opcode:: LOAD_NAME (namei)
627 Pushes the value associated with ``co_names[namei]`` onto the stack.
630 .. opcode:: BUILD_TUPLE (count)
632 Creates a tuple consuming *count* items from the stack, and pushes the resulting
633 tuple onto the stack.
636 .. opcode:: BUILD_LIST (count)
638 Works as ``BUILD_TUPLE``, but creates a list.
641 .. opcode:: BUILD_MAP (count)
643 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
644 to hold *count* entries.
647 .. opcode:: LOAD_ATTR (namei)
649 Replaces TOS with ``getattr(TOS, co_names[namei])``.
652 .. opcode:: COMPARE_OP (opname)
654 Performs a Boolean operation. The operation name can be found in
658 .. opcode:: IMPORT_NAME (namei)
660 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
661 the *fromlist* and *level* arguments of :func:`__import__`. The module
662 object is pushed onto the stack. The current namespace is not affected:
663 for a proper import statement, a subsequent ``STORE_FAST`` instruction
664 modifies the namespace.
667 .. opcode:: IMPORT_FROM (namei)
669 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
670 resulting object is pushed onto the stack, to be subsequently stored by a
671 ``STORE_FAST`` instruction.
674 .. opcode:: JUMP_FORWARD (delta)
676 Increments bytecode counter by *delta*.
679 .. opcode:: POP_JUMP_IF_TRUE (target)
681 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
684 .. opcode:: POP_JUMP_IF_FALSE (target)
686 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
689 .. opcode:: JUMP_IF_TRUE_OR_POP (target)
691 If TOS is true, sets the bytecode counter to *target* and leaves TOS
692 on the stack. Otherwise (TOS is false), TOS is popped.
695 .. opcode:: JUMP_IF_FALSE_OR_POP (target)
697 If TOS is false, sets the bytecode counter to *target* and leaves
698 TOS on the stack. Otherwise (TOS is true), TOS is popped.
701 .. opcode:: JUMP_ABSOLUTE (target)
703 Set bytecode counter to *target*.
706 .. opcode:: FOR_ITER (delta)
708 ``TOS`` is an :term:`iterator`. Call its :meth:`!next` method. If this
709 yields a new value, push it on the stack (leaving the iterator below it). If
710 the iterator indicates it is exhausted ``TOS`` is popped, and the bytecode
711 counter is incremented by *delta*.
714 .. opcode:: LOAD_GLOBAL (namei)
716 Loads the global named ``co_names[namei]`` onto the stack.
719 .. opcode:: SETUP_LOOP (delta)
721 Pushes a block for a loop onto the block stack. The block spans from the
722 current instruction with a size of *delta* bytes.
725 .. opcode:: SETUP_EXCEPT (delta)
727 Pushes a try block from a try-except clause onto the block stack. *delta* points
728 to the first except block.
731 .. opcode:: SETUP_FINALLY (delta)
733 Pushes a try block from a try-except clause onto the block stack. *delta* points
734 to the finally block.
736 .. opcode:: STORE_MAP ()
738 Store a key and value pair in a dictionary. Pops the key and value while leaving
739 the dictionary on the stack.
741 .. opcode:: LOAD_FAST (var_num)
743 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
746 .. opcode:: STORE_FAST (var_num)
748 Stores TOS into the local ``co_varnames[var_num]``.
751 .. opcode:: DELETE_FAST (var_num)
753 Deletes local ``co_varnames[var_num]``.
756 .. opcode:: LOAD_CLOSURE (i)
758 Pushes a reference to the cell contained in slot *i* of the cell and free
759 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
760 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
764 .. opcode:: LOAD_DEREF (i)
766 Loads the cell contained in slot *i* of the cell and free variable storage.
767 Pushes a reference to the object the cell contains on the stack.
770 .. opcode:: STORE_DEREF (i)
772 Stores TOS into the cell contained in slot *i* of the cell and free variable
776 .. opcode:: SET_LINENO (lineno)
778 This opcode is obsolete.
781 .. opcode:: RAISE_VARARGS (argc)
783 Raises an exception. *argc* indicates the number of parameters to the raise
784 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
785 the parameter as TOS1, and the exception as TOS.
788 .. opcode:: CALL_FUNCTION (argc)
790 Calls a function. The low byte of *argc* indicates the number of positional
791 parameters, the high byte the number of keyword parameters. On the stack, the
792 opcode finds the keyword parameters first. For each keyword argument, the value
793 is on top of the key. Below the keyword parameters, the positional parameters
794 are on the stack, with the right-most parameter on top. Below the parameters,
795 the function object to call is on the stack. Pops all function arguments, and
796 the function itself off the stack, and pushes the return value.
799 .. opcode:: MAKE_FUNCTION (argc)
801 Pushes a new function object on the stack. TOS is the code associated with the
802 function. The function object is defined to have *argc* default parameters,
803 which are found below TOS.
806 .. opcode:: MAKE_CLOSURE (argc)
808 Creates a new function object, sets its *func_closure* slot, and pushes it on
809 the stack. TOS is the code associated with the function, TOS1 the tuple
810 containing cells for the closure's free variables. The function also has
811 *argc* default parameters, which are found below the cells.
814 .. opcode:: BUILD_SLICE (argc)
816 .. index:: builtin: slice
818 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
819 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
820 pushed. See the :func:`slice` built-in function for more information.
823 .. opcode:: EXTENDED_ARG (ext)
825 Prefixes any opcode which has an argument too big to fit into the default two
826 bytes. *ext* holds two additional bytes which, taken together with the
827 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
828 most-significant bytes.
831 .. opcode:: CALL_FUNCTION_VAR (argc)
833 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
834 on the stack contains the variable argument list, followed by keyword and
835 positional arguments.
838 .. opcode:: CALL_FUNCTION_KW (argc)
840 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
841 on the stack contains the keyword arguments dictionary, followed by explicit
842 keyword and positional arguments.
845 .. opcode:: CALL_FUNCTION_VAR_KW (argc)
847 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
848 element on the stack contains the keyword arguments dictionary, followed by the
849 variable-arguments tuple, followed by explicit keyword and positional arguments.
852 .. opcode:: HAVE_ARGUMENT ()
854 This is not really an opcode. It identifies the dividing line between opcodes
855 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=