blob: 8653da765bf651032d98264a019e69af6548b540 [file] [log] [blame]
Georg Brandl9afde1c2007-11-01 20:32:30 +00001:mod:`dis` --- Disassembler for Python bytecode
2===============================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: dis
Georg Brandl9afde1c2007-11-01 20:32:30 +00005 :synopsis: Disassembler for Python bytecode.
Georg Brandl116aa622007-08-15 14:28:22 +00006
Raymond Hettinger10480942011-01-10 03:26:08 +00007**Source code:** :source:`Lib/dis.py`
Georg Brandl116aa622007-08-15 14:28:22 +00008
Raymond Hettinger4f707fd2011-01-10 19:54:11 +00009--------------
10
Brett Cannon8315fd12010-07-02 22:03:00 +000011The :mod:`dis` module supports the analysis of CPython :term:`bytecode` by
12disassembling it. The CPython bytecode which this module takes as an
Georg Brandl71515ca2009-05-17 12:29:12 +000013input is defined in the file :file:`Include/opcode.h` and used by the compiler
14and the interpreter.
Georg Brandl116aa622007-08-15 14:28:22 +000015
Georg Brandl19b7a872010-07-03 10:21:50 +000016.. impl-detail::
17
Raymond Hettinger10480942011-01-10 03:26:08 +000018 Bytecode is an implementation detail of the CPython interpreter. No
Georg Brandl19b7a872010-07-03 10:21:50 +000019 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.
22
Brett Cannon8315fd12010-07-02 22:03:00 +000023
Georg Brandl116aa622007-08-15 14:28:22 +000024Example: Given the function :func:`myfunc`::
25
26 def myfunc(alist):
27 return len(alist)
28
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100029the following command can be used to display the disassembly of
30:func:`myfunc`::
Georg Brandl116aa622007-08-15 14:28:22 +000031
32 >>> dis.dis(myfunc)
33 2 0 LOAD_GLOBAL 0 (len)
34 3 LOAD_FAST 0 (alist)
35 6 CALL_FUNCTION 1
36 9 RETURN_VALUE
37
38(The "2" is a line number).
39
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100040Bytecode analysis
41-----------------
Georg Brandl116aa622007-08-15 14:28:22 +000042
R David Murray0bce6e72014-01-07 14:30:17 -050043.. versionadded:: 3.4
44
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100045The bytecode analysis API allows pieces of Python code to be wrapped in a
46:class:`Bytecode` object that provides easy access to details of the
47compiled code.
48
Nick Coghlan50c48b82013-11-23 00:57:00 +100049.. class:: Bytecode(x, *, first_line=None, current_offset=None)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100050
Nick Coghlanefd5df92014-07-25 23:02:56 +100051 Analyse the bytecode corresponding to a function, generator, method,
52 string of source code, or a code object (as returned by :func:`compile`).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100053
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100054 This is a convenience wrapper around many of the functions listed below,
55 most notably :func:`get_instructions`, as iterating over a
Nick Coghlan07155c92013-11-06 22:12:07 +100056 :class:`Bytecode` instance yields the bytecode operations as
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100057 :class:`Instruction` instances.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100058
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100059 If *first_line* is not None, it indicates the line number that should
60 be reported for the first source line in the disassembled code.
61 Otherwise, the source line information (if any) is taken directly from
62 the disassembled code object.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100063
Nick Coghlan50c48b82013-11-23 00:57:00 +100064 If *current_offset* is not None, it refers to an instruction offset
65 in the disassembled code. Setting this means :meth:`dis` will display
66 a "current instruction" marker against the specified opcode.
67
68 .. classmethod:: from_traceback(tb)
69
70 Construct a :class:`Bytecode` instance from the given traceback,
71 setting *current_offset* to the instruction responsible for the
72 exception.
73
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100074 .. data:: codeobj
75
76 The compiled code object.
77
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100078 .. data:: first_line
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100079
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100080 The first source line of the code object (if available)
81
82 .. method:: dis()
83
84 Return a formatted view of the bytecode operations (the same as
85 printed by :func:`dis`, but returned as a multi-line string).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100086
87 .. method:: info()
88
89 Return a formatted multi-line string with detailed information about the
90 code object, like :func:`code_info`.
91
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100092Example::
93
94 >>> bytecode = dis.Bytecode(myfunc)
95 >>> for instr in bytecode:
96 ... print(instr.opname)
97 ...
98 LOAD_GLOBAL
99 LOAD_FAST
100 CALL_FUNCTION
101 RETURN_VALUE
102
103
104Analysis functions
105------------------
106
107The :mod:`dis` module also defines the following analysis functions that
108convert the input directly to the desired output. They can be useful if
109only a single operation is being performed, so the intermediate analysis
110object isn't useful:
Georg Brandl116aa622007-08-15 14:28:22 +0000111
Nick Coghlane8814fb2010-09-10 14:08:04 +0000112.. function:: code_info(x)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000113
Georg Brandl67b21b72010-08-17 15:07:14 +0000114 Return a formatted multi-line string with detailed code object information
Nick Coghlanefd5df92014-07-25 23:02:56 +1000115 for the supplied function, generator, method, source code string or code object.
Nick Coghlaneae2da12010-08-17 08:03:36 +0000116
Georg Brandl67b21b72010-08-17 15:07:14 +0000117 Note that the exact contents of code info strings are highly implementation
118 dependent and they may change arbitrarily across Python VMs or Python
119 releases.
Nick Coghlaneae2da12010-08-17 08:03:36 +0000120
121 .. versionadded:: 3.2
122
Georg Brandl67b21b72010-08-17 15:07:14 +0000123
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000124.. function:: show_code(x, *, file=None)
Nick Coghlane8814fb2010-09-10 14:08:04 +0000125
126 Print detailed code object information for the supplied function, method,
Ezio Melotti6e6c6ac2013-08-23 22:41:39 +0300127 source code string or code object to *file* (or ``sys.stdout`` if *file*
128 is not specified).
Nick Coghlane8814fb2010-09-10 14:08:04 +0000129
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000130 This is a convenient shorthand for ``print(code_info(x), file=file)``,
131 intended for interactive exploration at the interpreter prompt.
Nick Coghlane8814fb2010-09-10 14:08:04 +0000132
133 .. versionadded:: 3.2
134
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000135 .. versionchanged:: 3.4
136 Added ``file`` parameter
137
138
139.. function:: dis(x=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000140
Georg Brandl67b21b72010-08-17 15:07:14 +0000141 Disassemble the *x* object. *x* can denote either a module, a class, a
Nick Coghlanefd5df92014-07-25 23:02:56 +1000142 method, a function, a generator, a code object, a string of source code or
143 a byte sequence of raw bytecode. For a module, it disassembles all functions.
144 For a class, it disassembles all methods. For a code object or sequence of
145 raw bytecode, it prints one line per bytecode instruction. Strings are first
146 compiled to code objects with the :func:`compile` built-in function before being
Georg Brandl67b21b72010-08-17 15:07:14 +0000147 disassembled. If no object is provided, this function disassembles the last
148 traceback.
Georg Brandl116aa622007-08-15 14:28:22 +0000149
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000150 The disassembly is written as text to the supplied ``file`` argument if
151 provided and to ``sys.stdout`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000152
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000153 .. versionchanged:: 3.4
154 Added ``file`` parameter
155
156
157.. function:: distb(tb=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000158
Georg Brandl4833e5b2010-07-03 10:41:33 +0000159 Disassemble the top-of-stack function of a traceback, using the last
160 traceback if none was passed. The instruction causing the exception is
161 indicated.
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000163 The disassembly is written as text to the supplied ``file`` argument if
164 provided and to ``sys.stdout`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000165
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000166 .. versionchanged:: 3.4
167 Added ``file`` parameter
168
169
170.. function:: disassemble(code, lasti=-1, *, file=None)
171 disco(code, lasti=-1, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000172
Georg Brandl4833e5b2010-07-03 10:41:33 +0000173 Disassemble a code object, indicating the last instruction if *lasti* was
Georg Brandl116aa622007-08-15 14:28:22 +0000174 provided. The output is divided in the following columns:
175
176 #. the line number, for the first instruction of each line
177 #. the current instruction, indicated as ``-->``,
178 #. a labelled instruction, indicated with ``>>``,
179 #. the address of the instruction,
180 #. the operation code name,
181 #. operation parameters, and
182 #. interpretation of the parameters in parentheses.
183
184 The parameter interpretation recognizes local and global variable names,
185 constant values, branch targets, and compare operators.
186
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000187 The disassembly is written as text to the supplied ``file`` argument if
188 provided and to ``sys.stdout`` otherwise.
189
190 .. versionchanged:: 3.4
191 Added ``file`` parameter
192
193
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000194.. function:: get_instructions(x, *, first_line=None)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000195
196 Return an iterator over the instructions in the supplied function, method,
197 source code string or code object.
198
199 The iterator generates a series of :class:`Instruction` named tuples
200 giving the details of each operation in the supplied code.
201
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000202 If *first_line* is not None, it indicates the line number that should
203 be reported for the first source line in the disassembled code.
204 Otherwise, the source line information (if any) is taken directly from
205 the disassembled code object.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000206
207 .. versionadded:: 3.4
208
Georg Brandl116aa622007-08-15 14:28:22 +0000209
Benjamin Peterson75edad02009-01-01 15:05:06 +0000210.. function:: findlinestarts(code)
211
212 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
213 attributes of the code object *code* to find the offsets which are starts of
214 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
215
216
217.. function:: findlabels(code)
218
219 Detect all offsets in the code object *code* which are jump targets, and
220 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +0000221
Larry Hastings3a907972013-11-23 14:49:22 -0800222
223.. function:: stack_effect(opcode, [oparg])
224
225 Compute the stack effect of *opcode* with argument *oparg*.
226
227 .. versionadded:: 3.4
228
Georg Brandl116aa622007-08-15 14:28:22 +0000229.. _bytecodes:
230
Georg Brandl9afde1c2007-11-01 20:32:30 +0000231Python Bytecode Instructions
232----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000233
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000234The :func:`get_instructions` function and :class:`Bytecode` class provide
235details of bytecode instructions as :class:`Instruction` instances:
236
237.. class:: Instruction
238
239 Details for a bytecode operation
240
241 .. data:: opcode
242
243 numeric code for operation, corresponding to the opcode values listed
244 below and the bytecode values in the :ref:`opcode_collections`.
245
246
247 .. data:: opname
248
249 human readable name for operation
250
251
252 .. data:: arg
253
254 numeric argument to operation (if any), otherwise None
255
256
257 .. data:: argval
258
259 resolved arg value (if known), otherwise same as arg
260
261
262 .. data:: argrepr
263
264 human readable description of operation argument
265
266
267 .. data:: offset
268
269 start index of operation within bytecode sequence
270
271
272 .. data:: starts_line
273
274 line started by this opcode (if any), otherwise None
275
276
277 .. data:: is_jump_target
278
Serhiy Storchaka0e90e992013-11-29 12:19:53 +0200279 ``True`` if other code jumps to here, otherwise ``False``
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000280
281 .. versionadded:: 3.4
282
283
Georg Brandl9afde1c2007-11-01 20:32:30 +0000284The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000285
286
Georg Brandl4833e5b2010-07-03 10:41:33 +0000287**General instructions**
288
Georg Brandl4833e5b2010-07-03 10:41:33 +0000289.. opcode:: NOP
Georg Brandl116aa622007-08-15 14:28:22 +0000290
291 Do nothing code. Used as a placeholder by the bytecode optimizer.
292
293
Georg Brandl4833e5b2010-07-03 10:41:33 +0000294.. opcode:: POP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000295
296 Removes the top-of-stack (TOS) item.
297
298
Georg Brandl4833e5b2010-07-03 10:41:33 +0000299.. opcode:: ROT_TWO
Georg Brandl116aa622007-08-15 14:28:22 +0000300
301 Swaps the two top-most stack items.
302
303
Georg Brandl4833e5b2010-07-03 10:41:33 +0000304.. opcode:: ROT_THREE
Georg Brandl116aa622007-08-15 14:28:22 +0000305
306 Lifts second and third stack item one position up, moves top down to position
307 three.
308
309
Georg Brandl4833e5b2010-07-03 10:41:33 +0000310.. opcode:: DUP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000311
312 Duplicates the reference on top of the stack.
313
Georg Brandl4833e5b2010-07-03 10:41:33 +0000314
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000315.. opcode:: DUP_TOP_TWO
316
317 Duplicates the two references on top of the stack, leaving them in the
318 same order.
319
320
Georg Brandl4833e5b2010-07-03 10:41:33 +0000321**Unary operations**
322
323Unary operations take the top of the stack, apply the operation, and push the
Georg Brandl116aa622007-08-15 14:28:22 +0000324result back on the stack.
325
Georg Brandl4833e5b2010-07-03 10:41:33 +0000326.. opcode:: UNARY_POSITIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000327
328 Implements ``TOS = +TOS``.
329
330
Georg Brandl4833e5b2010-07-03 10:41:33 +0000331.. opcode:: UNARY_NEGATIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000332
333 Implements ``TOS = -TOS``.
334
335
Georg Brandl4833e5b2010-07-03 10:41:33 +0000336.. opcode:: UNARY_NOT
Georg Brandl116aa622007-08-15 14:28:22 +0000337
338 Implements ``TOS = not TOS``.
339
340
Georg Brandl4833e5b2010-07-03 10:41:33 +0000341.. opcode:: UNARY_INVERT
Georg Brandl116aa622007-08-15 14:28:22 +0000342
343 Implements ``TOS = ~TOS``.
344
345
Georg Brandl4833e5b2010-07-03 10:41:33 +0000346.. opcode:: GET_ITER
Georg Brandl116aa622007-08-15 14:28:22 +0000347
348 Implements ``TOS = iter(TOS)``.
349
Georg Brandl4833e5b2010-07-03 10:41:33 +0000350
351**Binary operations**
352
Georg Brandl116aa622007-08-15 14:28:22 +0000353Binary operations remove the top of the stack (TOS) and the second top-most
354stack item (TOS1) from the stack. They perform the operation, and put the
355result back on the stack.
356
Georg Brandl4833e5b2010-07-03 10:41:33 +0000357.. opcode:: BINARY_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000358
359 Implements ``TOS = TOS1 ** TOS``.
360
361
Georg Brandl4833e5b2010-07-03 10:41:33 +0000362.. opcode:: BINARY_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000363
364 Implements ``TOS = TOS1 * TOS``.
365
366
Benjamin Petersond51374e2014-04-09 23:55:56 -0400367.. opcode:: BINARY_MATRIX_MULTIPLY
368
369 Implements ``TOS = TOS1 @ TOS``.
370
371
Georg Brandl4833e5b2010-07-03 10:41:33 +0000372.. opcode:: BINARY_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000373
374 Implements ``TOS = TOS1 // TOS``.
375
376
Georg Brandl4833e5b2010-07-03 10:41:33 +0000377.. opcode:: BINARY_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000378
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000379 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000380
381
Georg Brandl4833e5b2010-07-03 10:41:33 +0000382.. opcode:: BINARY_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000383
384 Implements ``TOS = TOS1 % TOS``.
385
386
Georg Brandl4833e5b2010-07-03 10:41:33 +0000387.. opcode:: BINARY_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000388
389 Implements ``TOS = TOS1 + TOS``.
390
391
Georg Brandl4833e5b2010-07-03 10:41:33 +0000392.. opcode:: BINARY_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000393
394 Implements ``TOS = TOS1 - TOS``.
395
396
Georg Brandl4833e5b2010-07-03 10:41:33 +0000397.. opcode:: BINARY_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000398
399 Implements ``TOS = TOS1[TOS]``.
400
401
Georg Brandl4833e5b2010-07-03 10:41:33 +0000402.. opcode:: BINARY_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000403
404 Implements ``TOS = TOS1 << TOS``.
405
406
Georg Brandl4833e5b2010-07-03 10:41:33 +0000407.. opcode:: BINARY_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000408
409 Implements ``TOS = TOS1 >> TOS``.
410
411
Georg Brandl4833e5b2010-07-03 10:41:33 +0000412.. opcode:: BINARY_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000413
414 Implements ``TOS = TOS1 & TOS``.
415
416
Georg Brandl4833e5b2010-07-03 10:41:33 +0000417.. opcode:: BINARY_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000418
419 Implements ``TOS = TOS1 ^ TOS``.
420
421
Georg Brandl4833e5b2010-07-03 10:41:33 +0000422.. opcode:: BINARY_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000423
424 Implements ``TOS = TOS1 | TOS``.
425
Georg Brandl4833e5b2010-07-03 10:41:33 +0000426
427**In-place operations**
428
Georg Brandl116aa622007-08-15 14:28:22 +0000429In-place operations are like binary operations, in that they remove TOS and
430TOS1, and push the result back on the stack, but the operation is done in-place
431when TOS1 supports it, and the resulting TOS may be (but does not have to be)
432the original TOS1.
433
Georg Brandl4833e5b2010-07-03 10:41:33 +0000434.. opcode:: INPLACE_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000435
436 Implements in-place ``TOS = TOS1 ** TOS``.
437
438
Georg Brandl4833e5b2010-07-03 10:41:33 +0000439.. opcode:: INPLACE_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000440
441 Implements in-place ``TOS = TOS1 * TOS``.
442
443
Benjamin Petersond51374e2014-04-09 23:55:56 -0400444.. opcode:: INPLACE_MATRIX_MULTIPLY
445
446 Implements in-place ``TOS = TOS1 @ TOS``.
447
448
Georg Brandl4833e5b2010-07-03 10:41:33 +0000449.. opcode:: INPLACE_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000450
451 Implements in-place ``TOS = TOS1 // TOS``.
452
453
Georg Brandl4833e5b2010-07-03 10:41:33 +0000454.. opcode:: INPLACE_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000455
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000456 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000457
458
Georg Brandl4833e5b2010-07-03 10:41:33 +0000459.. opcode:: INPLACE_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000460
461 Implements in-place ``TOS = TOS1 % TOS``.
462
463
Georg Brandl4833e5b2010-07-03 10:41:33 +0000464.. opcode:: INPLACE_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000465
466 Implements in-place ``TOS = TOS1 + TOS``.
467
468
Georg Brandl4833e5b2010-07-03 10:41:33 +0000469.. opcode:: INPLACE_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000470
471 Implements in-place ``TOS = TOS1 - TOS``.
472
473
Georg Brandl4833e5b2010-07-03 10:41:33 +0000474.. opcode:: INPLACE_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000475
476 Implements in-place ``TOS = TOS1 << TOS``.
477
478
Georg Brandl4833e5b2010-07-03 10:41:33 +0000479.. opcode:: INPLACE_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000480
481 Implements in-place ``TOS = TOS1 >> TOS``.
482
483
Georg Brandl4833e5b2010-07-03 10:41:33 +0000484.. opcode:: INPLACE_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000485
486 Implements in-place ``TOS = TOS1 & TOS``.
487
488
Georg Brandl4833e5b2010-07-03 10:41:33 +0000489.. opcode:: INPLACE_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000490
491 Implements in-place ``TOS = TOS1 ^ TOS``.
492
493
Georg Brandl4833e5b2010-07-03 10:41:33 +0000494.. opcode:: INPLACE_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000495
496 Implements in-place ``TOS = TOS1 | TOS``.
497
Georg Brandl116aa622007-08-15 14:28:22 +0000498
Georg Brandl4833e5b2010-07-03 10:41:33 +0000499.. opcode:: STORE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000500
501 Implements ``TOS1[TOS] = TOS2``.
502
503
Georg Brandl4833e5b2010-07-03 10:41:33 +0000504.. opcode:: DELETE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000505
506 Implements ``del TOS1[TOS]``.
507
Georg Brandl116aa622007-08-15 14:28:22 +0000508
Georg Brandl4833e5b2010-07-03 10:41:33 +0000509**Miscellaneous opcodes**
Georg Brandl116aa622007-08-15 14:28:22 +0000510
Georg Brandl4833e5b2010-07-03 10:41:33 +0000511.. opcode:: PRINT_EXPR
Georg Brandl116aa622007-08-15 14:28:22 +0000512
513 Implements the expression statement for the interactive mode. TOS is removed
514 from the stack and printed. In non-interactive mode, an expression statement is
515 terminated with ``POP_STACK``.
516
517
Georg Brandl4833e5b2010-07-03 10:41:33 +0000518.. opcode:: BREAK_LOOP
Georg Brandl116aa622007-08-15 14:28:22 +0000519
520 Terminates a loop due to a :keyword:`break` statement.
521
522
523.. opcode:: CONTINUE_LOOP (target)
524
525 Continues a loop due to a :keyword:`continue` statement. *target* is the
526 address to jump to (which should be a ``FOR_ITER`` instruction).
527
528
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000529.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000530
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000531 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000532
533
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000534.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000535
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000536 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
537
538
539.. opcode:: MAP_ADD (i)
540
541 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
542 comprehensions.
543
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000544For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the
545added value or key/value pair is popped off, the container object remains on
546the stack so that it is available for further iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000547
548
Georg Brandl4833e5b2010-07-03 10:41:33 +0000549.. opcode:: RETURN_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000550
551 Returns with TOS to the caller of the function.
552
553
Georg Brandl4833e5b2010-07-03 10:41:33 +0000554.. opcode:: YIELD_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000555
Georg Brandl9afde1c2007-11-01 20:32:30 +0000556 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000557
558
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000559.. opcode:: YIELD_FROM
560
561 Pops ``TOS`` and delegates to it as a subiterator from a :term:`generator`.
562
563 .. versionadded:: 3.3
564
565
Georg Brandl4833e5b2010-07-03 10:41:33 +0000566.. opcode:: IMPORT_STAR
Georg Brandl116aa622007-08-15 14:28:22 +0000567
568 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
569 local namespace. The module is popped after loading all names. This opcode
570 implements ``from module import *``.
571
572
Georg Brandl4833e5b2010-07-03 10:41:33 +0000573.. opcode:: POP_BLOCK
Georg Brandl116aa622007-08-15 14:28:22 +0000574
575 Removes one block from the block stack. Per frame, there is a stack of blocks,
576 denoting nested loops, try statements, and such.
577
578
Georg Brandl4833e5b2010-07-03 10:41:33 +0000579.. opcode:: POP_EXCEPT
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000580
581 Removes one block from the block stack. The popped block must be an exception
582 handler block, as implicitly created when entering an except handler.
583 In addition to popping extraneous values from the frame stack, the
584 last three popped values are used to restore the exception state.
585
586
Georg Brandl4833e5b2010-07-03 10:41:33 +0000587.. opcode:: END_FINALLY
Georg Brandl116aa622007-08-15 14:28:22 +0000588
589 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
590 exception has to be re-raised, or whether the function returns, and continues
591 with the outer-next block.
592
593
Georg Brandl4833e5b2010-07-03 10:41:33 +0000594.. opcode:: LOAD_BUILD_CLASS
Georg Brandl116aa622007-08-15 14:28:22 +0000595
Georg Brandl5ac22302008-07-20 21:39:03 +0000596 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Benjamin Petersonaac8fd32008-07-20 22:02:26 +0000597 by ``CALL_FUNCTION`` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000598
Guido van Rossum04110fb2007-08-24 16:32:05 +0000599
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000600.. opcode:: SETUP_WITH (delta)
601
602 This opcode performs several operations before a with block starts. First,
603 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
604 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
605 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
606 is pushed. Finally, the result of calling the enter method is pushed onto
607 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
608 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
609 :opcode:`UNPACK_SEQUENCE`).
610
611
Georg Brandl4833e5b2010-07-03 10:41:33 +0000612.. opcode:: WITH_CLEANUP
Guido van Rossum04110fb2007-08-24 16:32:05 +0000613
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000614 Cleans up the stack when a :keyword:`with` statement block exits. TOS is
615 the context manager's :meth:`__exit__` bound method. Below TOS are 1--3
616 values indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000617
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000618 * SECOND = ``None``
619 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
620 * SECOND = ``WHY_*``; no retval below it
621 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000622
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000623 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
624 ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000625
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000626 If the stack represents an exception, *and* the function call returns
627 a 'true' value, this information is "zapped" and replaced with a single
628 ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception.
629 (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000630
Georg Brandl9afde1c2007-11-01 20:32:30 +0000631 .. XXX explain the WHY stuff!
632
Guido van Rossum04110fb2007-08-24 16:32:05 +0000633
Georg Brandl116aa622007-08-15 14:28:22 +0000634All of the following opcodes expect arguments. An argument is two bytes, with
635the more significant byte last.
636
Georg Brandl116aa622007-08-15 14:28:22 +0000637.. opcode:: STORE_NAME (namei)
638
639 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Christian Heimes8640e742008-02-23 16:23:06 +0000640 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl116aa622007-08-15 14:28:22 +0000641 or ``STORE_GLOBAL`` if possible.
642
643
644.. opcode:: DELETE_NAME (namei)
645
646 Implements ``del name``, where *namei* is the index into :attr:`co_names`
647 attribute of the code object.
648
649
650.. opcode:: UNPACK_SEQUENCE (count)
651
652 Unpacks TOS into *count* individual values, which are put onto the stack
653 right-to-left.
654
Georg Brandl116aa622007-08-15 14:28:22 +0000655
Georg Brandl5ac22302008-07-20 21:39:03 +0000656.. opcode:: UNPACK_EX (counts)
657
658 Implements assignment with a starred target: Unpacks an iterable in TOS into
659 individual values, where the total number of values can be smaller than the
660 number of items in the iterable: one the new values will be a list of all
661 leftover items.
662
663 The low byte of *counts* is the number of values before the list value, the
664 high byte of *counts* the number of values after it. The resulting values
665 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000666
Georg Brandl5ac22302008-07-20 21:39:03 +0000667
Georg Brandl116aa622007-08-15 14:28:22 +0000668.. opcode:: STORE_ATTR (namei)
669
670 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
671 :attr:`co_names`.
672
673
674.. opcode:: DELETE_ATTR (namei)
675
676 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
677
678
679.. opcode:: STORE_GLOBAL (namei)
680
681 Works as ``STORE_NAME``, but stores the name as a global.
682
683
684.. opcode:: DELETE_GLOBAL (namei)
685
686 Works as ``DELETE_NAME``, but deletes a global name.
687
Georg Brandl116aa622007-08-15 14:28:22 +0000688
689.. opcode:: LOAD_CONST (consti)
690
691 Pushes ``co_consts[consti]`` onto the stack.
692
693
694.. opcode:: LOAD_NAME (namei)
695
696 Pushes the value associated with ``co_names[namei]`` onto the stack.
697
698
699.. opcode:: BUILD_TUPLE (count)
700
701 Creates a tuple consuming *count* items from the stack, and pushes the resulting
702 tuple onto the stack.
703
704
705.. opcode:: BUILD_LIST (count)
706
707 Works as ``BUILD_TUPLE``, but creates a list.
708
709
710.. opcode:: BUILD_SET (count)
711
712 Works as ``BUILD_TUPLE``, but creates a set.
713
714
Christian Heimesa62da1d2008-01-12 19:39:10 +0000715.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000716
Christian Heimesa62da1d2008-01-12 19:39:10 +0000717 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
718 to hold *count* entries.
Georg Brandl116aa622007-08-15 14:28:22 +0000719
720
721.. opcode:: LOAD_ATTR (namei)
722
723 Replaces TOS with ``getattr(TOS, co_names[namei])``.
724
725
726.. opcode:: COMPARE_OP (opname)
727
728 Performs a Boolean operation. The operation name can be found in
729 ``cmp_op[opname]``.
730
731
732.. opcode:: IMPORT_NAME (namei)
733
Christian Heimesa342c012008-04-20 21:01:16 +0000734 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
735 the *fromlist* and *level* arguments of :func:`__import__`. The module
736 object is pushed onto the stack. The current namespace is not affected:
737 for a proper import statement, a subsequent ``STORE_FAST`` instruction
738 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000739
740
741.. opcode:: IMPORT_FROM (namei)
742
743 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
744 resulting object is pushed onto the stack, to be subsequently stored by a
745 ``STORE_FAST`` instruction.
746
747
748.. opcode:: JUMP_FORWARD (delta)
749
Georg Brandl9afde1c2007-11-01 20:32:30 +0000750 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000751
752
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000753.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000754
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000755 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000756
757
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000758.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000759
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000760 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
761
762
763.. opcode:: JUMP_IF_TRUE_OR_POP (target)
764
765 If TOS is true, sets the bytecode counter to *target* and leaves TOS
766 on the stack. Otherwise (TOS is false), TOS is popped.
767
768
769.. opcode:: JUMP_IF_FALSE_OR_POP (target)
770
771 If TOS is false, sets the bytecode counter to *target* and leaves
772 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000773
774
775.. opcode:: JUMP_ABSOLUTE (target)
776
Georg Brandl9afde1c2007-11-01 20:32:30 +0000777 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000778
779
780.. opcode:: FOR_ITER (delta)
781
Ezio Melotti7fa82222012-10-12 13:42:08 +0300782 ``TOS`` is an :term:`iterator`. Call its :meth:`~iterator.__next__` method.
783 If this yields a new value, push it on the stack (leaving the iterator below
784 it). If the iterator indicates it is exhausted ``TOS`` is popped, and the
785 byte code counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000786
Georg Brandl116aa622007-08-15 14:28:22 +0000787
788.. opcode:: LOAD_GLOBAL (namei)
789
790 Loads the global named ``co_names[namei]`` onto the stack.
791
Georg Brandl116aa622007-08-15 14:28:22 +0000792
793.. opcode:: SETUP_LOOP (delta)
794
795 Pushes a block for a loop onto the block stack. The block spans from the
796 current instruction with a size of *delta* bytes.
797
798
799.. opcode:: SETUP_EXCEPT (delta)
800
801 Pushes a try block from a try-except clause onto the block stack. *delta* points
802 to the first except block.
803
804
805.. opcode:: SETUP_FINALLY (delta)
806
807 Pushes a try block from a try-except clause onto the block stack. *delta* points
808 to the finally block.
809
Georg Brandl4833e5b2010-07-03 10:41:33 +0000810.. opcode:: STORE_MAP
Christian Heimesa62da1d2008-01-12 19:39:10 +0000811
812 Store a key and value pair in a dictionary. Pops the key and value while leaving
813 the dictionary on the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000814
815.. opcode:: LOAD_FAST (var_num)
816
817 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
818
819
820.. opcode:: STORE_FAST (var_num)
821
822 Stores TOS into the local ``co_varnames[var_num]``.
823
824
825.. opcode:: DELETE_FAST (var_num)
826
827 Deletes local ``co_varnames[var_num]``.
828
829
830.. opcode:: LOAD_CLOSURE (i)
831
832 Pushes a reference to the cell contained in slot *i* of the cell and free
833 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
834 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
835 len(co_cellvars)]``.
836
837
838.. opcode:: LOAD_DEREF (i)
839
840 Loads the cell contained in slot *i* of the cell and free variable storage.
841 Pushes a reference to the object the cell contains on the stack.
842
843
Benjamin Peterson3b0431d2013-04-30 09:41:40 -0400844.. opcode:: LOAD_CLASSDEREF (i)
845
846 Much like :opcode:`LOAD_DEREF` but first checks the locals dictionary before
847 consulting the cell. This is used for loading free variables in class
848 bodies.
849
850
Georg Brandl116aa622007-08-15 14:28:22 +0000851.. opcode:: STORE_DEREF (i)
852
853 Stores TOS into the cell contained in slot *i* of the cell and free variable
854 storage.
855
856
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000857.. opcode:: DELETE_DEREF (i)
858
859 Empties the cell contained in slot *i* of the cell and free variable storage.
860 Used by the :keyword:`del` statement.
861
862
Georg Brandl116aa622007-08-15 14:28:22 +0000863.. opcode:: RAISE_VARARGS (argc)
864
865 Raises an exception. *argc* indicates the number of parameters to the raise
866 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
867 the parameter as TOS1, and the exception as TOS.
868
869
870.. opcode:: CALL_FUNCTION (argc)
871
872 Calls a function. The low byte of *argc* indicates the number of positional
873 parameters, the high byte the number of keyword parameters. On the stack, the
874 opcode finds the keyword parameters first. For each keyword argument, the value
875 is on top of the key. Below the keyword parameters, the positional parameters
876 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl48310cd2009-01-03 21:18:54 +0000877 the function object to call is on the stack. Pops all function arguments, and
Benjamin Peterson206e3072008-10-19 14:07:49 +0000878 the function itself off the stack, and pushes the return value.
Georg Brandl116aa622007-08-15 14:28:22 +0000879
880
881.. opcode:: MAKE_FUNCTION (argc)
882
Georg Brandlc96ef1f2013-10-12 18:41:18 +0200883 Pushes a new function object on the stack. From bottom to top, the consumed
884 stack must consist of
885
886 * ``argc & 0xFF`` default argument objects in positional order
887 * ``(argc >> 8) & 0xFF`` pairs of name and default argument, with the name
888 just below the object on the stack, for keyword-only parameters
889 * ``(argc >> 16) & 0x7FFF`` parameter annotation objects
890 * a tuple listing the parameter names for the annotations (only if there are
891 ony annotation objects)
892 * the code associated with the function (at TOS1)
893 * the :term:`qualified name` of the function (at TOS)
Georg Brandl116aa622007-08-15 14:28:22 +0000894
895
896.. opcode:: MAKE_CLOSURE (argc)
897
Guido van Rossum04110fb2007-08-24 16:32:05 +0000898 Creates a new function object, sets its *__closure__* slot, and pushes it on
Andrew Svetlova5c43092012-11-23 15:28:34 +0200899 the stack. TOS is the :term:`qualified name` of the function, TOS1 is the
900 code associated with the function, and TOS2 is the tuple containing cells for
901 the closure's free variables. The function also has *argc* default parameters,
902 which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000903
904
905.. opcode:: BUILD_SLICE (argc)
906
907 .. index:: builtin: slice
908
909 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
910 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000911 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000912
913
914.. opcode:: EXTENDED_ARG (ext)
915
916 Prefixes any opcode which has an argument too big to fit into the default two
917 bytes. *ext* holds two additional bytes which, taken together with the
918 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
919 most-significant bytes.
920
921
922.. opcode:: CALL_FUNCTION_VAR (argc)
923
924 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
925 on the stack contains the variable argument list, followed by keyword and
926 positional arguments.
927
928
929.. opcode:: CALL_FUNCTION_KW (argc)
930
931 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
932 on the stack contains the keyword arguments dictionary, followed by explicit
933 keyword and positional arguments.
934
935
936.. opcode:: CALL_FUNCTION_VAR_KW (argc)
937
938 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
939 element on the stack contains the keyword arguments dictionary, followed by the
940 variable-arguments tuple, followed by explicit keyword and positional arguments.
941
942
Georg Brandl4833e5b2010-07-03 10:41:33 +0000943.. opcode:: HAVE_ARGUMENT
Georg Brandl116aa622007-08-15 14:28:22 +0000944
945 This is not really an opcode. It identifies the dividing line between opcodes
946 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
947 HAVE_ARGUMENT``.
948
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000949.. _opcode_collections:
950
951Opcode collections
952------------------
953
954These collections are provided for automatic introspection of bytecode
955instructions:
956
957.. data:: opname
958
959 Sequence of operation names, indexable using the bytecode.
960
961
962.. data:: opmap
963
964 Dictionary mapping operation names to bytecodes.
965
966
967.. data:: cmp_op
968
969 Sequence of all compare operation names.
970
971
972.. data:: hasconst
973
974 Sequence of bytecodes that have a constant parameter.
975
976
977.. data:: hasfree
978
979 Sequence of bytecodes that access a free variable (note that 'free' in
980 this context refers to names in the current scope that are referenced by
981 inner scopes or names in outer scopes that are referenced from this scope.
982 It does *not* include references to global or builtin scopes).
983
984
985.. data:: hasname
986
987 Sequence of bytecodes that access an attribute by name.
988
989
990.. data:: hasjrel
991
992 Sequence of bytecodes that have a relative jump target.
993
994
995.. data:: hasjabs
996
997 Sequence of bytecodes that have an absolute jump target.
998
999
1000.. data:: haslocal
1001
1002 Sequence of bytecodes that access a local variable.
1003
1004
1005.. data:: hascompare
1006
1007 Sequence of bytecodes of Boolean operations.