blob: 01d46f88fb5796aa4fda079d8f04c4263d866827 [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
Georg Brandl4833e5b2010-07-03 10:41:33 +0000342
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000343.. opcode:: DUP_TOP_TWO
344
345 Duplicates the two references on top of the stack, leaving them in the
346 same order.
347
348
Georg Brandl4833e5b2010-07-03 10:41:33 +0000349**Unary operations**
350
351Unary operations take the top of the stack, apply the operation, and push the
Georg Brandl116aa622007-08-15 14:28:22 +0000352result back on the stack.
353
Georg Brandl4833e5b2010-07-03 10:41:33 +0000354.. opcode:: UNARY_POSITIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000355
356 Implements ``TOS = +TOS``.
357
358
Georg Brandl4833e5b2010-07-03 10:41:33 +0000359.. opcode:: UNARY_NEGATIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000360
361 Implements ``TOS = -TOS``.
362
363
Georg Brandl4833e5b2010-07-03 10:41:33 +0000364.. opcode:: UNARY_NOT
Georg Brandl116aa622007-08-15 14:28:22 +0000365
366 Implements ``TOS = not TOS``.
367
368
Georg Brandl4833e5b2010-07-03 10:41:33 +0000369.. opcode:: UNARY_INVERT
Georg Brandl116aa622007-08-15 14:28:22 +0000370
371 Implements ``TOS = ~TOS``.
372
373
Georg Brandl4833e5b2010-07-03 10:41:33 +0000374.. opcode:: GET_ITER
Georg Brandl116aa622007-08-15 14:28:22 +0000375
376 Implements ``TOS = iter(TOS)``.
377
Georg Brandl4833e5b2010-07-03 10:41:33 +0000378
Yury Selivanov5376ba92015-06-22 12:19:30 -0400379.. opcode:: GET_YIELD_FROM_ITER
380
381 If ``TOS`` is a :term:`generator iterator` or :term:`coroutine` object
382 it is left as is. Otherwise, implements ``TOS = iter(TOS)``.
383
384 .. versionadded:: 3.5
385
386
Georg Brandl4833e5b2010-07-03 10:41:33 +0000387**Binary operations**
388
Georg Brandl116aa622007-08-15 14:28:22 +0000389Binary operations remove the top of the stack (TOS) and the second top-most
390stack item (TOS1) from the stack. They perform the operation, and put the
391result back on the stack.
392
Georg Brandl4833e5b2010-07-03 10:41:33 +0000393.. opcode:: BINARY_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000394
395 Implements ``TOS = TOS1 ** TOS``.
396
397
Georg Brandl4833e5b2010-07-03 10:41:33 +0000398.. opcode:: BINARY_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000399
400 Implements ``TOS = TOS1 * TOS``.
401
402
Benjamin Petersond51374e2014-04-09 23:55:56 -0400403.. opcode:: BINARY_MATRIX_MULTIPLY
404
405 Implements ``TOS = TOS1 @ TOS``.
406
Berker Peksagda0870c2015-03-12 20:56:45 +0200407 .. versionadded:: 3.5
408
Benjamin Petersond51374e2014-04-09 23:55:56 -0400409
Georg Brandl4833e5b2010-07-03 10:41:33 +0000410.. opcode:: BINARY_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000411
412 Implements ``TOS = TOS1 // TOS``.
413
414
Georg Brandl4833e5b2010-07-03 10:41:33 +0000415.. opcode:: BINARY_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000416
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000417 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000418
419
Georg Brandl4833e5b2010-07-03 10:41:33 +0000420.. opcode:: BINARY_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000421
422 Implements ``TOS = TOS1 % TOS``.
423
424
Georg Brandl4833e5b2010-07-03 10:41:33 +0000425.. opcode:: BINARY_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000426
427 Implements ``TOS = TOS1 + TOS``.
428
429
Georg Brandl4833e5b2010-07-03 10:41:33 +0000430.. opcode:: BINARY_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000431
432 Implements ``TOS = TOS1 - TOS``.
433
434
Georg Brandl4833e5b2010-07-03 10:41:33 +0000435.. opcode:: BINARY_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000436
437 Implements ``TOS = TOS1[TOS]``.
438
439
Georg Brandl4833e5b2010-07-03 10:41:33 +0000440.. opcode:: BINARY_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000441
442 Implements ``TOS = TOS1 << TOS``.
443
444
Georg Brandl4833e5b2010-07-03 10:41:33 +0000445.. opcode:: BINARY_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000446
447 Implements ``TOS = TOS1 >> TOS``.
448
449
Georg Brandl4833e5b2010-07-03 10:41:33 +0000450.. opcode:: BINARY_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000451
452 Implements ``TOS = TOS1 & TOS``.
453
454
Georg Brandl4833e5b2010-07-03 10:41:33 +0000455.. opcode:: BINARY_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000456
457 Implements ``TOS = TOS1 ^ TOS``.
458
459
Georg Brandl4833e5b2010-07-03 10:41:33 +0000460.. opcode:: BINARY_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000461
462 Implements ``TOS = TOS1 | TOS``.
463
Georg Brandl4833e5b2010-07-03 10:41:33 +0000464
465**In-place operations**
466
Georg Brandl116aa622007-08-15 14:28:22 +0000467In-place operations are like binary operations, in that they remove TOS and
468TOS1, and push the result back on the stack, but the operation is done in-place
469when TOS1 supports it, and the resulting TOS may be (but does not have to be)
470the original TOS1.
471
Georg Brandl4833e5b2010-07-03 10:41:33 +0000472.. opcode:: INPLACE_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000473
474 Implements in-place ``TOS = TOS1 ** TOS``.
475
476
Georg Brandl4833e5b2010-07-03 10:41:33 +0000477.. opcode:: INPLACE_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000478
479 Implements in-place ``TOS = TOS1 * TOS``.
480
481
Benjamin Petersond51374e2014-04-09 23:55:56 -0400482.. opcode:: INPLACE_MATRIX_MULTIPLY
483
484 Implements in-place ``TOS = TOS1 @ TOS``.
485
Berker Peksagda0870c2015-03-12 20:56:45 +0200486 .. versionadded:: 3.5
487
Benjamin Petersond51374e2014-04-09 23:55:56 -0400488
Georg Brandl4833e5b2010-07-03 10:41:33 +0000489.. opcode:: INPLACE_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000490
491 Implements in-place ``TOS = TOS1 // TOS``.
492
493
Georg Brandl4833e5b2010-07-03 10:41:33 +0000494.. opcode:: INPLACE_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000495
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000496 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000497
498
Georg Brandl4833e5b2010-07-03 10:41:33 +0000499.. opcode:: INPLACE_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000500
501 Implements in-place ``TOS = TOS1 % TOS``.
502
503
Georg Brandl4833e5b2010-07-03 10:41:33 +0000504.. opcode:: INPLACE_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000505
506 Implements in-place ``TOS = TOS1 + TOS``.
507
508
Georg Brandl4833e5b2010-07-03 10:41:33 +0000509.. opcode:: INPLACE_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000510
511 Implements in-place ``TOS = TOS1 - TOS``.
512
513
Georg Brandl4833e5b2010-07-03 10:41:33 +0000514.. opcode:: INPLACE_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000515
516 Implements in-place ``TOS = TOS1 << TOS``.
517
518
Georg Brandl4833e5b2010-07-03 10:41:33 +0000519.. opcode:: INPLACE_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000520
521 Implements in-place ``TOS = TOS1 >> TOS``.
522
523
Georg Brandl4833e5b2010-07-03 10:41:33 +0000524.. opcode:: INPLACE_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000525
526 Implements in-place ``TOS = TOS1 & TOS``.
527
528
Georg Brandl4833e5b2010-07-03 10:41:33 +0000529.. opcode:: INPLACE_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000530
531 Implements in-place ``TOS = TOS1 ^ TOS``.
532
533
Georg Brandl4833e5b2010-07-03 10:41:33 +0000534.. opcode:: INPLACE_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000535
536 Implements in-place ``TOS = TOS1 | TOS``.
537
Georg Brandl116aa622007-08-15 14:28:22 +0000538
Georg Brandl4833e5b2010-07-03 10:41:33 +0000539.. opcode:: STORE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000540
541 Implements ``TOS1[TOS] = TOS2``.
542
543
Georg Brandl4833e5b2010-07-03 10:41:33 +0000544.. opcode:: DELETE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000545
546 Implements ``del TOS1[TOS]``.
547
Georg Brandl116aa622007-08-15 14:28:22 +0000548
Yury Selivanov66f88282015-06-24 11:04:15 -0400549**Coroutine opcodes**
Yury Selivanov75445082015-05-11 22:57:16 -0400550
551.. opcode:: GET_AWAITABLE
552
Yury Selivanov66f88282015-06-24 11:04:15 -0400553 Implements ``TOS = get_awaitable(TOS)``, where ``get_awaitable(o)``
554 returns ``o`` if ``o`` is a coroutine object or a generator object with
555 the CO_ITERABLE_COROUTINE flag, or resolves
556 ``o.__await__``.
Yury Selivanov75445082015-05-11 22:57:16 -0400557
558
559.. opcode:: GET_AITER
560
Yury Selivanov02e82a02017-10-06 10:18:10 -0400561 Implements ``TOS = TOS.__aiter__()``.
562
563 .. versionchanged:: 3.7
564 Returning awaitable objects from ``__aiter__`` is no longer
565 supported.
Yury Selivanov75445082015-05-11 22:57:16 -0400566
567
568.. opcode:: GET_ANEXT
569
570 Implements ``PUSH(get_awaitable(TOS.__anext__()))``. See ``GET_AWAITABLE``
571 for details about ``get_awaitable``
572
573
574.. opcode:: BEFORE_ASYNC_WITH
575
576 Resolves ``__aenter__`` and ``__aexit__`` from the object on top of the
577 stack. Pushes ``__aexit__`` and result of ``__aenter__()`` to the stack.
578
579
580.. opcode:: SETUP_ASYNC_WITH
581
582 Creates a new frame object.
583
584
585
Georg Brandl4833e5b2010-07-03 10:41:33 +0000586**Miscellaneous opcodes**
Georg Brandl116aa622007-08-15 14:28:22 +0000587
Georg Brandl4833e5b2010-07-03 10:41:33 +0000588.. opcode:: PRINT_EXPR
Georg Brandl116aa622007-08-15 14:28:22 +0000589
590 Implements the expression statement for the interactive mode. TOS is removed
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500591 from the stack and printed. In non-interactive mode, an expression statement
592 is terminated with :opcode:`POP_TOP`.
Georg Brandl116aa622007-08-15 14:28:22 +0000593
594
Georg Brandl4833e5b2010-07-03 10:41:33 +0000595.. opcode:: BREAK_LOOP
Georg Brandl116aa622007-08-15 14:28:22 +0000596
597 Terminates a loop due to a :keyword:`break` statement.
598
599
600.. opcode:: CONTINUE_LOOP (target)
601
602 Continues a loop due to a :keyword:`continue` statement. *target* is the
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200603 address to jump to (which should be a :opcode:`FOR_ITER` instruction).
Georg Brandl116aa622007-08-15 14:28:22 +0000604
605
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000606.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000607
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000608 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000609
610
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000611.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000612
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000613 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
614
615
616.. opcode:: MAP_ADD (i)
617
618 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
619 comprehensions.
620
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200621For all of the :opcode:`SET_ADD`, :opcode:`LIST_APPEND` and :opcode:`MAP_ADD`
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500622instructions, while the added value or key/value pair is popped off, the
623container object remains on the stack so that it is available for further
624iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000625
626
Georg Brandl4833e5b2010-07-03 10:41:33 +0000627.. opcode:: RETURN_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000628
629 Returns with TOS to the caller of the function.
630
631
Georg Brandl4833e5b2010-07-03 10:41:33 +0000632.. opcode:: YIELD_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000633
Berker Peksagab4040e2015-03-02 06:33:30 +0200634 Pops TOS and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000635
636
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000637.. opcode:: YIELD_FROM
638
Berker Peksagab4040e2015-03-02 06:33:30 +0200639 Pops TOS and delegates to it as a subiterator from a :term:`generator`.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000640
641 .. versionadded:: 3.3
642
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700643.. opcode:: SETUP_ANNOTATIONS
644
645 Checks whether ``__annotations__`` is defined in ``locals()``, if not it is
Martin Panterb1321fb2016-10-10 00:38:21 +0000646 set up to an empty ``dict``. This opcode is only emitted if a class
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700647 or module body contains :term:`variable annotations <variable annotation>`
648 statically.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000649
Berker Peksag34b74ff2016-09-12 08:00:01 +0300650 .. versionadded:: 3.6
651
Georg Brandl4833e5b2010-07-03 10:41:33 +0000652.. opcode:: IMPORT_STAR
Georg Brandl116aa622007-08-15 14:28:22 +0000653
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500654 Loads all symbols not starting with ``'_'`` directly from the module TOS to
655 the local namespace. The module is popped after loading all names. This
656 opcode implements ``from module import *``.
Georg Brandl116aa622007-08-15 14:28:22 +0000657
658
Georg Brandl4833e5b2010-07-03 10:41:33 +0000659.. opcode:: POP_BLOCK
Georg Brandl116aa622007-08-15 14:28:22 +0000660
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500661 Removes one block from the block stack. Per frame, there is a stack of
662 blocks, denoting nested loops, try statements, and such.
Georg Brandl116aa622007-08-15 14:28:22 +0000663
664
Georg Brandl4833e5b2010-07-03 10:41:33 +0000665.. opcode:: POP_EXCEPT
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000666
667 Removes one block from the block stack. The popped block must be an exception
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500668 handler block, as implicitly created when entering an except handler. In
669 addition to popping extraneous values from the frame stack, the last three
670 popped values are used to restore the exception state.
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000671
672
Georg Brandl4833e5b2010-07-03 10:41:33 +0000673.. opcode:: END_FINALLY
Georg Brandl116aa622007-08-15 14:28:22 +0000674
675 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
676 exception has to be re-raised, or whether the function returns, and continues
677 with the outer-next block.
678
679
Georg Brandl4833e5b2010-07-03 10:41:33 +0000680.. opcode:: LOAD_BUILD_CLASS
Georg Brandl116aa622007-08-15 14:28:22 +0000681
Georg Brandl5ac22302008-07-20 21:39:03 +0000682 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200683 by :opcode:`CALL_FUNCTION` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000684
Guido van Rossum04110fb2007-08-24 16:32:05 +0000685
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000686.. opcode:: SETUP_WITH (delta)
687
688 This opcode performs several operations before a with block starts. First,
689 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
690 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
691 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
692 is pushed. Finally, the result of calling the enter method is pushed onto
693 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
694 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
695 :opcode:`UNPACK_SEQUENCE`).
696
697
Yury Selivanov75445082015-05-11 22:57:16 -0400698.. opcode:: WITH_CLEANUP_START
Guido van Rossum04110fb2007-08-24 16:32:05 +0000699
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500700 Cleans up the stack when a :keyword:`with` statement block exits. TOS is the
701 context manager's :meth:`__exit__` bound method. Below TOS are 1--3 values
702 indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000703
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000704 * SECOND = ``None``
705 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
706 * SECOND = ``WHY_*``; no retval below it
707 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000708
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000709 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
Yury Selivanov75445082015-05-11 22:57:16 -0400710 ``TOS(None, None, None)``. Pushes SECOND and result of the call
711 to the stack.
712
713
714.. opcode:: WITH_CLEANUP_FINISH
715
716 Pops exception type and result of 'exit' function call from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000717
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500718 If the stack represents an exception, *and* the function call returns a
719 'true' value, this information is "zapped" and replaced with a single
720 ``WHY_SILENCED`` to prevent :opcode:`END_FINALLY` from re-raising the
721 exception. (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000722
Georg Brandl9afde1c2007-11-01 20:32:30 +0000723 .. XXX explain the WHY stuff!
724
Guido van Rossum04110fb2007-08-24 16:32:05 +0000725
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300726All of the following opcodes use their arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000727
Georg Brandl116aa622007-08-15 14:28:22 +0000728.. opcode:: STORE_NAME (namei)
729
730 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500731 :attr:`co_names` of the code object. The compiler tries to use
732 :opcode:`STORE_FAST` or :opcode:`STORE_GLOBAL` if possible.
Georg Brandl116aa622007-08-15 14:28:22 +0000733
734
735.. opcode:: DELETE_NAME (namei)
736
737 Implements ``del name``, where *namei* is the index into :attr:`co_names`
738 attribute of the code object.
739
740
741.. opcode:: UNPACK_SEQUENCE (count)
742
743 Unpacks TOS into *count* individual values, which are put onto the stack
744 right-to-left.
745
Georg Brandl116aa622007-08-15 14:28:22 +0000746
Georg Brandl5ac22302008-07-20 21:39:03 +0000747.. opcode:: UNPACK_EX (counts)
748
749 Implements assignment with a starred target: Unpacks an iterable in TOS into
750 individual values, where the total number of values can be smaller than the
Martin Pantercc71a792016-04-05 06:19:42 +0000751 number of items in the iterable: one of the new values will be a list of all
Georg Brandl5ac22302008-07-20 21:39:03 +0000752 leftover items.
753
754 The low byte of *counts* is the number of values before the list value, the
755 high byte of *counts* the number of values after it. The resulting values
756 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000757
Georg Brandl5ac22302008-07-20 21:39:03 +0000758
Georg Brandl116aa622007-08-15 14:28:22 +0000759.. opcode:: STORE_ATTR (namei)
760
761 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
762 :attr:`co_names`.
763
764
765.. opcode:: DELETE_ATTR (namei)
766
767 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
768
769
770.. opcode:: STORE_GLOBAL (namei)
771
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200772 Works as :opcode:`STORE_NAME`, but stores the name as a global.
Georg Brandl116aa622007-08-15 14:28:22 +0000773
774
775.. opcode:: DELETE_GLOBAL (namei)
776
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200777 Works as :opcode:`DELETE_NAME`, but deletes a global name.
Georg Brandl116aa622007-08-15 14:28:22 +0000778
Georg Brandl116aa622007-08-15 14:28:22 +0000779
780.. opcode:: LOAD_CONST (consti)
781
782 Pushes ``co_consts[consti]`` onto the stack.
783
784
785.. opcode:: LOAD_NAME (namei)
786
787 Pushes the value associated with ``co_names[namei]`` onto the stack.
788
789
790.. opcode:: BUILD_TUPLE (count)
791
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500792 Creates a tuple consuming *count* items from the stack, and pushes the
793 resulting tuple onto the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000794
795
796.. opcode:: BUILD_LIST (count)
797
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200798 Works as :opcode:`BUILD_TUPLE`, but creates a list.
Georg Brandl116aa622007-08-15 14:28:22 +0000799
800
801.. opcode:: BUILD_SET (count)
802
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200803 Works as :opcode:`BUILD_TUPLE`, but creates a set.
Georg Brandl116aa622007-08-15 14:28:22 +0000804
805
Christian Heimesa62da1d2008-01-12 19:39:10 +0000806.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000807
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100808 Pushes a new dictionary object onto the stack. Pops ``2 * count`` items
809 so that the dictionary holds *count* entries:
810 ``{..., TOS3: TOS2, TOS1: TOS}``.
811
812 .. versionchanged:: 3.5
813 The dictionary is created from stack items instead of creating an
814 empty dictionary pre-sized to hold *count* items.
Georg Brandl116aa622007-08-15 14:28:22 +0000815
816
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +0300817.. opcode:: BUILD_CONST_KEY_MAP (count)
818
819 The version of :opcode:`BUILD_MAP` specialized for constant keys. *count*
820 values are consumed from the stack. The top element on the stack contains
821 a tuple of keys.
822
823 .. versionadded:: 3.6
824
825
Serhiy Storchakaea525a22016-09-06 22:07:53 +0300826.. opcode:: BUILD_STRING (count)
827
828 Concatenates *count* strings from the stack and pushes the resulting string
829 onto the stack.
830
831 .. versionadded:: 3.6
832
833
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100834.. opcode:: BUILD_TUPLE_UNPACK (count)
835
836 Pops *count* iterables from the stack, joins them in a single tuple,
837 and pushes the result. Implements iterable unpacking in tuple
838 displays ``(*x, *y, *z)``.
839
840 .. versionadded:: 3.5
841
842
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100843.. opcode:: BUILD_TUPLE_UNPACK_WITH_CALL (count)
844
845 This is similar to :opcode:`BUILD_TUPLE_UNPACK`,
846 but is used for ``f(*x, *y, *z)`` call syntax. The stack item at position
847 ``count + 1`` should be the corresponding callable ``f``.
848
849 .. versionadded:: 3.6
850
851
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100852.. opcode:: BUILD_LIST_UNPACK (count)
853
854 This is similar to :opcode:`BUILD_TUPLE_UNPACK`, but pushes a list
855 instead of tuple. Implements iterable unpacking in list
856 displays ``[*x, *y, *z]``.
857
858 .. versionadded:: 3.5
859
860
861.. opcode:: BUILD_SET_UNPACK (count)
862
863 This is similar to :opcode:`BUILD_TUPLE_UNPACK`, but pushes a set
864 instead of tuple. Implements iterable unpacking in set
865 displays ``{*x, *y, *z}``.
866
867 .. versionadded:: 3.5
868
869
870.. opcode:: BUILD_MAP_UNPACK (count)
871
872 Pops *count* mappings from the stack, merges them into a single dictionary,
873 and pushes the result. Implements dictionary unpacking in dictionary
874 displays ``{**x, **y, **z}``.
875
876 .. versionadded:: 3.5
877
878
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100879.. opcode:: BUILD_MAP_UNPACK_WITH_CALL (count)
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100880
881 This is similar to :opcode:`BUILD_MAP_UNPACK`,
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100882 but is used for ``f(**x, **y, **z)`` call syntax. The stack item at
883 position ``count + 2`` should be the corresponding callable ``f``.
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100884
885 .. versionadded:: 3.5
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100886 .. versionchanged:: 3.6
887 The position of the callable is determined by adding 2 to the opcode
888 argument instead of encoding it in the second byte of the argument.
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100889
890
Georg Brandl116aa622007-08-15 14:28:22 +0000891.. opcode:: LOAD_ATTR (namei)
892
893 Replaces TOS with ``getattr(TOS, co_names[namei])``.
894
895
896.. opcode:: COMPARE_OP (opname)
897
898 Performs a Boolean operation. The operation name can be found in
899 ``cmp_op[opname]``.
900
901
902.. opcode:: IMPORT_NAME (namei)
903
Christian Heimesa342c012008-04-20 21:01:16 +0000904 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
905 the *fromlist* and *level* arguments of :func:`__import__`. The module
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500906 object is pushed onto the stack. The current namespace is not affected: for
907 a proper import statement, a subsequent :opcode:`STORE_FAST` instruction
Christian Heimesa342c012008-04-20 21:01:16 +0000908 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000909
910
911.. opcode:: IMPORT_FROM (namei)
912
913 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
914 resulting object is pushed onto the stack, to be subsequently stored by a
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200915 :opcode:`STORE_FAST` instruction.
Georg Brandl116aa622007-08-15 14:28:22 +0000916
917
918.. opcode:: JUMP_FORWARD (delta)
919
Georg Brandl9afde1c2007-11-01 20:32:30 +0000920 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000921
922
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000923.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000924
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000925 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000926
927
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000928.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000929
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000930 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
931
932
933.. opcode:: JUMP_IF_TRUE_OR_POP (target)
934
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500935 If TOS is true, sets the bytecode counter to *target* and leaves TOS on the
936 stack. Otherwise (TOS is false), TOS is popped.
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000937
938
939.. opcode:: JUMP_IF_FALSE_OR_POP (target)
940
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500941 If TOS is false, sets the bytecode counter to *target* and leaves TOS on the
942 stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000943
944
945.. opcode:: JUMP_ABSOLUTE (target)
946
Georg Brandl9afde1c2007-11-01 20:32:30 +0000947 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000948
949
950.. opcode:: FOR_ITER (delta)
951
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500952 TOS is an :term:`iterator`. Call its :meth:`~iterator.__next__` method. If
953 this yields a new value, push it on the stack (leaving the iterator below
954 it). If the iterator indicates it is exhausted TOS is popped, and the byte
955 code counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000956
Georg Brandl116aa622007-08-15 14:28:22 +0000957
958.. opcode:: LOAD_GLOBAL (namei)
959
960 Loads the global named ``co_names[namei]`` onto the stack.
961
Georg Brandl116aa622007-08-15 14:28:22 +0000962
963.. opcode:: SETUP_LOOP (delta)
964
965 Pushes a block for a loop onto the block stack. The block spans from the
966 current instruction with a size of *delta* bytes.
967
968
969.. opcode:: SETUP_EXCEPT (delta)
970
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500971 Pushes a try block from a try-except clause onto the block stack. *delta*
972 points to the first except block.
Georg Brandl116aa622007-08-15 14:28:22 +0000973
974
975.. opcode:: SETUP_FINALLY (delta)
976
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500977 Pushes a try block from a try-except clause onto the block stack. *delta*
978 points to the finally block.
Georg Brandl116aa622007-08-15 14:28:22 +0000979
980
981.. opcode:: LOAD_FAST (var_num)
982
983 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
984
985
986.. opcode:: STORE_FAST (var_num)
987
988 Stores TOS into the local ``co_varnames[var_num]``.
989
990
991.. opcode:: DELETE_FAST (var_num)
992
993 Deletes local ``co_varnames[var_num]``.
994
995
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700996.. opcode:: STORE_ANNOTATION (namei)
997
998 Stores TOS as ``locals()['__annotations__'][co_names[namei]] = TOS``.
999
Berker Peksag34b74ff2016-09-12 08:00:01 +03001000 .. versionadded:: 3.6
1001
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001002
Georg Brandl116aa622007-08-15 14:28:22 +00001003.. opcode:: LOAD_CLOSURE (i)
1004
1005 Pushes a reference to the cell contained in slot *i* of the cell and free
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001006 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
1007 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
Georg Brandl116aa622007-08-15 14:28:22 +00001008 len(co_cellvars)]``.
1009
1010
1011.. opcode:: LOAD_DEREF (i)
1012
1013 Loads the cell contained in slot *i* of the cell and free variable storage.
1014 Pushes a reference to the object the cell contains on the stack.
1015
1016
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001017.. opcode:: LOAD_CLASSDEREF (i)
1018
1019 Much like :opcode:`LOAD_DEREF` but first checks the locals dictionary before
1020 consulting the cell. This is used for loading free variables in class
1021 bodies.
1022
1023
Georg Brandl116aa622007-08-15 14:28:22 +00001024.. opcode:: STORE_DEREF (i)
1025
1026 Stores TOS into the cell contained in slot *i* of the cell and free variable
1027 storage.
1028
1029
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001030.. opcode:: DELETE_DEREF (i)
1031
1032 Empties the cell contained in slot *i* of the cell and free variable storage.
1033 Used by the :keyword:`del` statement.
1034
1035
Georg Brandl116aa622007-08-15 14:28:22 +00001036.. opcode:: RAISE_VARARGS (argc)
1037
1038 Raises an exception. *argc* indicates the number of parameters to the raise
1039 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
1040 the parameter as TOS1, and the exception as TOS.
1041
1042
1043.. opcode:: CALL_FUNCTION (argc)
1044
Ivan Levkivskyi4b2a2a42017-03-10 23:52:35 +01001045 Calls a function. *argc* indicates the number of positional arguments.
1046 The positional arguments are on the stack, with the right-most argument
1047 on top. Below the arguments, the function object to call is on the stack.
1048 Pops all function arguments, and the function itself off the stack, and
1049 pushes the return value.
1050
1051 .. versionchanged:: 3.6
1052 This opcode is used only for calls with positional arguments.
1053
1054
1055.. opcode:: CALL_FUNCTION_KW (argc)
1056
1057 Calls a function. *argc* indicates the number of arguments (positional
1058 and keyword). The top element on the stack contains a tuple of keyword
1059 argument names. Below the tuple, keyword arguments are on the stack, in
1060 the order corresponding to the tuple. Below the keyword arguments, the
1061 positional arguments are on the stack, with the right-most parameter on
1062 top. Below the arguments, the function object to call is on the stack.
1063 Pops all function arguments, and the function itself off the stack, and
1064 pushes the return value.
1065
1066 .. versionchanged:: 3.6
1067 Keyword arguments are packed in a tuple instead of a dictionary,
1068 *argc* indicates the total number of arguments
1069
1070
1071.. opcode:: CALL_FUNCTION_EX (flags)
1072
1073 Calls a function. The lowest bit of *flags* indicates whether the
1074 var-keyword argument is placed at the top of the stack. Below the
1075 var-keyword argument, the var-positional argument is on the stack.
1076 Below the arguments, the function object to call is placed.
1077 Pops all function arguments, and the function itself off the stack, and
1078 pushes the return value. Note that this opcode pops at most three items
1079 from the stack. Var-positional and var-keyword arguments are packed
Moses Koledoye0c716532017-11-18 23:49:15 +01001080 by :opcode:`BUILD_TUPLE_UNPACK_WITH_CALL` and
Ivan Levkivskyi4b2a2a42017-03-10 23:52:35 +01001081 :opcode:`BUILD_MAP_UNPACK_WITH_CALL`.
1082
1083 .. versionadded:: 3.6
Georg Brandl116aa622007-08-15 14:28:22 +00001084
1085
INADA Naoki015bce62017-01-16 17:23:30 +09001086.. opcode:: LOAD_METHOD (namei)
1087
1088 Loads a method named ``co_names[namei]`` from TOS object. TOS is popped and
1089 method and TOS are pushed when interpreter can call unbound method directly.
Ivan Levkivskyi4b2a2a42017-03-10 23:52:35 +01001090 TOS will be used as the first argument (``self``) by :opcode:`CALL_METHOD`.
INADA Naoki015bce62017-01-16 17:23:30 +09001091 Otherwise, ``NULL`` and method is pushed (method is bound method or
1092 something else).
1093
1094 .. versionadded:: 3.7
1095
1096
1097.. opcode:: CALL_METHOD (argc)
1098
1099 Calls a method. *argc* is number of positional arguments.
1100 Keyword arguments are not supported. This opcode is designed to be used
1101 with :opcode:`LOAD_METHOD`. Positional arguments are on top of the stack.
1102 Below them, two items described in :opcode:`LOAD_METHOD` on the stack.
1103 All of them are popped and return value is pushed.
1104
1105 .. versionadded:: 3.7
1106
1107
Georg Brandl116aa622007-08-15 14:28:22 +00001108.. opcode:: MAKE_FUNCTION (argc)
1109
Georg Brandlc96ef1f2013-10-12 18:41:18 +02001110 Pushes a new function object on the stack. From bottom to top, the consumed
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001111 stack must consist of values if the argument carries a specified flag value
Georg Brandlc96ef1f2013-10-12 18:41:18 +02001112
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001113 * ``0x01`` a tuple of default argument objects in positional order
1114 * ``0x02`` a dictionary of keyword-only parameters' default values
1115 * ``0x04`` an annotation dictionary
1116 * ``0x08`` a tuple containing cells for free variables, making a closure
Georg Brandlc96ef1f2013-10-12 18:41:18 +02001117 * the code associated with the function (at TOS1)
1118 * the :term:`qualified name` of the function (at TOS)
Georg Brandl116aa622007-08-15 14:28:22 +00001119
1120
Georg Brandl116aa622007-08-15 14:28:22 +00001121.. opcode:: BUILD_SLICE (argc)
1122
1123 .. index:: builtin: slice
1124
1125 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
1126 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001127 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +00001128
1129
1130.. opcode:: EXTENDED_ARG (ext)
1131
1132 Prefixes any opcode which has an argument too big to fit into the default two
1133 bytes. *ext* holds two additional bytes which, taken together with the
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001134 subsequent opcode's argument, comprise a four-byte argument, *ext* being the
1135 two most-significant bytes.
Georg Brandl116aa622007-08-15 14:28:22 +00001136
1137
Eric V. Smith281d5322015-11-03 13:09:01 -05001138.. opcode:: FORMAT_VALUE (flags)
1139
1140 Used for implementing formatted literal strings (f-strings). Pops
1141 an optional *fmt_spec* from the stack, then a required *value*.
1142 *flags* is interpreted as follows:
1143
Eric V. Smith9ce52e32015-11-03 16:30:49 -05001144 * ``(flags & 0x03) == 0x00``: *value* is formatted as-is.
Eric V. Smith281d5322015-11-03 13:09:01 -05001145 * ``(flags & 0x03) == 0x01``: call :func:`str` on *value* before
1146 formatting it.
1147 * ``(flags & 0x03) == 0x02``: call :func:`repr` on *value* before
1148 formatting it.
1149 * ``(flags & 0x03) == 0x03``: call :func:`ascii` on *value* before
1150 formatting it.
1151 * ``(flags & 0x04) == 0x04``: pop *fmt_spec* from the stack and use
1152 it, else use an empty *fmt_spec*.
1153
Eric V. Smitha3a3d732015-11-04 07:11:13 -05001154 Formatting is performed using :c:func:`PyObject_Format`. The
1155 result is pushed on the stack.
Eric V. Smith281d5322015-11-03 13:09:01 -05001156
Eric V. Smith9ce52e32015-11-03 16:30:49 -05001157 .. versionadded:: 3.6
1158
Eric V. Smith281d5322015-11-03 13:09:01 -05001159
Georg Brandl4833e5b2010-07-03 10:41:33 +00001160.. opcode:: HAVE_ARGUMENT
Georg Brandl116aa622007-08-15 14:28:22 +00001161
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001162 This is not really an opcode. It identifies the dividing line between
Ivan Levkivskyi8f9e1bbf2017-03-24 22:05:04 +01001163 opcodes which don't use their argument and those that do
1164 (``< HAVE_ARGUMENT`` and ``>= HAVE_ARGUMENT``, respectively).
1165
1166 .. versionchanged:: 3.6
1167 Now every instruction has an argument, but opcodes ``< HAVE_ARGUMENT``
1168 ignore it. Before, only opcodes ``>= HAVE_ARGUMENT`` had an argument.
1169
Georg Brandl116aa622007-08-15 14:28:22 +00001170
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001171.. _opcode_collections:
1172
1173Opcode collections
1174------------------
1175
1176These collections are provided for automatic introspection of bytecode
1177instructions:
1178
1179.. data:: opname
1180
1181 Sequence of operation names, indexable using the bytecode.
1182
1183
1184.. data:: opmap
1185
1186 Dictionary mapping operation names to bytecodes.
1187
1188
1189.. data:: cmp_op
1190
1191 Sequence of all compare operation names.
1192
1193
1194.. data:: hasconst
1195
1196 Sequence of bytecodes that have a constant parameter.
1197
1198
1199.. data:: hasfree
1200
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001201 Sequence of bytecodes that access a free variable (note that 'free' in this
1202 context refers to names in the current scope that are referenced by inner
1203 scopes or names in outer scopes that are referenced from this scope. It does
1204 *not* include references to global or builtin scopes).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001205
1206
1207.. data:: hasname
1208
1209 Sequence of bytecodes that access an attribute by name.
1210
1211
1212.. data:: hasjrel
1213
1214 Sequence of bytecodes that have a relative jump target.
1215
1216
1217.. data:: hasjabs
1218
1219 Sequence of bytecodes that have an absolute jump target.
1220
1221
1222.. data:: haslocal
1223
1224 Sequence of bytecodes that access a local variable.
1225
1226
1227.. data:: hascompare
1228
1229 Sequence of bytecodes of Boolean operations.