blob: 47f226bd1625b39935706032855ce6ed552750ec [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
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200338.. opcode:: ROT_FOUR
339
340 Lifts second, third and forth stack items one position up, moves top down
341 to position four.
342
343 .. versionadded:: 3.8
344
345
Georg Brandl4833e5b2010-07-03 10:41:33 +0000346.. opcode:: DUP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000347
348 Duplicates the reference on top of the stack.
349
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200350 .. versionadded:: 3.2
351
Georg Brandl4833e5b2010-07-03 10:41:33 +0000352
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000353.. opcode:: DUP_TOP_TWO
354
355 Duplicates the two references on top of the stack, leaving them in the
356 same order.
357
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200358 .. versionadded:: 3.2
359
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000360
Georg Brandl4833e5b2010-07-03 10:41:33 +0000361**Unary operations**
362
363Unary operations take the top of the stack, apply the operation, and push the
Georg Brandl116aa622007-08-15 14:28:22 +0000364result back on the stack.
365
Georg Brandl4833e5b2010-07-03 10:41:33 +0000366.. opcode:: UNARY_POSITIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000367
368 Implements ``TOS = +TOS``.
369
370
Georg Brandl4833e5b2010-07-03 10:41:33 +0000371.. opcode:: UNARY_NEGATIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000372
373 Implements ``TOS = -TOS``.
374
375
Georg Brandl4833e5b2010-07-03 10:41:33 +0000376.. opcode:: UNARY_NOT
Georg Brandl116aa622007-08-15 14:28:22 +0000377
378 Implements ``TOS = not TOS``.
379
380
Georg Brandl4833e5b2010-07-03 10:41:33 +0000381.. opcode:: UNARY_INVERT
Georg Brandl116aa622007-08-15 14:28:22 +0000382
383 Implements ``TOS = ~TOS``.
384
385
Georg Brandl4833e5b2010-07-03 10:41:33 +0000386.. opcode:: GET_ITER
Georg Brandl116aa622007-08-15 14:28:22 +0000387
388 Implements ``TOS = iter(TOS)``.
389
Georg Brandl4833e5b2010-07-03 10:41:33 +0000390
Yury Selivanov5376ba92015-06-22 12:19:30 -0400391.. opcode:: GET_YIELD_FROM_ITER
392
393 If ``TOS`` is a :term:`generator iterator` or :term:`coroutine` object
394 it is left as is. Otherwise, implements ``TOS = iter(TOS)``.
395
396 .. versionadded:: 3.5
397
398
Georg Brandl4833e5b2010-07-03 10:41:33 +0000399**Binary operations**
400
Georg Brandl116aa622007-08-15 14:28:22 +0000401Binary operations remove the top of the stack (TOS) and the second top-most
402stack item (TOS1) from the stack. They perform the operation, and put the
403result back on the stack.
404
Georg Brandl4833e5b2010-07-03 10:41:33 +0000405.. opcode:: BINARY_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000406
407 Implements ``TOS = TOS1 ** TOS``.
408
409
Georg Brandl4833e5b2010-07-03 10:41:33 +0000410.. opcode:: BINARY_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000411
412 Implements ``TOS = TOS1 * TOS``.
413
414
Benjamin Petersond51374e2014-04-09 23:55:56 -0400415.. opcode:: BINARY_MATRIX_MULTIPLY
416
417 Implements ``TOS = TOS1 @ TOS``.
418
Berker Peksagda0870c2015-03-12 20:56:45 +0200419 .. versionadded:: 3.5
420
Benjamin Petersond51374e2014-04-09 23:55:56 -0400421
Georg Brandl4833e5b2010-07-03 10:41:33 +0000422.. opcode:: BINARY_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000423
424 Implements ``TOS = TOS1 // TOS``.
425
426
Georg Brandl4833e5b2010-07-03 10:41:33 +0000427.. opcode:: BINARY_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000428
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000429 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000430
431
Georg Brandl4833e5b2010-07-03 10:41:33 +0000432.. opcode:: BINARY_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000433
434 Implements ``TOS = TOS1 % TOS``.
435
436
Georg Brandl4833e5b2010-07-03 10:41:33 +0000437.. opcode:: BINARY_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000438
439 Implements ``TOS = TOS1 + TOS``.
440
441
Georg Brandl4833e5b2010-07-03 10:41:33 +0000442.. opcode:: BINARY_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000443
444 Implements ``TOS = TOS1 - TOS``.
445
446
Georg Brandl4833e5b2010-07-03 10:41:33 +0000447.. opcode:: BINARY_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000448
449 Implements ``TOS = TOS1[TOS]``.
450
451
Georg Brandl4833e5b2010-07-03 10:41:33 +0000452.. opcode:: BINARY_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000453
454 Implements ``TOS = TOS1 << TOS``.
455
456
Georg Brandl4833e5b2010-07-03 10:41:33 +0000457.. opcode:: BINARY_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000458
459 Implements ``TOS = TOS1 >> TOS``.
460
461
Georg Brandl4833e5b2010-07-03 10:41:33 +0000462.. opcode:: BINARY_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000463
464 Implements ``TOS = TOS1 & TOS``.
465
466
Georg Brandl4833e5b2010-07-03 10:41:33 +0000467.. opcode:: BINARY_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000468
469 Implements ``TOS = TOS1 ^ TOS``.
470
471
Georg Brandl4833e5b2010-07-03 10:41:33 +0000472.. opcode:: BINARY_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000473
474 Implements ``TOS = TOS1 | TOS``.
475
Georg Brandl4833e5b2010-07-03 10:41:33 +0000476
477**In-place operations**
478
Georg Brandl116aa622007-08-15 14:28:22 +0000479In-place operations are like binary operations, in that they remove TOS and
480TOS1, and push the result back on the stack, but the operation is done in-place
481when TOS1 supports it, and the resulting TOS may be (but does not have to be)
482the original TOS1.
483
Georg Brandl4833e5b2010-07-03 10:41:33 +0000484.. opcode:: INPLACE_POWER
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_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000490
491 Implements in-place ``TOS = TOS1 * TOS``.
492
493
Benjamin Petersond51374e2014-04-09 23:55:56 -0400494.. opcode:: INPLACE_MATRIX_MULTIPLY
495
496 Implements in-place ``TOS = TOS1 @ TOS``.
497
Berker Peksagda0870c2015-03-12 20:56:45 +0200498 .. versionadded:: 3.5
499
Benjamin Petersond51374e2014-04-09 23:55:56 -0400500
Georg Brandl4833e5b2010-07-03 10:41:33 +0000501.. opcode:: INPLACE_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000502
503 Implements in-place ``TOS = TOS1 // TOS``.
504
505
Georg Brandl4833e5b2010-07-03 10:41:33 +0000506.. opcode:: INPLACE_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000507
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000508 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000509
510
Georg Brandl4833e5b2010-07-03 10:41:33 +0000511.. opcode:: INPLACE_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000512
513 Implements in-place ``TOS = TOS1 % TOS``.
514
515
Georg Brandl4833e5b2010-07-03 10:41:33 +0000516.. opcode:: INPLACE_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000517
518 Implements in-place ``TOS = TOS1 + TOS``.
519
520
Georg Brandl4833e5b2010-07-03 10:41:33 +0000521.. opcode:: INPLACE_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000522
523 Implements in-place ``TOS = TOS1 - TOS``.
524
525
Georg Brandl4833e5b2010-07-03 10:41:33 +0000526.. opcode:: INPLACE_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000527
528 Implements in-place ``TOS = TOS1 << TOS``.
529
530
Georg Brandl4833e5b2010-07-03 10:41:33 +0000531.. opcode:: INPLACE_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000532
533 Implements in-place ``TOS = TOS1 >> TOS``.
534
535
Georg Brandl4833e5b2010-07-03 10:41:33 +0000536.. opcode:: INPLACE_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000537
538 Implements in-place ``TOS = TOS1 & TOS``.
539
540
Georg Brandl4833e5b2010-07-03 10:41:33 +0000541.. opcode:: INPLACE_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000542
543 Implements in-place ``TOS = TOS1 ^ TOS``.
544
545
Georg Brandl4833e5b2010-07-03 10:41:33 +0000546.. opcode:: INPLACE_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000547
548 Implements in-place ``TOS = TOS1 | TOS``.
549
Georg Brandl116aa622007-08-15 14:28:22 +0000550
Georg Brandl4833e5b2010-07-03 10:41:33 +0000551.. opcode:: STORE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000552
553 Implements ``TOS1[TOS] = TOS2``.
554
555
Georg Brandl4833e5b2010-07-03 10:41:33 +0000556.. opcode:: DELETE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000557
558 Implements ``del TOS1[TOS]``.
559
Georg Brandl116aa622007-08-15 14:28:22 +0000560
Yury Selivanov66f88282015-06-24 11:04:15 -0400561**Coroutine opcodes**
Yury Selivanov75445082015-05-11 22:57:16 -0400562
563.. opcode:: GET_AWAITABLE
564
Yury Selivanov66f88282015-06-24 11:04:15 -0400565 Implements ``TOS = get_awaitable(TOS)``, where ``get_awaitable(o)``
566 returns ``o`` if ``o`` is a coroutine object or a generator object with
567 the CO_ITERABLE_COROUTINE flag, or resolves
568 ``o.__await__``.
Yury Selivanov75445082015-05-11 22:57:16 -0400569
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200570 .. versionadded:: 3.5
571
Yury Selivanov75445082015-05-11 22:57:16 -0400572
573.. opcode:: GET_AITER
574
Yury Selivanov02e82a02017-10-06 10:18:10 -0400575 Implements ``TOS = TOS.__aiter__()``.
576
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200577 .. versionadded:: 3.5
Yury Selivanov02e82a02017-10-06 10:18:10 -0400578 .. versionchanged:: 3.7
579 Returning awaitable objects from ``__aiter__`` is no longer
580 supported.
Yury Selivanov75445082015-05-11 22:57:16 -0400581
582
583.. opcode:: GET_ANEXT
584
585 Implements ``PUSH(get_awaitable(TOS.__anext__()))``. See ``GET_AWAITABLE``
586 for details about ``get_awaitable``
587
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200588 .. versionadded:: 3.5
589
Yury Selivanov75445082015-05-11 22:57:16 -0400590
591.. opcode:: BEFORE_ASYNC_WITH
592
593 Resolves ``__aenter__`` and ``__aexit__`` from the object on top of the
594 stack. Pushes ``__aexit__`` and result of ``__aenter__()`` to the stack.
595
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200596 .. versionadded:: 3.5
597
Yury Selivanov75445082015-05-11 22:57:16 -0400598
599.. opcode:: SETUP_ASYNC_WITH
600
601 Creates a new frame object.
602
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200603 .. versionadded:: 3.5
604
Yury Selivanov75445082015-05-11 22:57:16 -0400605
606
Georg Brandl4833e5b2010-07-03 10:41:33 +0000607**Miscellaneous opcodes**
Georg Brandl116aa622007-08-15 14:28:22 +0000608
Georg Brandl4833e5b2010-07-03 10:41:33 +0000609.. opcode:: PRINT_EXPR
Georg Brandl116aa622007-08-15 14:28:22 +0000610
611 Implements the expression statement for the interactive mode. TOS is removed
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500612 from the stack and printed. In non-interactive mode, an expression statement
613 is terminated with :opcode:`POP_TOP`.
Georg Brandl116aa622007-08-15 14:28:22 +0000614
615
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000616.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000617
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000618 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000619
620
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000621.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000622
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000623 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
624
625
626.. opcode:: MAP_ADD (i)
627
628 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
629 comprehensions.
630
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200631 .. versionadded:: 3.1
632
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200633For all of the :opcode:`SET_ADD`, :opcode:`LIST_APPEND` and :opcode:`MAP_ADD`
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500634instructions, while the added value or key/value pair is popped off, the
635container object remains on the stack so that it is available for further
636iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000637
638
Georg Brandl4833e5b2010-07-03 10:41:33 +0000639.. opcode:: RETURN_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000640
641 Returns with TOS to the caller of the function.
642
643
Georg Brandl4833e5b2010-07-03 10:41:33 +0000644.. opcode:: YIELD_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000645
Berker Peksagab4040e2015-03-02 06:33:30 +0200646 Pops TOS and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000647
648
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000649.. opcode:: YIELD_FROM
650
Berker Peksagab4040e2015-03-02 06:33:30 +0200651 Pops TOS and delegates to it as a subiterator from a :term:`generator`.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000652
653 .. versionadded:: 3.3
654
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200655
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700656.. opcode:: SETUP_ANNOTATIONS
657
658 Checks whether ``__annotations__`` is defined in ``locals()``, if not it is
Martin Panterb1321fb2016-10-10 00:38:21 +0000659 set up to an empty ``dict``. This opcode is only emitted if a class
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700660 or module body contains :term:`variable annotations <variable annotation>`
661 statically.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000662
Berker Peksag34b74ff2016-09-12 08:00:01 +0300663 .. versionadded:: 3.6
664
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200665
Georg Brandl4833e5b2010-07-03 10:41:33 +0000666.. opcode:: IMPORT_STAR
Georg Brandl116aa622007-08-15 14:28:22 +0000667
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500668 Loads all symbols not starting with ``'_'`` directly from the module TOS to
669 the local namespace. The module is popped after loading all names. This
670 opcode implements ``from module import *``.
Georg Brandl116aa622007-08-15 14:28:22 +0000671
672
Georg Brandl4833e5b2010-07-03 10:41:33 +0000673.. opcode:: POP_BLOCK
Georg Brandl116aa622007-08-15 14:28:22 +0000674
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500675 Removes one block from the block stack. Per frame, there is a stack of
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200676 blocks, denoting :keyword:`try` statements, and such.
Georg Brandl116aa622007-08-15 14:28:22 +0000677
678
Georg Brandl4833e5b2010-07-03 10:41:33 +0000679.. opcode:: POP_EXCEPT
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000680
681 Removes one block from the block stack. The popped block must be an exception
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500682 handler block, as implicitly created when entering an except handler. In
683 addition to popping extraneous values from the frame stack, the last three
684 popped values are used to restore the exception state.
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000685
686
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200687.. opcode:: POP_FINALLY (preserve_tos)
688
689 Cleans up the value stack and the block stack. If *preserve_tos* is not
690 ``0`` TOS first is popped from the stack and pushed on the stack after
691 perfoming other stack operations:
692
693 * If TOS is ``NULL`` or an integer (pushed by :opcode:`BEGIN_FINALLY`
694 or :opcode:`CALL_FINALLY`) it is popped from the stack.
695 * If TOS is an exception type (pushed when an exception has been raised)
696 6 values are popped from the stack, the last three popped values are
697 used to restore the exception state. An exception handler block is
698 removed from the block stack.
699
700 It is similar to :opcode:`END_FINALLY`, but doesn't change the bytecode
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200701 counter nor raise an exception. Used for implementing :keyword:`break`,
702 :keyword:`continue` and :keyword:`return` in the :keyword:`finally` block.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200703
704 .. versionadded:: 3.8
705
706
707.. opcode:: BEGIN_FINALLY
708
709 Pushes ``NULL`` onto the stack for using it in :opcode:`END_FINALLY`,
710 :opcode:`POP_FINALLY`, :opcode:`WITH_CLEANUP_START` and
711 :opcode:`WITH_CLEANUP_FINISH`. Starts the :keyword:`finally` block.
712
713 .. versionadded:: 3.8
714
715
Georg Brandl4833e5b2010-07-03 10:41:33 +0000716.. opcode:: END_FINALLY
Georg Brandl116aa622007-08-15 14:28:22 +0000717
718 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200719 exception has to be re-raised or execution has to be continued depending on
720 the value of TOS.
721
722 * If TOS is ``NULL`` (pushed by :opcode:`BEGIN_FINALLY`) continue from
723 the next instruction. TOS is popped.
724 * If TOS is an integer (pushed by :opcode:`CALL_FINALLY`), sets the
725 bytecode counter to TOS. TOS is popped.
726 * If TOS is an exception type (pushed when an exception has been raised)
727 6 values are popped from the stack, the first three popped values are
728 used to re-raise the exception and the last three popped values are used
729 to restore the exception state. An exception handler block is removed
730 from the block stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000731
732
Georg Brandl4833e5b2010-07-03 10:41:33 +0000733.. opcode:: LOAD_BUILD_CLASS
Georg Brandl116aa622007-08-15 14:28:22 +0000734
Georg Brandl5ac22302008-07-20 21:39:03 +0000735 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200736 by :opcode:`CALL_FUNCTION` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000737
Guido van Rossum04110fb2007-08-24 16:32:05 +0000738
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000739.. opcode:: SETUP_WITH (delta)
740
741 This opcode performs several operations before a with block starts. First,
742 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200743 the stack for later use by :opcode:`WITH_CLEANUP_START`. Then,
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000744 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200745 is pushed. Finally, the result of calling the ``__enter__()`` method is pushed onto
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000746 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
747 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
748 :opcode:`UNPACK_SEQUENCE`).
749
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200750 .. versionadded:: 3.2
751
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000752
Yury Selivanov75445082015-05-11 22:57:16 -0400753.. opcode:: WITH_CLEANUP_START
Guido van Rossum04110fb2007-08-24 16:32:05 +0000754
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200755 Starts cleaning up the stack when a :keyword:`with` statement block exits.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000756
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200757 At the top of the stack are either ``NULL`` (pushed by
758 :opcode:`BEGIN_FINALLY`) or 6 values pushed if an exception has been
759 raised in the with block. Below is the context manager's
760 :meth:`~object.__exit__` or :meth:`~object.__aexit__` bound method.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000761
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200762 If TOS is ``NULL``, calls ``SECOND(None, None, None)``,
763 removes the function from the stack, leaving TOS, and pushes ``None``
764 to the stack. Otherwise calls ``SEVENTH(TOP, SECOND, THIRD)``,
765 shifts the bottom 3 values of the stack down, replaces the empty spot
766 with ``NULL`` and pushes TOS. Finally pushes the result of the call.
Yury Selivanov75445082015-05-11 22:57:16 -0400767
768
769.. opcode:: WITH_CLEANUP_FINISH
770
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200771 Finishes cleaning up the stack when a :keyword:`with` statement block exits.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000772
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200773 TOS is result of ``__exit__()`` or ``__aexit__()`` function call pushed
774 by :opcode:`WITH_CLEANUP_START`. SECOND is ``None`` or an exception type
775 (pushed when an exception has been raised).
Guido van Rossum04110fb2007-08-24 16:32:05 +0000776
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200777 Pops two values from the stack. If SECOND is not None and TOS is true
778 unwinds the EXCEPT_HANDLER block which was created when the exception
779 was caught and pushes ``NULL`` to the stack.
Georg Brandl9afde1c2007-11-01 20:32:30 +0000780
Guido van Rossum04110fb2007-08-24 16:32:05 +0000781
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300782All of the following opcodes use their arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000783
Georg Brandl116aa622007-08-15 14:28:22 +0000784.. opcode:: STORE_NAME (namei)
785
786 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500787 :attr:`co_names` of the code object. The compiler tries to use
788 :opcode:`STORE_FAST` or :opcode:`STORE_GLOBAL` if possible.
Georg Brandl116aa622007-08-15 14:28:22 +0000789
790
791.. opcode:: DELETE_NAME (namei)
792
793 Implements ``del name``, where *namei* is the index into :attr:`co_names`
794 attribute of the code object.
795
796
797.. opcode:: UNPACK_SEQUENCE (count)
798
799 Unpacks TOS into *count* individual values, which are put onto the stack
800 right-to-left.
801
Georg Brandl116aa622007-08-15 14:28:22 +0000802
Georg Brandl5ac22302008-07-20 21:39:03 +0000803.. opcode:: UNPACK_EX (counts)
804
805 Implements assignment with a starred target: Unpacks an iterable in TOS into
806 individual values, where the total number of values can be smaller than the
Martin Pantercc71a792016-04-05 06:19:42 +0000807 number of items in the iterable: one of the new values will be a list of all
Georg Brandl5ac22302008-07-20 21:39:03 +0000808 leftover items.
809
810 The low byte of *counts* is the number of values before the list value, the
811 high byte of *counts* the number of values after it. The resulting values
812 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000813
Georg Brandl5ac22302008-07-20 21:39:03 +0000814
Georg Brandl116aa622007-08-15 14:28:22 +0000815.. opcode:: STORE_ATTR (namei)
816
817 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
818 :attr:`co_names`.
819
820
821.. opcode:: DELETE_ATTR (namei)
822
823 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
824
825
826.. opcode:: STORE_GLOBAL (namei)
827
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200828 Works as :opcode:`STORE_NAME`, but stores the name as a global.
Georg Brandl116aa622007-08-15 14:28:22 +0000829
830
831.. opcode:: DELETE_GLOBAL (namei)
832
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200833 Works as :opcode:`DELETE_NAME`, but deletes a global name.
Georg Brandl116aa622007-08-15 14:28:22 +0000834
Georg Brandl116aa622007-08-15 14:28:22 +0000835
836.. opcode:: LOAD_CONST (consti)
837
838 Pushes ``co_consts[consti]`` onto the stack.
839
840
841.. opcode:: LOAD_NAME (namei)
842
843 Pushes the value associated with ``co_names[namei]`` onto the stack.
844
845
846.. opcode:: BUILD_TUPLE (count)
847
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500848 Creates a tuple consuming *count* items from the stack, and pushes the
849 resulting tuple onto the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000850
851
852.. opcode:: BUILD_LIST (count)
853
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200854 Works as :opcode:`BUILD_TUPLE`, but creates a list.
Georg Brandl116aa622007-08-15 14:28:22 +0000855
856
857.. opcode:: BUILD_SET (count)
858
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200859 Works as :opcode:`BUILD_TUPLE`, but creates a set.
Georg Brandl116aa622007-08-15 14:28:22 +0000860
861
Christian Heimesa62da1d2008-01-12 19:39:10 +0000862.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000863
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100864 Pushes a new dictionary object onto the stack. Pops ``2 * count`` items
865 so that the dictionary holds *count* entries:
866 ``{..., TOS3: TOS2, TOS1: TOS}``.
867
868 .. versionchanged:: 3.5
869 The dictionary is created from stack items instead of creating an
870 empty dictionary pre-sized to hold *count* items.
Georg Brandl116aa622007-08-15 14:28:22 +0000871
872
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +0300873.. opcode:: BUILD_CONST_KEY_MAP (count)
874
875 The version of :opcode:`BUILD_MAP` specialized for constant keys. *count*
876 values are consumed from the stack. The top element on the stack contains
877 a tuple of keys.
878
879 .. versionadded:: 3.6
880
881
Serhiy Storchakaea525a22016-09-06 22:07:53 +0300882.. opcode:: BUILD_STRING (count)
883
884 Concatenates *count* strings from the stack and pushes the resulting string
885 onto the stack.
886
887 .. versionadded:: 3.6
888
889
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100890.. opcode:: BUILD_TUPLE_UNPACK (count)
891
892 Pops *count* iterables from the stack, joins them in a single tuple,
893 and pushes the result. Implements iterable unpacking in tuple
894 displays ``(*x, *y, *z)``.
895
896 .. versionadded:: 3.5
897
898
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100899.. opcode:: BUILD_TUPLE_UNPACK_WITH_CALL (count)
900
901 This is similar to :opcode:`BUILD_TUPLE_UNPACK`,
902 but is used for ``f(*x, *y, *z)`` call syntax. The stack item at position
903 ``count + 1`` should be the corresponding callable ``f``.
904
905 .. versionadded:: 3.6
906
907
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100908.. opcode:: BUILD_LIST_UNPACK (count)
909
910 This is similar to :opcode:`BUILD_TUPLE_UNPACK`, but pushes a list
911 instead of tuple. Implements iterable unpacking in list
912 displays ``[*x, *y, *z]``.
913
914 .. versionadded:: 3.5
915
916
917.. opcode:: BUILD_SET_UNPACK (count)
918
919 This is similar to :opcode:`BUILD_TUPLE_UNPACK`, but pushes a set
920 instead of tuple. Implements iterable unpacking in set
921 displays ``{*x, *y, *z}``.
922
923 .. versionadded:: 3.5
924
925
926.. opcode:: BUILD_MAP_UNPACK (count)
927
928 Pops *count* mappings from the stack, merges them into a single dictionary,
929 and pushes the result. Implements dictionary unpacking in dictionary
930 displays ``{**x, **y, **z}``.
931
932 .. versionadded:: 3.5
933
934
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100935.. opcode:: BUILD_MAP_UNPACK_WITH_CALL (count)
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100936
937 This is similar to :opcode:`BUILD_MAP_UNPACK`,
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100938 but is used for ``f(**x, **y, **z)`` call syntax. The stack item at
939 position ``count + 2`` should be the corresponding callable ``f``.
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100940
941 .. versionadded:: 3.5
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100942 .. versionchanged:: 3.6
943 The position of the callable is determined by adding 2 to the opcode
944 argument instead of encoding it in the second byte of the argument.
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100945
946
Georg Brandl116aa622007-08-15 14:28:22 +0000947.. opcode:: LOAD_ATTR (namei)
948
949 Replaces TOS with ``getattr(TOS, co_names[namei])``.
950
951
952.. opcode:: COMPARE_OP (opname)
953
954 Performs a Boolean operation. The operation name can be found in
955 ``cmp_op[opname]``.
956
957
958.. opcode:: IMPORT_NAME (namei)
959
Christian Heimesa342c012008-04-20 21:01:16 +0000960 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
961 the *fromlist* and *level* arguments of :func:`__import__`. The module
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500962 object is pushed onto the stack. The current namespace is not affected: for
963 a proper import statement, a subsequent :opcode:`STORE_FAST` instruction
Christian Heimesa342c012008-04-20 21:01:16 +0000964 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000965
966
967.. opcode:: IMPORT_FROM (namei)
968
969 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
970 resulting object is pushed onto the stack, to be subsequently stored by a
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200971 :opcode:`STORE_FAST` instruction.
Georg Brandl116aa622007-08-15 14:28:22 +0000972
973
974.. opcode:: JUMP_FORWARD (delta)
975
Georg Brandl9afde1c2007-11-01 20:32:30 +0000976 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000977
978
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000979.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000980
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000981 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000982
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200983 .. versionadded:: 3.1
984
Georg Brandl116aa622007-08-15 14:28:22 +0000985
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000986.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000987
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000988 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
989
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200990 .. versionadded:: 3.1
991
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000992
993.. opcode:: JUMP_IF_TRUE_OR_POP (target)
994
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500995 If TOS is true, sets the bytecode counter to *target* and leaves TOS on the
996 stack. Otherwise (TOS is false), TOS is popped.
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000997
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +0200998 .. versionadded:: 3.1
999
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001000
1001.. opcode:: JUMP_IF_FALSE_OR_POP (target)
1002
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001003 If TOS is false, sets the bytecode counter to *target* and leaves TOS on the
1004 stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +00001005
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +02001006 .. versionadded:: 3.1
1007
Georg Brandl116aa622007-08-15 14:28:22 +00001008
1009.. opcode:: JUMP_ABSOLUTE (target)
1010
Georg Brandl9afde1c2007-11-01 20:32:30 +00001011 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +00001012
1013
1014.. opcode:: FOR_ITER (delta)
1015
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001016 TOS is an :term:`iterator`. Call its :meth:`~iterator.__next__` method. If
1017 this yields a new value, push it on the stack (leaving the iterator below
1018 it). If the iterator indicates it is exhausted TOS is popped, and the byte
1019 code counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +00001020
Georg Brandl116aa622007-08-15 14:28:22 +00001021
1022.. opcode:: LOAD_GLOBAL (namei)
1023
1024 Loads the global named ``co_names[namei]`` onto the stack.
1025
Georg Brandl116aa622007-08-15 14:28:22 +00001026
Georg Brandl116aa622007-08-15 14:28:22 +00001027.. opcode:: SETUP_FINALLY (delta)
1028
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001029 Pushes a try block from a try-finally or try-except clause onto the block
1030 stack. *delta* points to the finally block or the first except block.
1031
1032
1033.. opcode:: CALL_FINALLY (delta)
1034
1035 Pushes the address of the next instruction onto the stack and increments
1036 bytecode counter by *delta*. Used for calling the finally block as a
1037 "subroutine".
1038
1039 .. versionadded:: 3.8
Georg Brandl116aa622007-08-15 14:28:22 +00001040
1041
1042.. opcode:: LOAD_FAST (var_num)
1043
1044 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
1045
1046
1047.. opcode:: STORE_FAST (var_num)
1048
1049 Stores TOS into the local ``co_varnames[var_num]``.
1050
1051
1052.. opcode:: DELETE_FAST (var_num)
1053
1054 Deletes local ``co_varnames[var_num]``.
1055
1056
1057.. opcode:: LOAD_CLOSURE (i)
1058
1059 Pushes a reference to the cell contained in slot *i* of the cell and free
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001060 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
1061 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
Georg Brandl116aa622007-08-15 14:28:22 +00001062 len(co_cellvars)]``.
1063
1064
1065.. opcode:: LOAD_DEREF (i)
1066
1067 Loads the cell contained in slot *i* of the cell and free variable storage.
1068 Pushes a reference to the object the cell contains on the stack.
1069
1070
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001071.. opcode:: LOAD_CLASSDEREF (i)
1072
1073 Much like :opcode:`LOAD_DEREF` but first checks the locals dictionary before
1074 consulting the cell. This is used for loading free variables in class
1075 bodies.
1076
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +02001077 .. versionadded:: 3.4
1078
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001079
Georg Brandl116aa622007-08-15 14:28:22 +00001080.. opcode:: STORE_DEREF (i)
1081
1082 Stores TOS into the cell contained in slot *i* of the cell and free variable
1083 storage.
1084
1085
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001086.. opcode:: DELETE_DEREF (i)
1087
1088 Empties the cell contained in slot *i* of the cell and free variable storage.
1089 Used by the :keyword:`del` statement.
1090
Serhiy Storchaka12e7cd82018-02-01 13:48:33 +02001091 .. versionadded:: 3.2
1092
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001093
Georg Brandl116aa622007-08-15 14:28:22 +00001094.. opcode:: RAISE_VARARGS (argc)
1095
1096 Raises an exception. *argc* indicates the number of parameters to the raise
1097 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
1098 the parameter as TOS1, and the exception as TOS.
1099
1100
1101.. opcode:: CALL_FUNCTION (argc)
1102
Ivan Levkivskyi4b2a2a42017-03-10 23:52:35 +01001103 Calls a function. *argc* indicates the number of positional arguments.
1104 The positional arguments are on the stack, with the right-most argument
1105 on top. Below the arguments, the function object to call is on the stack.
1106 Pops all function arguments, and the function itself off the stack, and
1107 pushes the return value.
1108
1109 .. versionchanged:: 3.6
1110 This opcode is used only for calls with positional arguments.
1111
1112
1113.. opcode:: CALL_FUNCTION_KW (argc)
1114
1115 Calls a function. *argc* indicates the number of arguments (positional
1116 and keyword). The top element on the stack contains a tuple of keyword
1117 argument names. Below the tuple, keyword arguments are on the stack, in
1118 the order corresponding to the tuple. Below the keyword arguments, the
1119 positional arguments are on the stack, with the right-most parameter on
1120 top. Below the arguments, the function object to call is on the stack.
1121 Pops all function arguments, and the function itself off the stack, and
1122 pushes the return value.
1123
1124 .. versionchanged:: 3.6
1125 Keyword arguments are packed in a tuple instead of a dictionary,
1126 *argc* indicates the total number of arguments
1127
1128
1129.. opcode:: CALL_FUNCTION_EX (flags)
1130
1131 Calls a function. The lowest bit of *flags* indicates whether the
1132 var-keyword argument is placed at the top of the stack. Below the
1133 var-keyword argument, the var-positional argument is on the stack.
1134 Below the arguments, the function object to call is placed.
1135 Pops all function arguments, and the function itself off the stack, and
1136 pushes the return value. Note that this opcode pops at most three items
1137 from the stack. Var-positional and var-keyword arguments are packed
Moses Koledoye0c716532017-11-18 23:49:15 +01001138 by :opcode:`BUILD_TUPLE_UNPACK_WITH_CALL` and
Ivan Levkivskyi4b2a2a42017-03-10 23:52:35 +01001139 :opcode:`BUILD_MAP_UNPACK_WITH_CALL`.
1140
1141 .. versionadded:: 3.6
Georg Brandl116aa622007-08-15 14:28:22 +00001142
1143
INADA Naoki015bce62017-01-16 17:23:30 +09001144.. opcode:: LOAD_METHOD (namei)
1145
1146 Loads a method named ``co_names[namei]`` from TOS object. TOS is popped and
1147 method and TOS are pushed when interpreter can call unbound method directly.
Ivan Levkivskyi4b2a2a42017-03-10 23:52:35 +01001148 TOS will be used as the first argument (``self``) by :opcode:`CALL_METHOD`.
INADA Naoki015bce62017-01-16 17:23:30 +09001149 Otherwise, ``NULL`` and method is pushed (method is bound method or
1150 something else).
1151
1152 .. versionadded:: 3.7
1153
1154
1155.. opcode:: CALL_METHOD (argc)
1156
1157 Calls a method. *argc* is number of positional arguments.
1158 Keyword arguments are not supported. This opcode is designed to be used
1159 with :opcode:`LOAD_METHOD`. Positional arguments are on top of the stack.
1160 Below them, two items described in :opcode:`LOAD_METHOD` on the stack.
1161 All of them are popped and return value is pushed.
1162
1163 .. versionadded:: 3.7
1164
1165
Georg Brandl116aa622007-08-15 14:28:22 +00001166.. opcode:: MAKE_FUNCTION (argc)
1167
Georg Brandlc96ef1f2013-10-12 18:41:18 +02001168 Pushes a new function object on the stack. From bottom to top, the consumed
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001169 stack must consist of values if the argument carries a specified flag value
Georg Brandlc96ef1f2013-10-12 18:41:18 +02001170
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001171 * ``0x01`` a tuple of default argument objects in positional order
1172 * ``0x02`` a dictionary of keyword-only parameters' default values
1173 * ``0x04`` an annotation dictionary
1174 * ``0x08`` a tuple containing cells for free variables, making a closure
Georg Brandlc96ef1f2013-10-12 18:41:18 +02001175 * the code associated with the function (at TOS1)
1176 * the :term:`qualified name` of the function (at TOS)
Georg Brandl116aa622007-08-15 14:28:22 +00001177
1178
Georg Brandl116aa622007-08-15 14:28:22 +00001179.. opcode:: BUILD_SLICE (argc)
1180
1181 .. index:: builtin: slice
1182
1183 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
1184 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001185 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +00001186
1187
1188.. opcode:: EXTENDED_ARG (ext)
1189
1190 Prefixes any opcode which has an argument too big to fit into the default two
1191 bytes. *ext* holds two additional bytes which, taken together with the
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001192 subsequent opcode's argument, comprise a four-byte argument, *ext* being the
1193 two most-significant bytes.
Georg Brandl116aa622007-08-15 14:28:22 +00001194
1195
Eric V. Smith281d5322015-11-03 13:09:01 -05001196.. opcode:: FORMAT_VALUE (flags)
1197
1198 Used for implementing formatted literal strings (f-strings). Pops
1199 an optional *fmt_spec* from the stack, then a required *value*.
1200 *flags* is interpreted as follows:
1201
Eric V. Smith9ce52e32015-11-03 16:30:49 -05001202 * ``(flags & 0x03) == 0x00``: *value* is formatted as-is.
Eric V. Smith281d5322015-11-03 13:09:01 -05001203 * ``(flags & 0x03) == 0x01``: call :func:`str` on *value* before
1204 formatting it.
1205 * ``(flags & 0x03) == 0x02``: call :func:`repr` on *value* before
1206 formatting it.
1207 * ``(flags & 0x03) == 0x03``: call :func:`ascii` on *value* before
1208 formatting it.
1209 * ``(flags & 0x04) == 0x04``: pop *fmt_spec* from the stack and use
1210 it, else use an empty *fmt_spec*.
1211
Eric V. Smitha3a3d732015-11-04 07:11:13 -05001212 Formatting is performed using :c:func:`PyObject_Format`. The
1213 result is pushed on the stack.
Eric V. Smith281d5322015-11-03 13:09:01 -05001214
Eric V. Smith9ce52e32015-11-03 16:30:49 -05001215 .. versionadded:: 3.6
1216
Eric V. Smith281d5322015-11-03 13:09:01 -05001217
Georg Brandl4833e5b2010-07-03 10:41:33 +00001218.. opcode:: HAVE_ARGUMENT
Georg Brandl116aa622007-08-15 14:28:22 +00001219
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001220 This is not really an opcode. It identifies the dividing line between
Ivan Levkivskyi8f9e1bbf2017-03-24 22:05:04 +01001221 opcodes which don't use their argument and those that do
1222 (``< HAVE_ARGUMENT`` and ``>= HAVE_ARGUMENT``, respectively).
1223
1224 .. versionchanged:: 3.6
1225 Now every instruction has an argument, but opcodes ``< HAVE_ARGUMENT``
1226 ignore it. Before, only opcodes ``>= HAVE_ARGUMENT`` had an argument.
1227
Georg Brandl116aa622007-08-15 14:28:22 +00001228
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001229.. _opcode_collections:
1230
1231Opcode collections
1232------------------
1233
1234These collections are provided for automatic introspection of bytecode
1235instructions:
1236
1237.. data:: opname
1238
1239 Sequence of operation names, indexable using the bytecode.
1240
1241
1242.. data:: opmap
1243
1244 Dictionary mapping operation names to bytecodes.
1245
1246
1247.. data:: cmp_op
1248
1249 Sequence of all compare operation names.
1250
1251
1252.. data:: hasconst
1253
1254 Sequence of bytecodes that have a constant parameter.
1255
1256
1257.. data:: hasfree
1258
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001259 Sequence of bytecodes that access a free variable (note that 'free' in this
1260 context refers to names in the current scope that are referenced by inner
1261 scopes or names in outer scopes that are referenced from this scope. It does
1262 *not* include references to global or builtin scopes).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001263
1264
1265.. data:: hasname
1266
1267 Sequence of bytecodes that access an attribute by name.
1268
1269
1270.. data:: hasjrel
1271
1272 Sequence of bytecodes that have a relative jump target.
1273
1274
1275.. data:: hasjabs
1276
1277 Sequence of bytecodes that have an absolute jump target.
1278
1279
1280.. data:: haslocal
1281
1282 Sequence of bytecodes that access a local variable.
1283
1284
1285.. data:: hascompare
1286
1287 Sequence of bytecodes of Boolean operations.