blob: 535b36efbf2ec8c848d87b67046813548611df61 [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
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050012disassembling it. The CPython bytecode which this module takes as an input is
13defined in the file :file:`Include/opcode.h` and used by the compiler and the
14interpreter.
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
Ivan Levkivskyi8f9e1bbf2017-03-24 22:05:04 +010023 .. versionchanged:: 3.6
24 Use 2 bytes for each instruction. Previously the number of bytes varied
25 by instruction.
26
Brett Cannon8315fd12010-07-02 22:03:00 +000027
Georg Brandl116aa622007-08-15 14:28:22 +000028Example: Given the function :func:`myfunc`::
29
30 def myfunc(alist):
31 return len(alist)
32
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100033the following command can be used to display the disassembly of
34:func:`myfunc`::
Georg Brandl116aa622007-08-15 14:28:22 +000035
36 >>> dis.dis(myfunc)
37 2 0 LOAD_GLOBAL 0 (len)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030038 2 LOAD_FAST 0 (alist)
39 4 CALL_FUNCTION 1
40 6 RETURN_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +000041
42(The "2" is a line number).
43
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100044Bytecode analysis
45-----------------
Georg Brandl116aa622007-08-15 14:28:22 +000046
R David Murray0bce6e72014-01-07 14:30:17 -050047.. versionadded:: 3.4
48
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100049The bytecode analysis API allows pieces of Python code to be wrapped in a
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050050:class:`Bytecode` object that provides easy access to details of the compiled
51code.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100052
Nick Coghlan50c48b82013-11-23 00:57:00 +100053.. class:: Bytecode(x, *, first_line=None, current_offset=None)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100054
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100055
syncosmicfe2b56a2017-08-17 19:29:21 -070056 Analyse the bytecode corresponding to a function, generator, asynchronous
57 generator, coroutine, method, string of source code, or a code object (as
58 returned by :func:`compile`).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100059
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050060 This is a convenience wrapper around many of the functions listed below, most
61 notably :func:`get_instructions`, as iterating over a :class:`Bytecode`
62 instance yields the bytecode operations as :class:`Instruction` instances.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100063
Serhiy Storchakaecf41da2016-10-19 16:29:26 +030064 If *first_line* is not ``None``, it indicates the line number that should be
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050065 reported for the first source line in the disassembled code. Otherwise, the
66 source line information (if any) is taken directly from the disassembled code
67 object.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100068
Serhiy Storchakaecf41da2016-10-19 16:29:26 +030069 If *current_offset* is not ``None``, it refers to an instruction offset in the
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050070 disassembled code. Setting this means :meth:`.dis` will display a "current
71 instruction" marker against the specified opcode.
Nick Coghlan50c48b82013-11-23 00:57:00 +100072
73 .. classmethod:: from_traceback(tb)
74
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050075 Construct a :class:`Bytecode` instance from the given traceback, setting
76 *current_offset* to the instruction responsible for the exception.
Nick Coghlan50c48b82013-11-23 00:57:00 +100077
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100078 .. data:: codeobj
79
80 The compiled code object.
81
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100082 .. data:: first_line
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100083
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100084 The first source line of the code object (if available)
85
86 .. method:: dis()
87
Benjamin Peterson29fec922015-03-02 09:27:43 -050088 Return a formatted view of the bytecode operations (the same as printed by
89 :func:`dis.dis`, but returned as a multi-line string).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100090
91 .. method:: info()
92
93 Return a formatted multi-line string with detailed information about the
94 code object, like :func:`code_info`.
95
syncosmicfe2b56a2017-08-17 19:29:21 -070096 .. versionchanged:: 3.7
97 This can now handle coroutine and asynchronous generator objects.
98
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100099Example::
100
101 >>> bytecode = dis.Bytecode(myfunc)
102 >>> for instr in bytecode:
103 ... print(instr.opname)
104 ...
105 LOAD_GLOBAL
106 LOAD_FAST
107 CALL_FUNCTION
108 RETURN_VALUE
109
110
111Analysis functions
112------------------
113
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500114The :mod:`dis` module also defines the following analysis functions that convert
115the input directly to the desired output. They can be useful if only a single
116operation is being performed, so the intermediate analysis object isn't useful:
Georg Brandl116aa622007-08-15 14:28:22 +0000117
Nick Coghlane8814fb2010-09-10 14:08:04 +0000118.. function:: code_info(x)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000119
Georg Brandl67b21b72010-08-17 15:07:14 +0000120 Return a formatted multi-line string with detailed code object information
syncosmicfe2b56a2017-08-17 19:29:21 -0700121 for the supplied function, generator, asynchronous generator, coroutine,
122 method, source code string or code object.
Nick Coghlaneae2da12010-08-17 08:03:36 +0000123
Georg Brandl67b21b72010-08-17 15:07:14 +0000124 Note that the exact contents of code info strings are highly implementation
125 dependent and they may change arbitrarily across Python VMs or Python
126 releases.
Nick Coghlaneae2da12010-08-17 08:03:36 +0000127
128 .. versionadded:: 3.2
129
syncosmicfe2b56a2017-08-17 19:29:21 -0700130 .. versionchanged:: 3.7
131 This can now handle coroutine and asynchronous generator objects.
132
Georg Brandl67b21b72010-08-17 15:07:14 +0000133
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000134.. function:: show_code(x, *, file=None)
Nick Coghlane8814fb2010-09-10 14:08:04 +0000135
136 Print detailed code object information for the supplied function, method,
Ezio Melotti6e6c6ac2013-08-23 22:41:39 +0300137 source code string or code object to *file* (or ``sys.stdout`` if *file*
138 is not specified).
Nick Coghlane8814fb2010-09-10 14:08:04 +0000139
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000140 This is a convenient shorthand for ``print(code_info(x), file=file)``,
141 intended for interactive exploration at the interpreter prompt.
Nick Coghlane8814fb2010-09-10 14:08:04 +0000142
143 .. versionadded:: 3.2
144
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000145 .. versionchanged:: 3.4
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200146 Added *file* parameter.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000147
148
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300149.. function:: dis(x=None, *, file=None, depth=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Georg Brandl67b21b72010-08-17 15:07:14 +0000151 Disassemble the *x* object. *x* can denote either a module, a class, a
syncosmicfe2b56a2017-08-17 19:29:21 -0700152 method, a function, a generator, an asynchronous generator, a couroutine,
153 a code object, a string of source code or a byte sequence of raw bytecode.
154 For a module, it disassembles all functions. For a class, it disassembles
155 all methods (including class and static methods). For a code object or
156 sequence of raw bytecode, it prints one line per bytecode instruction.
157 It also recursively disassembles nested code objects (the code of
158 comprehensions, generator expressions and nested functions, and the code
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300159 used for building nested classes).
160 Strings are first compiled to code objects with the :func:`compile`
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300161 built-in function before being disassembled. If no object is provided, this
162 function disassembles the last traceback.
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200164 The disassembly is written as text to the supplied *file* argument if
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000165 provided and to ``sys.stdout`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000166
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300167 The maximal depth of recursion is limited by *depth* unless it is ``None``.
168 ``depth=0`` means no recursion.
169
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000170 .. versionchanged:: 3.4
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200171 Added *file* parameter.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000172
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300173 .. versionchanged:: 3.7
174 Implemented recursive disassembling and added *depth* parameter.
175
syncosmicfe2b56a2017-08-17 19:29:21 -0700176 .. versionchanged:: 3.7
177 This can now handle coroutine and asynchronous generator objects.
178
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000179
180.. function:: distb(tb=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000181
Georg Brandl4833e5b2010-07-03 10:41:33 +0000182 Disassemble the top-of-stack function of a traceback, using the last
183 traceback if none was passed. The instruction causing the exception is
184 indicated.
Georg Brandl116aa622007-08-15 14:28:22 +0000185
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200186 The disassembly is written as text to the supplied *file* argument if
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000187 provided and to ``sys.stdout`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000188
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000189 .. versionchanged:: 3.4
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200190 Added *file* parameter.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000191
192
193.. function:: disassemble(code, lasti=-1, *, file=None)
194 disco(code, lasti=-1, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000195
Georg Brandl4833e5b2010-07-03 10:41:33 +0000196 Disassemble a code object, indicating the last instruction if *lasti* was
Georg Brandl116aa622007-08-15 14:28:22 +0000197 provided. The output is divided in the following columns:
198
199 #. the line number, for the first instruction of each line
200 #. the current instruction, indicated as ``-->``,
201 #. a labelled instruction, indicated with ``>>``,
202 #. the address of the instruction,
203 #. the operation code name,
204 #. operation parameters, and
205 #. interpretation of the parameters in parentheses.
206
207 The parameter interpretation recognizes local and global variable names,
208 constant values, branch targets, and compare operators.
209
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200210 The disassembly is written as text to the supplied *file* argument if
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000211 provided and to ``sys.stdout`` otherwise.
212
213 .. versionchanged:: 3.4
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200214 Added *file* parameter.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000215
216
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000217.. function:: get_instructions(x, *, first_line=None)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000218
219 Return an iterator over the instructions in the supplied function, method,
220 source code string or code object.
221
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500222 The iterator generates a series of :class:`Instruction` named tuples giving
223 the details of each operation in the supplied code.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000224
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300225 If *first_line* is not ``None``, it indicates the line number that should be
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500226 reported for the first source line in the disassembled code. Otherwise, the
227 source line information (if any) is taken directly from the disassembled code
228 object.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000229
230 .. versionadded:: 3.4
231
Georg Brandl116aa622007-08-15 14:28:22 +0000232
Benjamin Peterson75edad02009-01-01 15:05:06 +0000233.. function:: findlinestarts(code)
234
235 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
236 attributes of the code object *code* to find the offsets which are starts of
237 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
Ivan Levkivskyi8f9e1bbf2017-03-24 22:05:04 +0100238 See :source:`Objects/lnotab_notes.txt` for the ``co_lnotab`` format and
239 how to decode it.
240
241 .. versionchanged:: 3.6
242 Line numbers can be decreasing. Before, they were always increasing.
Benjamin Peterson75edad02009-01-01 15:05:06 +0000243
244
245.. function:: findlabels(code)
246
247 Detect all offsets in the code object *code* which are jump targets, and
248 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +0000249
Larry Hastings3a907972013-11-23 14:49:22 -0800250
251.. function:: stack_effect(opcode, [oparg])
252
253 Compute the stack effect of *opcode* with argument *oparg*.
254
255 .. versionadded:: 3.4
256
Georg Brandl116aa622007-08-15 14:28:22 +0000257.. _bytecodes:
258
Georg Brandl9afde1c2007-11-01 20:32:30 +0000259Python Bytecode Instructions
260----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000261
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000262The :func:`get_instructions` function and :class:`Bytecode` class provide
263details of bytecode instructions as :class:`Instruction` instances:
264
265.. class:: Instruction
266
267 Details for a bytecode operation
268
269 .. data:: opcode
270
271 numeric code for operation, corresponding to the opcode values listed
272 below and the bytecode values in the :ref:`opcode_collections`.
273
274
275 .. data:: opname
276
277 human readable name for operation
278
279
280 .. data:: arg
281
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300282 numeric argument to operation (if any), otherwise ``None``
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000283
284
285 .. data:: argval
286
287 resolved arg value (if known), otherwise same as arg
288
289
290 .. data:: argrepr
291
292 human readable description of operation argument
293
294
295 .. data:: offset
296
297 start index of operation within bytecode sequence
298
299
300 .. data:: starts_line
301
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300302 line started by this opcode (if any), otherwise ``None``
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000303
304
305 .. data:: is_jump_target
306
Serhiy Storchaka0e90e992013-11-29 12:19:53 +0200307 ``True`` if other code jumps to here, otherwise ``False``
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000308
309 .. versionadded:: 3.4
310
311
Georg Brandl9afde1c2007-11-01 20:32:30 +0000312The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000313
314
Georg Brandl4833e5b2010-07-03 10:41:33 +0000315**General instructions**
316
Georg Brandl4833e5b2010-07-03 10:41:33 +0000317.. opcode:: NOP
Georg Brandl116aa622007-08-15 14:28:22 +0000318
319 Do nothing code. Used as a placeholder by the bytecode optimizer.
320
321
Georg Brandl4833e5b2010-07-03 10:41:33 +0000322.. opcode:: POP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000323
324 Removes the top-of-stack (TOS) item.
325
326
Georg Brandl4833e5b2010-07-03 10:41:33 +0000327.. opcode:: ROT_TWO
Georg Brandl116aa622007-08-15 14:28:22 +0000328
329 Swaps the two top-most stack items.
330
331
Georg Brandl4833e5b2010-07-03 10:41:33 +0000332.. opcode:: ROT_THREE
Georg Brandl116aa622007-08-15 14:28:22 +0000333
334 Lifts second and third stack item one position up, moves top down to position
335 three.
336
337
Georg Brandl4833e5b2010-07-03 10:41:33 +0000338.. opcode:: DUP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000339
340 Duplicates the reference on top of the stack.
341
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200342 .. versionadded:: 3.2
343
Georg Brandl4833e5b2010-07-03 10:41:33 +0000344
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000345.. opcode:: DUP_TOP_TWO
346
347 Duplicates the two references on top of the stack, leaving them in the
348 same order.
349
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200350 .. versionadded:: 3.2
351
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000352
Georg Brandl4833e5b2010-07-03 10:41:33 +0000353**Unary operations**
354
355Unary operations take the top of the stack, apply the operation, and push the
Georg Brandl116aa622007-08-15 14:28:22 +0000356result back on the stack.
357
Georg Brandl4833e5b2010-07-03 10:41:33 +0000358.. opcode:: UNARY_POSITIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000359
360 Implements ``TOS = +TOS``.
361
362
Georg Brandl4833e5b2010-07-03 10:41:33 +0000363.. opcode:: UNARY_NEGATIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000364
365 Implements ``TOS = -TOS``.
366
367
Georg Brandl4833e5b2010-07-03 10:41:33 +0000368.. opcode:: UNARY_NOT
Georg Brandl116aa622007-08-15 14:28:22 +0000369
370 Implements ``TOS = not TOS``.
371
372
Georg Brandl4833e5b2010-07-03 10:41:33 +0000373.. opcode:: UNARY_INVERT
Georg Brandl116aa622007-08-15 14:28:22 +0000374
375 Implements ``TOS = ~TOS``.
376
377
Georg Brandl4833e5b2010-07-03 10:41:33 +0000378.. opcode:: GET_ITER
Georg Brandl116aa622007-08-15 14:28:22 +0000379
380 Implements ``TOS = iter(TOS)``.
381
Georg Brandl4833e5b2010-07-03 10:41:33 +0000382
Yury Selivanov5376ba92015-06-22 12:19:30 -0400383.. opcode:: GET_YIELD_FROM_ITER
384
385 If ``TOS`` is a :term:`generator iterator` or :term:`coroutine` object
386 it is left as is. Otherwise, implements ``TOS = iter(TOS)``.
387
388 .. versionadded:: 3.5
389
390
Georg Brandl4833e5b2010-07-03 10:41:33 +0000391**Binary operations**
392
Georg Brandl116aa622007-08-15 14:28:22 +0000393Binary operations remove the top of the stack (TOS) and the second top-most
394stack item (TOS1) from the stack. They perform the operation, and put the
395result back on the stack.
396
Georg Brandl4833e5b2010-07-03 10:41:33 +0000397.. opcode:: BINARY_POWER
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_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000403
404 Implements ``TOS = TOS1 * TOS``.
405
406
Benjamin Petersond51374e2014-04-09 23:55:56 -0400407.. opcode:: BINARY_MATRIX_MULTIPLY
408
409 Implements ``TOS = TOS1 @ TOS``.
410
Berker Peksagda0870c2015-03-12 20:56:45 +0200411 .. versionadded:: 3.5
412
Benjamin Petersond51374e2014-04-09 23:55:56 -0400413
Georg Brandl4833e5b2010-07-03 10:41:33 +0000414.. opcode:: BINARY_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000415
416 Implements ``TOS = TOS1 // TOS``.
417
418
Georg Brandl4833e5b2010-07-03 10:41:33 +0000419.. opcode:: BINARY_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000420
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000421 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000422
423
Georg Brandl4833e5b2010-07-03 10:41:33 +0000424.. opcode:: BINARY_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000425
426 Implements ``TOS = TOS1 % TOS``.
427
428
Georg Brandl4833e5b2010-07-03 10:41:33 +0000429.. opcode:: BINARY_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000430
431 Implements ``TOS = TOS1 + TOS``.
432
433
Georg Brandl4833e5b2010-07-03 10:41:33 +0000434.. opcode:: BINARY_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000435
436 Implements ``TOS = TOS1 - TOS``.
437
438
Georg Brandl4833e5b2010-07-03 10:41:33 +0000439.. opcode:: BINARY_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000440
441 Implements ``TOS = TOS1[TOS]``.
442
443
Georg Brandl4833e5b2010-07-03 10:41:33 +0000444.. opcode:: BINARY_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000445
446 Implements ``TOS = TOS1 << TOS``.
447
448
Georg Brandl4833e5b2010-07-03 10:41:33 +0000449.. opcode:: BINARY_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000450
451 Implements ``TOS = TOS1 >> TOS``.
452
453
Georg Brandl4833e5b2010-07-03 10:41:33 +0000454.. opcode:: BINARY_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000455
456 Implements ``TOS = TOS1 & TOS``.
457
458
Georg Brandl4833e5b2010-07-03 10:41:33 +0000459.. opcode:: BINARY_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000460
461 Implements ``TOS = TOS1 ^ TOS``.
462
463
Georg Brandl4833e5b2010-07-03 10:41:33 +0000464.. opcode:: BINARY_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000465
466 Implements ``TOS = TOS1 | TOS``.
467
Georg Brandl4833e5b2010-07-03 10:41:33 +0000468
469**In-place operations**
470
Georg Brandl116aa622007-08-15 14:28:22 +0000471In-place operations are like binary operations, in that they remove TOS and
472TOS1, and push the result back on the stack, but the operation is done in-place
473when TOS1 supports it, and the resulting TOS may be (but does not have to be)
474the original TOS1.
475
Georg Brandl4833e5b2010-07-03 10:41:33 +0000476.. opcode:: INPLACE_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000477
478 Implements in-place ``TOS = TOS1 ** TOS``.
479
480
Georg Brandl4833e5b2010-07-03 10:41:33 +0000481.. opcode:: INPLACE_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000482
483 Implements in-place ``TOS = TOS1 * TOS``.
484
485
Benjamin Petersond51374e2014-04-09 23:55:56 -0400486.. opcode:: INPLACE_MATRIX_MULTIPLY
487
488 Implements in-place ``TOS = TOS1 @ TOS``.
489
Berker Peksagda0870c2015-03-12 20:56:45 +0200490 .. versionadded:: 3.5
491
Benjamin Petersond51374e2014-04-09 23:55:56 -0400492
Georg Brandl4833e5b2010-07-03 10:41:33 +0000493.. opcode:: INPLACE_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000494
495 Implements in-place ``TOS = TOS1 // TOS``.
496
497
Georg Brandl4833e5b2010-07-03 10:41:33 +0000498.. opcode:: INPLACE_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000499
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000500 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000501
502
Georg Brandl4833e5b2010-07-03 10:41:33 +0000503.. opcode:: INPLACE_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000504
505 Implements in-place ``TOS = TOS1 % TOS``.
506
507
Georg Brandl4833e5b2010-07-03 10:41:33 +0000508.. opcode:: INPLACE_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000509
510 Implements in-place ``TOS = TOS1 + TOS``.
511
512
Georg Brandl4833e5b2010-07-03 10:41:33 +0000513.. opcode:: INPLACE_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000514
515 Implements in-place ``TOS = TOS1 - TOS``.
516
517
Georg Brandl4833e5b2010-07-03 10:41:33 +0000518.. opcode:: INPLACE_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000519
520 Implements in-place ``TOS = TOS1 << TOS``.
521
522
Georg Brandl4833e5b2010-07-03 10:41:33 +0000523.. opcode:: INPLACE_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000524
525 Implements in-place ``TOS = TOS1 >> TOS``.
526
527
Georg Brandl4833e5b2010-07-03 10:41:33 +0000528.. opcode:: INPLACE_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000529
530 Implements in-place ``TOS = TOS1 & TOS``.
531
532
Georg Brandl4833e5b2010-07-03 10:41:33 +0000533.. opcode:: INPLACE_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000534
535 Implements in-place ``TOS = TOS1 ^ TOS``.
536
537
Georg Brandl4833e5b2010-07-03 10:41:33 +0000538.. opcode:: INPLACE_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000539
540 Implements in-place ``TOS = TOS1 | TOS``.
541
Georg Brandl116aa622007-08-15 14:28:22 +0000542
Georg Brandl4833e5b2010-07-03 10:41:33 +0000543.. opcode:: STORE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000544
545 Implements ``TOS1[TOS] = TOS2``.
546
547
Georg Brandl4833e5b2010-07-03 10:41:33 +0000548.. opcode:: DELETE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000549
550 Implements ``del TOS1[TOS]``.
551
Georg Brandl116aa622007-08-15 14:28:22 +0000552
Yury Selivanov66f88282015-06-24 11:04:15 -0400553**Coroutine opcodes**
Yury Selivanov75445082015-05-11 22:57:16 -0400554
555.. opcode:: GET_AWAITABLE
556
Yury Selivanov66f88282015-06-24 11:04:15 -0400557 Implements ``TOS = get_awaitable(TOS)``, where ``get_awaitable(o)``
558 returns ``o`` if ``o`` is a coroutine object or a generator object with
559 the CO_ITERABLE_COROUTINE flag, or resolves
560 ``o.__await__``.
Yury Selivanov75445082015-05-11 22:57:16 -0400561
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200562 .. versionadded:: 3.5
563
Yury Selivanov75445082015-05-11 22:57:16 -0400564
565.. opcode:: GET_AITER
566
Yury Selivanov02e82a02017-10-06 10:18:10 -0400567 Implements ``TOS = TOS.__aiter__()``.
568
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200569 .. versionadded:: 3.5
Yury Selivanov02e82a02017-10-06 10:18:10 -0400570 .. versionchanged:: 3.7
571 Returning awaitable objects from ``__aiter__`` is no longer
572 supported.
Yury Selivanov75445082015-05-11 22:57:16 -0400573
574
575.. opcode:: GET_ANEXT
576
577 Implements ``PUSH(get_awaitable(TOS.__anext__()))``. See ``GET_AWAITABLE``
578 for details about ``get_awaitable``
579
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200580 .. versionadded:: 3.5
581
Yury Selivanov75445082015-05-11 22:57:16 -0400582
583.. opcode:: BEFORE_ASYNC_WITH
584
585 Resolves ``__aenter__`` and ``__aexit__`` from the object on top of the
586 stack. Pushes ``__aexit__`` and result of ``__aenter__()`` to the stack.
587
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200588 .. versionadded:: 3.5
589
Yury Selivanov75445082015-05-11 22:57:16 -0400590
591.. opcode:: SETUP_ASYNC_WITH
592
593 Creates a new frame object.
594
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200595 .. versionadded:: 3.5
596
Yury Selivanov75445082015-05-11 22:57:16 -0400597
598
Georg Brandl4833e5b2010-07-03 10:41:33 +0000599**Miscellaneous opcodes**
Georg Brandl116aa622007-08-15 14:28:22 +0000600
Georg Brandl4833e5b2010-07-03 10:41:33 +0000601.. opcode:: PRINT_EXPR
Georg Brandl116aa622007-08-15 14:28:22 +0000602
603 Implements the expression statement for the interactive mode. TOS is removed
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500604 from the stack and printed. In non-interactive mode, an expression statement
605 is terminated with :opcode:`POP_TOP`.
Georg Brandl116aa622007-08-15 14:28:22 +0000606
607
Georg Brandl4833e5b2010-07-03 10:41:33 +0000608.. opcode:: BREAK_LOOP
Georg Brandl116aa622007-08-15 14:28:22 +0000609
610 Terminates a loop due to a :keyword:`break` statement.
611
612
613.. opcode:: CONTINUE_LOOP (target)
614
615 Continues a loop due to a :keyword:`continue` statement. *target* is the
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200616 address to jump to (which should be a :opcode:`FOR_ITER` instruction).
Georg Brandl116aa622007-08-15 14:28:22 +0000617
618
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000619.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000620
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000621 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000622
623
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000624.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000625
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000626 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
627
628
629.. opcode:: MAP_ADD (i)
630
631 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
632 comprehensions.
633
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200634 .. versionadded:: 3.1
635
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200636For all of the :opcode:`SET_ADD`, :opcode:`LIST_APPEND` and :opcode:`MAP_ADD`
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500637instructions, while the added value or key/value pair is popped off, the
638container object remains on the stack so that it is available for further
639iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000640
641
Georg Brandl4833e5b2010-07-03 10:41:33 +0000642.. opcode:: RETURN_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000643
644 Returns with TOS to the caller of the function.
645
646
Georg Brandl4833e5b2010-07-03 10:41:33 +0000647.. opcode:: YIELD_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000648
Berker Peksagab4040e2015-03-02 06:33:30 +0200649 Pops TOS and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000650
651
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000652.. opcode:: YIELD_FROM
653
Berker Peksagab4040e2015-03-02 06:33:30 +0200654 Pops TOS and delegates to it as a subiterator from a :term:`generator`.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000655
656 .. versionadded:: 3.3
657
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200658
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700659.. opcode:: SETUP_ANNOTATIONS
660
661 Checks whether ``__annotations__`` is defined in ``locals()``, if not it is
Martin Panterb1321fb2016-10-10 00:38:21 +0000662 set up to an empty ``dict``. This opcode is only emitted if a class
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700663 or module body contains :term:`variable annotations <variable annotation>`
664 statically.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000665
Berker Peksag34b74ff2016-09-12 08:00:01 +0300666 .. versionadded:: 3.6
667
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200668
Georg Brandl4833e5b2010-07-03 10:41:33 +0000669.. opcode:: IMPORT_STAR
Georg Brandl116aa622007-08-15 14:28:22 +0000670
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500671 Loads all symbols not starting with ``'_'`` directly from the module TOS to
672 the local namespace. The module is popped after loading all names. This
673 opcode implements ``from module import *``.
Georg Brandl116aa622007-08-15 14:28:22 +0000674
675
Georg Brandl4833e5b2010-07-03 10:41:33 +0000676.. opcode:: POP_BLOCK
Georg Brandl116aa622007-08-15 14:28:22 +0000677
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500678 Removes one block from the block stack. Per frame, there is a stack of
679 blocks, denoting nested loops, try statements, and such.
Georg Brandl116aa622007-08-15 14:28:22 +0000680
681
Georg Brandl4833e5b2010-07-03 10:41:33 +0000682.. opcode:: POP_EXCEPT
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000683
684 Removes one block from the block stack. The popped block must be an exception
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500685 handler block, as implicitly created when entering an except handler. In
686 addition to popping extraneous values from the frame stack, the last three
687 popped values are used to restore the exception state.
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000688
689
Georg Brandl4833e5b2010-07-03 10:41:33 +0000690.. opcode:: END_FINALLY
Georg Brandl116aa622007-08-15 14:28:22 +0000691
692 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
693 exception has to be re-raised, or whether the function returns, and continues
694 with the outer-next block.
695
696
Georg Brandl4833e5b2010-07-03 10:41:33 +0000697.. opcode:: LOAD_BUILD_CLASS
Georg Brandl116aa622007-08-15 14:28:22 +0000698
Georg Brandl5ac22302008-07-20 21:39:03 +0000699 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200700 by :opcode:`CALL_FUNCTION` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000701
Guido van Rossum04110fb2007-08-24 16:32:05 +0000702
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000703.. opcode:: SETUP_WITH (delta)
704
705 This opcode performs several operations before a with block starts. First,
706 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
707 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
708 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
709 is pushed. Finally, the result of calling the enter method is pushed onto
710 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
711 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
712 :opcode:`UNPACK_SEQUENCE`).
713
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200714 .. versionadded:: 3.2
715
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000716
Yury Selivanov75445082015-05-11 22:57:16 -0400717.. opcode:: WITH_CLEANUP_START
Guido van Rossum04110fb2007-08-24 16:32:05 +0000718
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500719 Cleans up the stack when a :keyword:`with` statement block exits. TOS is the
720 context manager's :meth:`__exit__` bound method. Below TOS are 1--3 values
721 indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000722
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000723 * SECOND = ``None``
724 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
725 * SECOND = ``WHY_*``; no retval below it
726 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000727
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000728 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
Yury Selivanov75445082015-05-11 22:57:16 -0400729 ``TOS(None, None, None)``. Pushes SECOND and result of the call
730 to the stack.
731
732
733.. opcode:: WITH_CLEANUP_FINISH
734
735 Pops exception type and result of 'exit' function call from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000736
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500737 If the stack represents an exception, *and* the function call returns a
738 'true' value, this information is "zapped" and replaced with a single
739 ``WHY_SILENCED`` to prevent :opcode:`END_FINALLY` from re-raising the
740 exception. (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000741
Georg Brandl9afde1c2007-11-01 20:32:30 +0000742 .. XXX explain the WHY stuff!
743
Guido van Rossum04110fb2007-08-24 16:32:05 +0000744
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300745All of the following opcodes use their arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000746
Georg Brandl116aa622007-08-15 14:28:22 +0000747.. opcode:: STORE_NAME (namei)
748
749 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500750 :attr:`co_names` of the code object. The compiler tries to use
751 :opcode:`STORE_FAST` or :opcode:`STORE_GLOBAL` if possible.
Georg Brandl116aa622007-08-15 14:28:22 +0000752
753
754.. opcode:: DELETE_NAME (namei)
755
756 Implements ``del name``, where *namei* is the index into :attr:`co_names`
757 attribute of the code object.
758
759
760.. opcode:: UNPACK_SEQUENCE (count)
761
762 Unpacks TOS into *count* individual values, which are put onto the stack
763 right-to-left.
764
Georg Brandl116aa622007-08-15 14:28:22 +0000765
Georg Brandl5ac22302008-07-20 21:39:03 +0000766.. opcode:: UNPACK_EX (counts)
767
768 Implements assignment with a starred target: Unpacks an iterable in TOS into
769 individual values, where the total number of values can be smaller than the
Martin Pantercc71a792016-04-05 06:19:42 +0000770 number of items in the iterable: one of the new values will be a list of all
Georg Brandl5ac22302008-07-20 21:39:03 +0000771 leftover items.
772
773 The low byte of *counts* is the number of values before the list value, the
774 high byte of *counts* the number of values after it. The resulting values
775 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000776
Georg Brandl5ac22302008-07-20 21:39:03 +0000777
Georg Brandl116aa622007-08-15 14:28:22 +0000778.. opcode:: STORE_ATTR (namei)
779
780 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
781 :attr:`co_names`.
782
783
784.. opcode:: DELETE_ATTR (namei)
785
786 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
787
788
789.. opcode:: STORE_GLOBAL (namei)
790
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200791 Works as :opcode:`STORE_NAME`, but stores the name as a global.
Georg Brandl116aa622007-08-15 14:28:22 +0000792
793
794.. opcode:: DELETE_GLOBAL (namei)
795
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200796 Works as :opcode:`DELETE_NAME`, but deletes a global name.
Georg Brandl116aa622007-08-15 14:28:22 +0000797
Georg Brandl116aa622007-08-15 14:28:22 +0000798
799.. opcode:: LOAD_CONST (consti)
800
801 Pushes ``co_consts[consti]`` onto the stack.
802
803
804.. opcode:: LOAD_NAME (namei)
805
806 Pushes the value associated with ``co_names[namei]`` onto the stack.
807
808
809.. opcode:: BUILD_TUPLE (count)
810
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500811 Creates a tuple consuming *count* items from the stack, and pushes the
812 resulting tuple onto the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000813
814
815.. opcode:: BUILD_LIST (count)
816
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200817 Works as :opcode:`BUILD_TUPLE`, but creates a list.
Georg Brandl116aa622007-08-15 14:28:22 +0000818
819
820.. opcode:: BUILD_SET (count)
821
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200822 Works as :opcode:`BUILD_TUPLE`, but creates a set.
Georg Brandl116aa622007-08-15 14:28:22 +0000823
824
Christian Heimesa62da1d2008-01-12 19:39:10 +0000825.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000826
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100827 Pushes a new dictionary object onto the stack. Pops ``2 * count`` items
828 so that the dictionary holds *count* entries:
829 ``{..., TOS3: TOS2, TOS1: TOS}``.
830
831 .. versionchanged:: 3.5
832 The dictionary is created from stack items instead of creating an
833 empty dictionary pre-sized to hold *count* items.
Georg Brandl116aa622007-08-15 14:28:22 +0000834
835
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +0300836.. opcode:: BUILD_CONST_KEY_MAP (count)
837
838 The version of :opcode:`BUILD_MAP` specialized for constant keys. *count*
839 values are consumed from the stack. The top element on the stack contains
840 a tuple of keys.
841
842 .. versionadded:: 3.6
843
844
Serhiy Storchakaea525a22016-09-06 22:07:53 +0300845.. opcode:: BUILD_STRING (count)
846
847 Concatenates *count* strings from the stack and pushes the resulting string
848 onto the stack.
849
850 .. versionadded:: 3.6
851
852
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100853.. opcode:: BUILD_TUPLE_UNPACK (count)
854
855 Pops *count* iterables from the stack, joins them in a single tuple,
856 and pushes the result. Implements iterable unpacking in tuple
857 displays ``(*x, *y, *z)``.
858
859 .. versionadded:: 3.5
860
861
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100862.. opcode:: BUILD_TUPLE_UNPACK_WITH_CALL (count)
863
864 This is similar to :opcode:`BUILD_TUPLE_UNPACK`,
865 but is used for ``f(*x, *y, *z)`` call syntax. The stack item at position
866 ``count + 1`` should be the corresponding callable ``f``.
867
868 .. versionadded:: 3.6
869
870
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100871.. opcode:: BUILD_LIST_UNPACK (count)
872
873 This is similar to :opcode:`BUILD_TUPLE_UNPACK`, but pushes a list
874 instead of tuple. Implements iterable unpacking in list
875 displays ``[*x, *y, *z]``.
876
877 .. versionadded:: 3.5
878
879
880.. opcode:: BUILD_SET_UNPACK (count)
881
882 This is similar to :opcode:`BUILD_TUPLE_UNPACK`, but pushes a set
883 instead of tuple. Implements iterable unpacking in set
884 displays ``{*x, *y, *z}``.
885
886 .. versionadded:: 3.5
887
888
889.. opcode:: BUILD_MAP_UNPACK (count)
890
891 Pops *count* mappings from the stack, merges them into a single dictionary,
892 and pushes the result. Implements dictionary unpacking in dictionary
893 displays ``{**x, **y, **z}``.
894
895 .. versionadded:: 3.5
896
897
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100898.. opcode:: BUILD_MAP_UNPACK_WITH_CALL (count)
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100899
900 This is similar to :opcode:`BUILD_MAP_UNPACK`,
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100901 but is used for ``f(**x, **y, **z)`` call syntax. The stack item at
902 position ``count + 2`` should be the corresponding callable ``f``.
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100903
904 .. versionadded:: 3.5
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100905 .. versionchanged:: 3.6
906 The position of the callable is determined by adding 2 to the opcode
907 argument instead of encoding it in the second byte of the argument.
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100908
909
Georg Brandl116aa622007-08-15 14:28:22 +0000910.. opcode:: LOAD_ATTR (namei)
911
912 Replaces TOS with ``getattr(TOS, co_names[namei])``.
913
914
915.. opcode:: COMPARE_OP (opname)
916
917 Performs a Boolean operation. The operation name can be found in
918 ``cmp_op[opname]``.
919
920
921.. opcode:: IMPORT_NAME (namei)
922
Christian Heimesa342c012008-04-20 21:01:16 +0000923 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
924 the *fromlist* and *level* arguments of :func:`__import__`. The module
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500925 object is pushed onto the stack. The current namespace is not affected: for
926 a proper import statement, a subsequent :opcode:`STORE_FAST` instruction
Christian Heimesa342c012008-04-20 21:01:16 +0000927 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000928
929
930.. opcode:: IMPORT_FROM (namei)
931
932 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
933 resulting object is pushed onto the stack, to be subsequently stored by a
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200934 :opcode:`STORE_FAST` instruction.
Georg Brandl116aa622007-08-15 14:28:22 +0000935
936
937.. opcode:: JUMP_FORWARD (delta)
938
Georg Brandl9afde1c2007-11-01 20:32:30 +0000939 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000940
941
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000942.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000943
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000944 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000945
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200946 .. versionadded:: 3.1
947
Georg Brandl116aa622007-08-15 14:28:22 +0000948
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000949.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000950
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000951 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
952
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200953 .. versionadded:: 3.1
954
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000955
956.. opcode:: JUMP_IF_TRUE_OR_POP (target)
957
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500958 If TOS is true, sets the bytecode counter to *target* and leaves TOS on the
959 stack. Otherwise (TOS is false), TOS is popped.
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000960
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200961 .. versionadded:: 3.1
962
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000963
964.. opcode:: JUMP_IF_FALSE_OR_POP (target)
965
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500966 If TOS is false, sets the bytecode counter to *target* and leaves TOS on the
967 stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000968
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200969 .. versionadded:: 3.1
970
Georg Brandl116aa622007-08-15 14:28:22 +0000971
972.. opcode:: JUMP_ABSOLUTE (target)
973
Georg Brandl9afde1c2007-11-01 20:32:30 +0000974 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000975
976
977.. opcode:: FOR_ITER (delta)
978
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500979 TOS is an :term:`iterator`. Call its :meth:`~iterator.__next__` method. If
980 this yields a new value, push it on the stack (leaving the iterator below
981 it). If the iterator indicates it is exhausted TOS is popped, and the byte
982 code counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000983
Georg Brandl116aa622007-08-15 14:28:22 +0000984
985.. opcode:: LOAD_GLOBAL (namei)
986
987 Loads the global named ``co_names[namei]`` onto the stack.
988
Georg Brandl116aa622007-08-15 14:28:22 +0000989
990.. opcode:: SETUP_LOOP (delta)
991
992 Pushes a block for a loop onto the block stack. The block spans from the
993 current instruction with a size of *delta* bytes.
994
995
996.. opcode:: SETUP_EXCEPT (delta)
997
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500998 Pushes a try block from a try-except clause onto the block stack. *delta*
999 points to the first except block.
Georg Brandl116aa622007-08-15 14:28:22 +00001000
1001
1002.. opcode:: SETUP_FINALLY (delta)
1003
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001004 Pushes a try block from a try-except clause onto the block stack. *delta*
1005 points to the finally block.
Georg Brandl116aa622007-08-15 14:28:22 +00001006
1007
1008.. opcode:: LOAD_FAST (var_num)
1009
1010 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
1011
1012
1013.. opcode:: STORE_FAST (var_num)
1014
1015 Stores TOS into the local ``co_varnames[var_num]``.
1016
1017
1018.. opcode:: DELETE_FAST (var_num)
1019
1020 Deletes local ``co_varnames[var_num]``.
1021
1022
1023.. opcode:: LOAD_CLOSURE (i)
1024
1025 Pushes a reference to the cell contained in slot *i* of the cell and free
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001026 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
1027 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
Georg Brandl116aa622007-08-15 14:28:22 +00001028 len(co_cellvars)]``.
1029
1030
1031.. opcode:: LOAD_DEREF (i)
1032
1033 Loads the cell contained in slot *i* of the cell and free variable storage.
1034 Pushes a reference to the object the cell contains on the stack.
1035
1036
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001037.. opcode:: LOAD_CLASSDEREF (i)
1038
1039 Much like :opcode:`LOAD_DEREF` but first checks the locals dictionary before
1040 consulting the cell. This is used for loading free variables in class
1041 bodies.
1042
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +02001043 .. versionadded:: 3.4
1044
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001045
Georg Brandl116aa622007-08-15 14:28:22 +00001046.. opcode:: STORE_DEREF (i)
1047
1048 Stores TOS into the cell contained in slot *i* of the cell and free variable
1049 storage.
1050
1051
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001052.. opcode:: DELETE_DEREF (i)
1053
1054 Empties the cell contained in slot *i* of the cell and free variable storage.
1055 Used by the :keyword:`del` statement.
1056
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +02001057 .. versionadded:: 3.2
1058
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001059
Georg Brandl116aa622007-08-15 14:28:22 +00001060.. opcode:: RAISE_VARARGS (argc)
1061
1062 Raises an exception. *argc* indicates the number of parameters to the raise
1063 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
1064 the parameter as TOS1, and the exception as TOS.
1065
1066
1067.. opcode:: CALL_FUNCTION (argc)
1068
Ivan Levkivskyi4b2a2a42017-03-10 23:52:35 +01001069 Calls a function. *argc* indicates the number of positional arguments.
1070 The positional arguments are on the stack, with the right-most argument
1071 on top. Below the arguments, the function object to call is on the stack.
1072 Pops all function arguments, and the function itself off the stack, and
1073 pushes the return value.
1074
1075 .. versionchanged:: 3.6
1076 This opcode is used only for calls with positional arguments.
1077
1078
1079.. opcode:: CALL_FUNCTION_KW (argc)
1080
1081 Calls a function. *argc* indicates the number of arguments (positional
1082 and keyword). The top element on the stack contains a tuple of keyword
1083 argument names. Below the tuple, keyword arguments are on the stack, in
1084 the order corresponding to the tuple. Below the keyword arguments, the
1085 positional arguments are on the stack, with the right-most parameter on
1086 top. Below the arguments, the function object to call is on the stack.
1087 Pops all function arguments, and the function itself off the stack, and
1088 pushes the return value.
1089
1090 .. versionchanged:: 3.6
1091 Keyword arguments are packed in a tuple instead of a dictionary,
1092 *argc* indicates the total number of arguments
1093
1094
1095.. opcode:: CALL_FUNCTION_EX (flags)
1096
1097 Calls a function. The lowest bit of *flags* indicates whether the
1098 var-keyword argument is placed at the top of the stack. Below the
1099 var-keyword argument, the var-positional argument is on the stack.
1100 Below the arguments, the function object to call is placed.
1101 Pops all function arguments, and the function itself off the stack, and
1102 pushes the return value. Note that this opcode pops at most three items
1103 from the stack. Var-positional and var-keyword arguments are packed
Moses Koledoye0c716532017-11-18 23:49:15 +01001104 by :opcode:`BUILD_TUPLE_UNPACK_WITH_CALL` and
Ivan Levkivskyi4b2a2a42017-03-10 23:52:35 +01001105 :opcode:`BUILD_MAP_UNPACK_WITH_CALL`.
1106
1107 .. versionadded:: 3.6
Georg Brandl116aa622007-08-15 14:28:22 +00001108
1109
INADA Naoki015bce62017-01-16 17:23:30 +09001110.. opcode:: LOAD_METHOD (namei)
1111
1112 Loads a method named ``co_names[namei]`` from TOS object. TOS is popped and
1113 method and TOS are pushed when interpreter can call unbound method directly.
Ivan Levkivskyi4b2a2a42017-03-10 23:52:35 +01001114 TOS will be used as the first argument (``self``) by :opcode:`CALL_METHOD`.
INADA Naoki015bce62017-01-16 17:23:30 +09001115 Otherwise, ``NULL`` and method is pushed (method is bound method or
1116 something else).
1117
1118 .. versionadded:: 3.7
1119
1120
1121.. opcode:: CALL_METHOD (argc)
1122
1123 Calls a method. *argc* is number of positional arguments.
1124 Keyword arguments are not supported. This opcode is designed to be used
1125 with :opcode:`LOAD_METHOD`. Positional arguments are on top of the stack.
1126 Below them, two items described in :opcode:`LOAD_METHOD` on the stack.
1127 All of them are popped and return value is pushed.
1128
1129 .. versionadded:: 3.7
1130
1131
Georg Brandl116aa622007-08-15 14:28:22 +00001132.. opcode:: MAKE_FUNCTION (argc)
1133
Georg Brandlc96ef1f2013-10-12 18:41:18 +02001134 Pushes a new function object on the stack. From bottom to top, the consumed
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001135 stack must consist of values if the argument carries a specified flag value
Georg Brandlc96ef1f2013-10-12 18:41:18 +02001136
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001137 * ``0x01`` a tuple of default argument objects in positional order
1138 * ``0x02`` a dictionary of keyword-only parameters' default values
1139 * ``0x04`` an annotation dictionary
1140 * ``0x08`` a tuple containing cells for free variables, making a closure
Georg Brandlc96ef1f2013-10-12 18:41:18 +02001141 * the code associated with the function (at TOS1)
1142 * the :term:`qualified name` of the function (at TOS)
Georg Brandl116aa622007-08-15 14:28:22 +00001143
1144
Georg Brandl116aa622007-08-15 14:28:22 +00001145.. opcode:: BUILD_SLICE (argc)
1146
1147 .. index:: builtin: slice
1148
1149 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
1150 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001151 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +00001152
1153
1154.. opcode:: EXTENDED_ARG (ext)
1155
1156 Prefixes any opcode which has an argument too big to fit into the default two
1157 bytes. *ext* holds two additional bytes which, taken together with the
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001158 subsequent opcode's argument, comprise a four-byte argument, *ext* being the
1159 two most-significant bytes.
Georg Brandl116aa622007-08-15 14:28:22 +00001160
1161
Eric V. Smith281d5322015-11-03 13:09:01 -05001162.. opcode:: FORMAT_VALUE (flags)
1163
1164 Used for implementing formatted literal strings (f-strings). Pops
1165 an optional *fmt_spec* from the stack, then a required *value*.
1166 *flags* is interpreted as follows:
1167
Eric V. Smith9ce52e32015-11-03 16:30:49 -05001168 * ``(flags & 0x03) == 0x00``: *value* is formatted as-is.
Eric V. Smith281d5322015-11-03 13:09:01 -05001169 * ``(flags & 0x03) == 0x01``: call :func:`str` on *value* before
1170 formatting it.
1171 * ``(flags & 0x03) == 0x02``: call :func:`repr` on *value* before
1172 formatting it.
1173 * ``(flags & 0x03) == 0x03``: call :func:`ascii` on *value* before
1174 formatting it.
1175 * ``(flags & 0x04) == 0x04``: pop *fmt_spec* from the stack and use
1176 it, else use an empty *fmt_spec*.
1177
Eric V. Smitha3a3d732015-11-04 07:11:13 -05001178 Formatting is performed using :c:func:`PyObject_Format`. The
1179 result is pushed on the stack.
Eric V. Smith281d5322015-11-03 13:09:01 -05001180
Eric V. Smith9ce52e32015-11-03 16:30:49 -05001181 .. versionadded:: 3.6
1182
Eric V. Smith281d5322015-11-03 13:09:01 -05001183
Georg Brandl4833e5b2010-07-03 10:41:33 +00001184.. opcode:: HAVE_ARGUMENT
Georg Brandl116aa622007-08-15 14:28:22 +00001185
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001186 This is not really an opcode. It identifies the dividing line between
Ivan Levkivskyi8f9e1bbf2017-03-24 22:05:04 +01001187 opcodes which don't use their argument and those that do
1188 (``< HAVE_ARGUMENT`` and ``>= HAVE_ARGUMENT``, respectively).
1189
1190 .. versionchanged:: 3.6
1191 Now every instruction has an argument, but opcodes ``< HAVE_ARGUMENT``
1192 ignore it. Before, only opcodes ``>= HAVE_ARGUMENT`` had an argument.
1193
Georg Brandl116aa622007-08-15 14:28:22 +00001194
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001195.. _opcode_collections:
1196
1197Opcode collections
1198------------------
1199
1200These collections are provided for automatic introspection of bytecode
1201instructions:
1202
1203.. data:: opname
1204
1205 Sequence of operation names, indexable using the bytecode.
1206
1207
1208.. data:: opmap
1209
1210 Dictionary mapping operation names to bytecodes.
1211
1212
1213.. data:: cmp_op
1214
1215 Sequence of all compare operation names.
1216
1217
1218.. data:: hasconst
1219
1220 Sequence of bytecodes that have a constant parameter.
1221
1222
1223.. data:: hasfree
1224
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001225 Sequence of bytecodes that access a free variable (note that 'free' in this
1226 context refers to names in the current scope that are referenced by inner
1227 scopes or names in outer scopes that are referenced from this scope. It does
1228 *not* include references to global or builtin scopes).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001229
1230
1231.. data:: hasname
1232
1233 Sequence of bytecodes that access an attribute by name.
1234
1235
1236.. data:: hasjrel
1237
1238 Sequence of bytecodes that have a relative jump target.
1239
1240
1241.. data:: hasjabs
1242
1243 Sequence of bytecodes that have an absolute jump target.
1244
1245
1246.. data:: haslocal
1247
1248 Sequence of bytecodes that access a local variable.
1249
1250
1251.. data:: hascompare
1252
1253 Sequence of bytecodes of Boolean operations.