1
:mod:`dis` --- Disassembler for Python bytecode
2
===============================================
5
:synopsis: Disassembler for Python bytecode.
7
**Source code:** :source:`Lib/dis.py`
11
The :mod:`dis` module supports the analysis of CPython :term:`bytecode` by
12
disassembling it. The CPython bytecode which this module takes as an input is
13
defined in the file :file:`Include/opcode.h` and used by the compiler and the
18
Bytecode is an implementation detail of the CPython interpreter! No
19
guarantees are made that bytecode will not be added, removed, or changed
20
between versions of Python. Use of this module should not be considered to
21
work across Python VMs or Python releases.
23
Example: Given the function :func:`myfunc`::
28
the following command can be used to get the disassembly of :func:`myfunc`::
31
2 0 LOAD_GLOBAL 0 (len)
36
(The "2" is a line number).
38
The :mod:`dis` module defines the following functions and constants:
41
.. function:: dis([bytesource])
43
Disassemble the *bytesource* object. *bytesource* can denote either a module,
44
a class, a method, a function, or a code object. For a module, it
45
disassembles all functions. For a class, it disassembles all methods. For a
46
single code sequence, it prints one line per bytecode instruction. If no
47
object is provided, it disassembles the last traceback.
50
.. function:: distb([tb])
52
Disassembles the top-of-stack function of a traceback, using the last
53
traceback if none was passed. The instruction causing the exception is
57
.. function:: disassemble(code[, lasti])
59
Disassembles a code object, indicating the last instruction if *lasti* was
60
provided. The output is divided in the following columns:
62
#. the line number, for the first instruction of each line
63
#. the current instruction, indicated as ``-->``,
64
#. a labelled instruction, indicated with ``>>``,
65
#. the address of the instruction,
66
#. the operation code name,
67
#. operation parameters, and
68
#. interpretation of the parameters in parentheses.
70
The parameter interpretation recognizes local and global variable names,
71
constant values, branch targets, and compare operators.
74
.. function:: disco(code[, lasti])
76
A synonym for :func:`disassemble`. It is more convenient to type, and kept
77
for compatibility with earlier Python releases.
80
.. function:: findlinestarts(code)
82
This generator function uses the ``co_firstlineno`` and ``co_lnotab``
83
attributes of the code object *code* to find the offsets which are starts of
84
lines in the source code. They are generated as ``(offset, lineno)`` pairs.
87
.. function:: findlabels(code)
89
Detect all offsets in the code object *code* which are jump targets, and
90
return a list of these offsets.
95
Sequence of operation names, indexable using the bytecode.
100
Dictionary mapping operation names to bytecodes.
105
Sequence of all compare operation names.
110
Sequence of bytecodes that have a constant parameter.
115
Sequence of bytecodes that access a free variable.
120
Sequence of bytecodes that access an attribute by name.
125
Sequence of bytecodes that have a relative jump target.
130
Sequence of bytecodes that have an absolute jump target.
135
Sequence of bytecodes that access a local variable.
140
Sequence of bytecodes of Boolean operations.
145
Python Bytecode Instructions
146
----------------------------
148
The Python compiler currently generates the following bytecode instructions.
151
.. opcode:: STOP_CODE ()
153
Indicates end-of-code to the compiler, not used by the interpreter.
158
Do nothing code. Used as a placeholder by the bytecode optimizer.
161
.. opcode:: POP_TOP ()
163
Removes the top-of-stack (TOS) item.
166
.. opcode:: ROT_TWO ()
168
Swaps the two top-most stack items.
171
.. opcode:: ROT_THREE ()
173
Lifts second and third stack item one position up, moves top down to position
177
.. opcode:: ROT_FOUR ()
179
Lifts second, third and forth stack item one position up, moves top down to
183
.. opcode:: DUP_TOP ()
185
Duplicates the reference on top of the stack.
187
Unary Operations take the top of the stack, apply the operation, and push the
188
result back on the stack.
191
.. opcode:: UNARY_POSITIVE ()
193
Implements ``TOS = +TOS``.
196
.. opcode:: UNARY_NEGATIVE ()
198
Implements ``TOS = -TOS``.
201
.. opcode:: UNARY_NOT ()
203
Implements ``TOS = not TOS``.
206
.. opcode:: UNARY_CONVERT ()
208
Implements ``TOS = `TOS```.
211
.. opcode:: UNARY_INVERT ()
213
Implements ``TOS = ~TOS``.
216
.. opcode:: GET_ITER ()
218
Implements ``TOS = iter(TOS)``.
220
Binary operations remove the top of the stack (TOS) and the second top-most
221
stack item (TOS1) from the stack. They perform the operation, and put the
222
result back on the stack.
225
.. opcode:: BINARY_POWER ()
227
Implements ``TOS = TOS1 ** TOS``.
230
.. opcode:: BINARY_MULTIPLY ()
232
Implements ``TOS = TOS1 * TOS``.
235
.. opcode:: BINARY_DIVIDE ()
237
Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is
241
.. opcode:: BINARY_FLOOR_DIVIDE ()
243
Implements ``TOS = TOS1 // TOS``.
246
.. opcode:: BINARY_TRUE_DIVIDE ()
248
Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is
252
.. opcode:: BINARY_MODULO ()
254
Implements ``TOS = TOS1 % TOS``.
257
.. opcode:: BINARY_ADD ()
259
Implements ``TOS = TOS1 + TOS``.
262
.. opcode:: BINARY_SUBTRACT ()
264
Implements ``TOS = TOS1 - TOS``.
267
.. opcode:: BINARY_SUBSCR ()
269
Implements ``TOS = TOS1[TOS]``.
272
.. opcode:: BINARY_LSHIFT ()
274
Implements ``TOS = TOS1 << TOS``.
277
.. opcode:: BINARY_RSHIFT ()
279
Implements ``TOS = TOS1 >> TOS``.
282
.. opcode:: BINARY_AND ()
284
Implements ``TOS = TOS1 & TOS``.
287
.. opcode:: BINARY_XOR ()
289
Implements ``TOS = TOS1 ^ TOS``.
292
.. opcode:: BINARY_OR ()
294
Implements ``TOS = TOS1 | TOS``.
296
In-place operations are like binary operations, in that they remove TOS and
297
TOS1, and push the result back on the stack, but the operation is done in-place
298
when TOS1 supports it, and the resulting TOS may be (but does not have to be)
302
.. opcode:: INPLACE_POWER ()
304
Implements in-place ``TOS = TOS1 ** TOS``.
307
.. opcode:: INPLACE_MULTIPLY ()
309
Implements in-place ``TOS = TOS1 * TOS``.
312
.. opcode:: INPLACE_DIVIDE ()
314
Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
315
division`` is not in effect.
318
.. opcode:: INPLACE_FLOOR_DIVIDE ()
320
Implements in-place ``TOS = TOS1 // TOS``.
323
.. opcode:: INPLACE_TRUE_DIVIDE ()
325
Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
326
division`` is in effect.
329
.. opcode:: INPLACE_MODULO ()
331
Implements in-place ``TOS = TOS1 % TOS``.
334
.. opcode:: INPLACE_ADD ()
336
Implements in-place ``TOS = TOS1 + TOS``.
339
.. opcode:: INPLACE_SUBTRACT ()
341
Implements in-place ``TOS = TOS1 - TOS``.
344
.. opcode:: INPLACE_LSHIFT ()
346
Implements in-place ``TOS = TOS1 << TOS``.
349
.. opcode:: INPLACE_RSHIFT ()
351
Implements in-place ``TOS = TOS1 >> TOS``.
354
.. opcode:: INPLACE_AND ()
356
Implements in-place ``TOS = TOS1 & TOS``.
359
.. opcode:: INPLACE_XOR ()
361
Implements in-place ``TOS = TOS1 ^ TOS``.
364
.. opcode:: INPLACE_OR ()
366
Implements in-place ``TOS = TOS1 | TOS``.
368
The slice opcodes take up to three parameters.
371
.. opcode:: SLICE+0 ()
373
Implements ``TOS = TOS[:]``.
376
.. opcode:: SLICE+1 ()
378
Implements ``TOS = TOS1[TOS:]``.
381
.. opcode:: SLICE+2 ()
383
Implements ``TOS = TOS1[:TOS]``.
386
.. opcode:: SLICE+3 ()
388
Implements ``TOS = TOS2[TOS1:TOS]``.
390
Slice assignment needs even an additional parameter. As any statement, they put
391
nothing on the stack.
394
.. opcode:: STORE_SLICE+0 ()
396
Implements ``TOS[:] = TOS1``.
399
.. opcode:: STORE_SLICE+1 ()
401
Implements ``TOS1[TOS:] = TOS2``.
404
.. opcode:: STORE_SLICE+2 ()
406
Implements ``TOS1[:TOS] = TOS2``.
409
.. opcode:: STORE_SLICE+3 ()
411
Implements ``TOS2[TOS1:TOS] = TOS3``.
414
.. opcode:: DELETE_SLICE+0 ()
416
Implements ``del TOS[:]``.
419
.. opcode:: DELETE_SLICE+1 ()
421
Implements ``del TOS1[TOS:]``.
424
.. opcode:: DELETE_SLICE+2 ()
426
Implements ``del TOS1[:TOS]``.
429
.. opcode:: DELETE_SLICE+3 ()
431
Implements ``del TOS2[TOS1:TOS]``.
434
.. opcode:: STORE_SUBSCR ()
436
Implements ``TOS1[TOS] = TOS2``.
439
.. opcode:: DELETE_SUBSCR ()
441
Implements ``del TOS1[TOS]``.
443
Miscellaneous opcodes.
446
.. opcode:: PRINT_EXPR ()
448
Implements the expression statement for the interactive mode. TOS is removed
449
from the stack and printed. In non-interactive mode, an expression statement
450
is terminated with :opcode:`POP_TOP`.
453
.. opcode:: PRINT_ITEM ()
455
Prints TOS to the file-like object bound to ``sys.stdout``. There is one
456
such instruction for each item in the :keyword:`print` statement.
459
.. opcode:: PRINT_ITEM_TO ()
461
Like ``PRINT_ITEM``, but prints the item second from TOS to the file-like
462
object at TOS. This is used by the extended print statement.
465
.. opcode:: PRINT_NEWLINE ()
467
Prints a new line on ``sys.stdout``. This is generated as the last operation
468
of a :keyword:`print` statement, unless the statement ends with a comma.
471
.. opcode:: PRINT_NEWLINE_TO ()
473
Like ``PRINT_NEWLINE``, but prints the new line on the file-like object on
474
the TOS. This is used by the extended print statement.
477
.. opcode:: BREAK_LOOP ()
479
Terminates a loop due to a :keyword:`break` statement.
482
.. opcode:: CONTINUE_LOOP (target)
484
Continues a loop due to a :keyword:`continue` statement. *target* is the
485
address to jump to (which should be a :opcode:`FOR_ITER` instruction).
488
.. opcode:: LIST_APPEND (i)
490
Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
491
While the appended value is popped off, the list object remains on the stack
492
so that it is available for further iterations of the loop.
495
.. opcode:: LOAD_LOCALS ()
497
Pushes a reference to the locals of the current scope on the stack. This is
498
used in the code for a class definition: After the class body is evaluated,
499
the locals are passed to the class definition.
502
.. opcode:: RETURN_VALUE ()
504
Returns with TOS to the caller of the function.
507
.. opcode:: YIELD_VALUE ()
509
Pops ``TOS`` and yields it from a :term:`generator`.
512
.. opcode:: IMPORT_STAR ()
514
Loads all symbols not starting with ``'_'`` directly from the module TOS to
515
the local namespace. The module is popped after loading all names. This
516
opcode implements ``from module import *``.
519
.. opcode:: EXEC_STMT ()
521
Implements ``exec TOS2,TOS1,TOS``. The compiler fills missing optional
522
parameters with ``None``.
525
.. opcode:: POP_BLOCK ()
527
Removes one block from the block stack. Per frame, there is a stack of
528
blocks, denoting nested loops, try statements, and such.
531
.. opcode:: END_FINALLY ()
533
Terminates a :keyword:`finally` clause. The interpreter recalls whether the
534
exception has to be re-raised, or whether the function returns, and continues
535
with the outer-next block.
538
.. opcode:: BUILD_CLASS ()
540
Creates a new class object. TOS is the methods dictionary, TOS1 the tuple of
541
the names of the base classes, and TOS2 the class name.
544
.. opcode:: SETUP_WITH (delta)
546
This opcode performs several operations before a with block starts. First,
547
it loads :meth:`~object.__exit__` from the context manager and pushes it onto
548
the stack for later use by :opcode:`WITH_CLEANUP`. Then,
549
:meth:`~object.__enter__` is called, and a finally block pointing to *delta*
550
is pushed. Finally, the result of calling the enter method is pushed onto
551
the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
552
store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
553
:opcode:`UNPACK_SEQUENCE`).
556
.. opcode:: WITH_CLEANUP ()
558
Cleans up the stack when a :keyword:`with` statement block exits. On top of
559
the stack are 1--3 values indicating how/why the finally clause was entered:
562
* (TOP, SECOND) = (``WHY_{RETURN,CONTINUE}``), retval
563
* TOP = ``WHY_*``; no retval below it
564
* (TOP, SECOND, THIRD) = exc_info()
566
Under them is EXIT, the context manager's :meth:`__exit__` bound method.
568
In the last case, ``EXIT(TOP, SECOND, THIRD)`` is called, otherwise
569
``EXIT(None, None, None)``.
571
EXIT is removed from the stack, leaving the values above it in the same
572
order. In addition, if the stack represents an exception, *and* the function
573
call returns a 'true' value, this information is "zapped", to prevent
574
``END_FINALLY`` from re-raising the exception. (But non-local gotos should
577
.. XXX explain the WHY stuff!
580
All of the following opcodes expect arguments. An argument is two bytes, with
581
the more significant byte last.
583
.. opcode:: STORE_NAME (namei)
585
Implements ``name = TOS``. *namei* is the index of *name* in the attribute
586
:attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
587
or ``STORE_GLOBAL`` if possible.
590
.. opcode:: DELETE_NAME (namei)
592
Implements ``del name``, where *namei* is the index into :attr:`co_names`
593
attribute of the code object.
596
.. opcode:: UNPACK_SEQUENCE (count)
598
Unpacks TOS into *count* individual values, which are put onto the stack
602
.. opcode:: DUP_TOPX (count)
604
Duplicate *count* items, keeping them in the same order. Due to
605
implementation limits, *count* should be between 1 and 5 inclusive.
608
.. opcode:: STORE_ATTR (namei)
610
Implements ``TOS.name = TOS1``, where *namei* is the index of name in
614
.. opcode:: DELETE_ATTR (namei)
616
Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
619
.. opcode:: STORE_GLOBAL (namei)
621
Works as ``STORE_NAME``, but stores the name as a global.
624
.. opcode:: DELETE_GLOBAL (namei)
626
Works as ``DELETE_NAME``, but deletes a global name.
629
.. opcode:: LOAD_CONST (consti)
631
Pushes ``co_consts[consti]`` onto the stack.
634
.. opcode:: LOAD_NAME (namei)
636
Pushes the value associated with ``co_names[namei]`` onto the stack.
639
.. opcode:: BUILD_TUPLE (count)
641
Creates a tuple consuming *count* items from the stack, and pushes the
642
resulting tuple onto the stack.
645
.. opcode:: BUILD_LIST (count)
647
Works as ``BUILD_TUPLE``, but creates a list.
650
.. opcode:: BUILD_SET (count)
652
Works as ``BUILD_TUPLE``, but creates a set.
654
.. versionadded:: 2.7
657
.. opcode:: BUILD_MAP (count)
659
Pushes a new dictionary object onto the stack. The dictionary is pre-sized
660
to hold *count* entries.
663
.. opcode:: LOAD_ATTR (namei)
665
Replaces TOS with ``getattr(TOS, co_names[namei])``.
668
.. opcode:: COMPARE_OP (opname)
670
Performs a Boolean operation. The operation name can be found in
674
.. opcode:: IMPORT_NAME (namei)
676
Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
677
the *fromlist* and *level* arguments of :func:`__import__`. The module
678
object is pushed onto the stack. The current namespace is not affected: for
679
a proper import statement, a subsequent ``STORE_FAST`` instruction modifies
683
.. opcode:: IMPORT_FROM (namei)
685
Loads the attribute ``co_names[namei]`` from the module found in TOS. The
686
resulting object is pushed onto the stack, to be subsequently stored by a
687
``STORE_FAST`` instruction.
690
.. opcode:: JUMP_FORWARD (delta)
692
Increments bytecode counter by *delta*.
695
.. opcode:: POP_JUMP_IF_TRUE (target)
697
If TOS is true, sets the bytecode counter to *target*. TOS is popped.
700
.. opcode:: POP_JUMP_IF_FALSE (target)
702
If TOS is false, sets the bytecode counter to *target*. TOS is popped.
705
.. opcode:: JUMP_IF_TRUE_OR_POP (target)
707
If TOS is true, sets the bytecode counter to *target* and leaves TOS on the
708
stack. Otherwise (TOS is false), TOS is popped.
711
.. opcode:: JUMP_IF_FALSE_OR_POP (target)
713
If TOS is false, sets the bytecode counter to *target* and leaves TOS on the
714
stack. Otherwise (TOS is true), TOS is popped.
717
.. opcode:: JUMP_ABSOLUTE (target)
719
Set bytecode counter to *target*.
722
.. opcode:: FOR_ITER (delta)
724
``TOS`` is an :term:`iterator`. Call its :meth:`!next` method. If this
725
yields a new value, push it on the stack (leaving the iterator below it). If
726
the iterator indicates it is exhausted ``TOS`` is popped, and the bytecode
727
counter is incremented by *delta*.
730
.. opcode:: LOAD_GLOBAL (namei)
732
Loads the global named ``co_names[namei]`` onto the stack.
735
.. opcode:: SETUP_LOOP (delta)
737
Pushes a block for a loop onto the block stack. The block spans from the
738
current instruction with a size of *delta* bytes.
741
.. opcode:: SETUP_EXCEPT (delta)
743
Pushes a try block from a try-except clause onto the block stack. *delta*
744
points to the first except block.
747
.. opcode:: SETUP_FINALLY (delta)
749
Pushes a try block from a try-except clause onto the block stack. *delta*
750
points to the finally block.
752
.. opcode:: STORE_MAP ()
754
Store a key and value pair in a dictionary. Pops the key and value while
755
leaving the dictionary on the stack.
757
.. opcode:: LOAD_FAST (var_num)
759
Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
762
.. opcode:: STORE_FAST (var_num)
764
Stores TOS into the local ``co_varnames[var_num]``.
767
.. opcode:: DELETE_FAST (var_num)
769
Deletes local ``co_varnames[var_num]``.
772
.. opcode:: LOAD_CLOSURE (i)
774
Pushes a reference to the cell contained in slot *i* of the cell and free
775
variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
776
less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
780
.. opcode:: LOAD_DEREF (i)
782
Loads the cell contained in slot *i* of the cell and free variable storage.
783
Pushes a reference to the object the cell contains on the stack.
786
.. opcode:: STORE_DEREF (i)
788
Stores TOS into the cell contained in slot *i* of the cell and free variable
792
.. opcode:: SET_LINENO (lineno)
794
This opcode is obsolete.
797
.. opcode:: RAISE_VARARGS (argc)
799
Raises an exception. *argc* indicates the number of parameters to the raise
800
statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
801
the parameter as TOS1, and the exception as TOS.
804
.. opcode:: CALL_FUNCTION (argc)
806
Calls a function. The low byte of *argc* indicates the number of positional
807
parameters, the high byte the number of keyword parameters. On the stack, the
808
opcode finds the keyword parameters first. For each keyword argument, the
809
value is on top of the key. Below the keyword parameters, the positional
810
parameters are on the stack, with the right-most parameter on top. Below the
811
parameters, the function object to call is on the stack. Pops all function
812
arguments, and the function itself off the stack, and pushes the return
816
.. opcode:: MAKE_FUNCTION (argc)
818
Pushes a new function object on the stack. TOS is the code associated with
819
the function. The function object is defined to have *argc* default
820
parameters, which are found below TOS.
823
.. opcode:: MAKE_CLOSURE (argc)
825
Creates a new function object, sets its *func_closure* slot, and pushes it on
826
the stack. TOS is the code associated with the function, TOS1 the tuple
827
containing cells for the closure's free variables. The function also has
828
*argc* default parameters, which are found below the cells.
831
.. opcode:: BUILD_SLICE (argc)
833
.. index:: builtin: slice
835
Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
836
``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
837
pushed. See the :func:`slice` built-in function for more information.
840
.. opcode:: EXTENDED_ARG (ext)
842
Prefixes any opcode which has an argument too big to fit into the default two
843
bytes. *ext* holds two additional bytes which, taken together with the
844
subsequent opcode's argument, comprise a four-byte argument, *ext* being the
845
two most-significant bytes.
848
.. opcode:: CALL_FUNCTION_VAR (argc)
850
Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
851
top element on the stack contains the variable argument list, followed by
852
keyword and positional arguments.
855
.. opcode:: CALL_FUNCTION_KW (argc)
857
Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
858
top element on the stack contains the keyword arguments dictionary, followed
859
by explicit keyword and positional arguments.
862
.. opcode:: CALL_FUNCTION_VAR_KW (argc)
864
Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
865
top element on the stack contains the keyword arguments dictionary, followed
866
by the variable-arguments tuple, followed by explicit keyword and positional
870
.. opcode:: HAVE_ARGUMENT ()
872
This is not really an opcode. It identifies the dividing line between
873
opcodes which don't take arguments ``< HAVE_ARGUMENT`` and those which do
874
``>= HAVE_ARGUMENT``.