blob: f82dc40e0931feaaeda831a3964eaf6ba210149b [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
Benjamin Peterson2f3d4402015-03-02 09:36:48 -050056 Analyse the bytecode corresponding to a function, generator, method, string
57 of source code, or a code object (as returned by :func:`compile`).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100058
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050059 This is a convenience wrapper around many of the functions listed below, most
60 notably :func:`get_instructions`, as iterating over a :class:`Bytecode`
61 instance yields the bytecode operations as :class:`Instruction` instances.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100062
Serhiy Storchakaecf41da2016-10-19 16:29:26 +030063 If *first_line* is not ``None``, it indicates the line number that should be
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050064 reported for the first source line in the disassembled code. Otherwise, the
65 source line information (if any) is taken directly from the disassembled code
66 object.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100067
Serhiy Storchakaecf41da2016-10-19 16:29:26 +030068 If *current_offset* is not ``None``, it refers to an instruction offset in the
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050069 disassembled code. Setting this means :meth:`.dis` will display a "current
70 instruction" marker against the specified opcode.
Nick Coghlan50c48b82013-11-23 00:57:00 +100071
72 .. classmethod:: from_traceback(tb)
73
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050074 Construct a :class:`Bytecode` instance from the given traceback, setting
75 *current_offset* to the instruction responsible for the exception.
Nick Coghlan50c48b82013-11-23 00:57:00 +100076
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100077 .. data:: codeobj
78
79 The compiled code object.
80
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100081 .. data:: first_line
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100082
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100083 The first source line of the code object (if available)
84
85 .. method:: dis()
86
Benjamin Peterson29fec922015-03-02 09:27:43 -050087 Return a formatted view of the bytecode operations (the same as printed by
88 :func:`dis.dis`, but returned as a multi-line string).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100089
90 .. method:: info()
91
92 Return a formatted multi-line string with detailed information about the
93 code object, like :func:`code_info`.
94
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100095Example::
96
97 >>> bytecode = dis.Bytecode(myfunc)
98 >>> for instr in bytecode:
99 ... print(instr.opname)
100 ...
101 LOAD_GLOBAL
102 LOAD_FAST
103 CALL_FUNCTION
104 RETURN_VALUE
105
106
107Analysis functions
108------------------
109
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500110The :mod:`dis` module also defines the following analysis functions that convert
111the input directly to the desired output. They can be useful if only a single
112operation is being performed, so the intermediate analysis object isn't useful:
Georg Brandl116aa622007-08-15 14:28:22 +0000113
Nick Coghlane8814fb2010-09-10 14:08:04 +0000114.. function:: code_info(x)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000115
Georg Brandl67b21b72010-08-17 15:07:14 +0000116 Return a formatted multi-line string with detailed code object information
Nick Coghlanefd5df92014-07-25 23:02:56 +1000117 for the supplied function, generator, method, source code string or code object.
Nick Coghlaneae2da12010-08-17 08:03:36 +0000118
Georg Brandl67b21b72010-08-17 15:07:14 +0000119 Note that the exact contents of code info strings are highly implementation
120 dependent and they may change arbitrarily across Python VMs or Python
121 releases.
Nick Coghlaneae2da12010-08-17 08:03:36 +0000122
123 .. versionadded:: 3.2
124
Georg Brandl67b21b72010-08-17 15:07:14 +0000125
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000126.. function:: show_code(x, *, file=None)
Nick Coghlane8814fb2010-09-10 14:08:04 +0000127
128 Print detailed code object information for the supplied function, method,
Ezio Melotti6e6c6ac2013-08-23 22:41:39 +0300129 source code string or code object to *file* (or ``sys.stdout`` if *file*
130 is not specified).
Nick Coghlane8814fb2010-09-10 14:08:04 +0000131
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000132 This is a convenient shorthand for ``print(code_info(x), file=file)``,
133 intended for interactive exploration at the interpreter prompt.
Nick Coghlane8814fb2010-09-10 14:08:04 +0000134
135 .. versionadded:: 3.2
136
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000137 .. versionchanged:: 3.4
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200138 Added *file* parameter.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000139
140
141.. function:: dis(x=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Georg Brandl67b21b72010-08-17 15:07:14 +0000143 Disassemble the *x* object. *x* can denote either a module, a class, a
Nick Coghlanefd5df92014-07-25 23:02:56 +1000144 method, a function, a generator, a code object, a string of source code or
145 a byte sequence of raw bytecode. For a module, it disassembles all functions.
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300146 For a class, it disassembles all methods (including class and static methods).
147 For a code object or sequence of raw bytecode, it prints one line per bytecode
148 instruction. Strings are first compiled to code objects with the :func:`compile`
149 built-in function before being disassembled. If no object is provided, this
150 function disassembles the last traceback.
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200152 The disassembly is written as text to the supplied *file* argument if
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000153 provided and to ``sys.stdout`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000154
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000155 .. versionchanged:: 3.4
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200156 Added *file* parameter.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000157
158
159.. function:: distb(tb=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000160
Georg Brandl4833e5b2010-07-03 10:41:33 +0000161 Disassemble the top-of-stack function of a traceback, using the last
162 traceback if none was passed. The instruction causing the exception is
163 indicated.
Georg Brandl116aa622007-08-15 14:28:22 +0000164
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200165 The disassembly is written as text to the supplied *file* argument if
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000166 provided and to ``sys.stdout`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000167
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000168 .. versionchanged:: 3.4
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200169 Added *file* parameter.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000170
171
172.. function:: disassemble(code, lasti=-1, *, file=None)
173 disco(code, lasti=-1, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Georg Brandl4833e5b2010-07-03 10:41:33 +0000175 Disassemble a code object, indicating the last instruction if *lasti* was
Georg Brandl116aa622007-08-15 14:28:22 +0000176 provided. The output is divided in the following columns:
177
178 #. the line number, for the first instruction of each line
179 #. the current instruction, indicated as ``-->``,
180 #. a labelled instruction, indicated with ``>>``,
181 #. the address of the instruction,
182 #. the operation code name,
183 #. operation parameters, and
184 #. interpretation of the parameters in parentheses.
185
186 The parameter interpretation recognizes local and global variable names,
187 constant values, branch targets, and compare operators.
188
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200189 The disassembly is written as text to the supplied *file* argument if
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000190 provided and to ``sys.stdout`` otherwise.
191
192 .. versionchanged:: 3.4
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200193 Added *file* parameter.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000194
195
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000196.. function:: get_instructions(x, *, first_line=None)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000197
198 Return an iterator over the instructions in the supplied function, method,
199 source code string or code object.
200
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500201 The iterator generates a series of :class:`Instruction` named tuples giving
202 the details of each operation in the supplied code.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000203
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300204 If *first_line* is not ``None``, it indicates the line number that should be
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500205 reported for the first source line in the disassembled code. Otherwise, the
206 source line information (if any) is taken directly from the disassembled code
207 object.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000208
209 .. versionadded:: 3.4
210
Georg Brandl116aa622007-08-15 14:28:22 +0000211
Benjamin Peterson75edad02009-01-01 15:05:06 +0000212.. function:: findlinestarts(code)
213
214 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
215 attributes of the code object *code* to find the offsets which are starts of
216 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
Ivan Levkivskyi8f9e1bbf2017-03-24 22:05:04 +0100217 See :source:`Objects/lnotab_notes.txt` for the ``co_lnotab`` format and
218 how to decode it.
219
220 .. versionchanged:: 3.6
221 Line numbers can be decreasing. Before, they were always increasing.
Benjamin Peterson75edad02009-01-01 15:05:06 +0000222
223
224.. function:: findlabels(code)
225
226 Detect all offsets in the code object *code* which are jump targets, and
227 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +0000228
Larry Hastings3a907972013-11-23 14:49:22 -0800229
230.. function:: stack_effect(opcode, [oparg])
231
232 Compute the stack effect of *opcode* with argument *oparg*.
233
234 .. versionadded:: 3.4
235
Georg Brandl116aa622007-08-15 14:28:22 +0000236.. _bytecodes:
237
Georg Brandl9afde1c2007-11-01 20:32:30 +0000238Python Bytecode Instructions
239----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000240
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000241The :func:`get_instructions` function and :class:`Bytecode` class provide
242details of bytecode instructions as :class:`Instruction` instances:
243
244.. class:: Instruction
245
246 Details for a bytecode operation
247
248 .. data:: opcode
249
250 numeric code for operation, corresponding to the opcode values listed
251 below and the bytecode values in the :ref:`opcode_collections`.
252
253
254 .. data:: opname
255
256 human readable name for operation
257
258
259 .. data:: arg
260
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300261 numeric argument to operation (if any), otherwise ``None``
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000262
263
264 .. data:: argval
265
266 resolved arg value (if known), otherwise same as arg
267
268
269 .. data:: argrepr
270
271 human readable description of operation argument
272
273
274 .. data:: offset
275
276 start index of operation within bytecode sequence
277
278
279 .. data:: starts_line
280
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300281 line started by this opcode (if any), otherwise ``None``
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000282
283
284 .. data:: is_jump_target
285
Serhiy Storchaka0e90e992013-11-29 12:19:53 +0200286 ``True`` if other code jumps to here, otherwise ``False``
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000287
288 .. versionadded:: 3.4
289
290
Georg Brandl9afde1c2007-11-01 20:32:30 +0000291The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000292
293
Georg Brandl4833e5b2010-07-03 10:41:33 +0000294**General instructions**
295
Georg Brandl4833e5b2010-07-03 10:41:33 +0000296.. opcode:: NOP
Georg Brandl116aa622007-08-15 14:28:22 +0000297
298 Do nothing code. Used as a placeholder by the bytecode optimizer.
299
300
Georg Brandl4833e5b2010-07-03 10:41:33 +0000301.. opcode:: POP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000302
303 Removes the top-of-stack (TOS) item.
304
305
Georg Brandl4833e5b2010-07-03 10:41:33 +0000306.. opcode:: ROT_TWO
Georg Brandl116aa622007-08-15 14:28:22 +0000307
308 Swaps the two top-most stack items.
309
310
Georg Brandl4833e5b2010-07-03 10:41:33 +0000311.. opcode:: ROT_THREE
Georg Brandl116aa622007-08-15 14:28:22 +0000312
313 Lifts second and third stack item one position up, moves top down to position
314 three.
315
316
Georg Brandl4833e5b2010-07-03 10:41:33 +0000317.. opcode:: DUP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000318
319 Duplicates the reference on top of the stack.
320
Georg Brandl4833e5b2010-07-03 10:41:33 +0000321
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000322.. opcode:: DUP_TOP_TWO
323
324 Duplicates the two references on top of the stack, leaving them in the
325 same order.
326
327
Georg Brandl4833e5b2010-07-03 10:41:33 +0000328**Unary operations**
329
330Unary operations take the top of the stack, apply the operation, and push the
Georg Brandl116aa622007-08-15 14:28:22 +0000331result back on the stack.
332
Georg Brandl4833e5b2010-07-03 10:41:33 +0000333.. opcode:: UNARY_POSITIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000334
335 Implements ``TOS = +TOS``.
336
337
Georg Brandl4833e5b2010-07-03 10:41:33 +0000338.. opcode:: UNARY_NEGATIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000339
340 Implements ``TOS = -TOS``.
341
342
Georg Brandl4833e5b2010-07-03 10:41:33 +0000343.. opcode:: UNARY_NOT
Georg Brandl116aa622007-08-15 14:28:22 +0000344
345 Implements ``TOS = not TOS``.
346
347
Georg Brandl4833e5b2010-07-03 10:41:33 +0000348.. opcode:: UNARY_INVERT
Georg Brandl116aa622007-08-15 14:28:22 +0000349
350 Implements ``TOS = ~TOS``.
351
352
Georg Brandl4833e5b2010-07-03 10:41:33 +0000353.. opcode:: GET_ITER
Georg Brandl116aa622007-08-15 14:28:22 +0000354
355 Implements ``TOS = iter(TOS)``.
356
Georg Brandl4833e5b2010-07-03 10:41:33 +0000357
Yury Selivanov5376ba92015-06-22 12:19:30 -0400358.. opcode:: GET_YIELD_FROM_ITER
359
360 If ``TOS`` is a :term:`generator iterator` or :term:`coroutine` object
361 it is left as is. Otherwise, implements ``TOS = iter(TOS)``.
362
363 .. versionadded:: 3.5
364
365
Georg Brandl4833e5b2010-07-03 10:41:33 +0000366**Binary operations**
367
Georg Brandl116aa622007-08-15 14:28:22 +0000368Binary operations remove the top of the stack (TOS) and the second top-most
369stack item (TOS1) from the stack. They perform the operation, and put the
370result back on the stack.
371
Georg Brandl4833e5b2010-07-03 10:41:33 +0000372.. opcode:: BINARY_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000373
374 Implements ``TOS = TOS1 ** TOS``.
375
376
Georg Brandl4833e5b2010-07-03 10:41:33 +0000377.. opcode:: BINARY_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000378
379 Implements ``TOS = TOS1 * TOS``.
380
381
Benjamin Petersond51374e2014-04-09 23:55:56 -0400382.. opcode:: BINARY_MATRIX_MULTIPLY
383
384 Implements ``TOS = TOS1 @ TOS``.
385
Berker Peksagda0870c2015-03-12 20:56:45 +0200386 .. versionadded:: 3.5
387
Benjamin Petersond51374e2014-04-09 23:55:56 -0400388
Georg Brandl4833e5b2010-07-03 10:41:33 +0000389.. opcode:: BINARY_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000390
391 Implements ``TOS = TOS1 // TOS``.
392
393
Georg Brandl4833e5b2010-07-03 10:41:33 +0000394.. opcode:: BINARY_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000395
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000396 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000397
398
Georg Brandl4833e5b2010-07-03 10:41:33 +0000399.. opcode:: BINARY_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000400
401 Implements ``TOS = TOS1 % TOS``.
402
403
Georg Brandl4833e5b2010-07-03 10:41:33 +0000404.. opcode:: BINARY_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000405
406 Implements ``TOS = TOS1 + TOS``.
407
408
Georg Brandl4833e5b2010-07-03 10:41:33 +0000409.. opcode:: BINARY_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000410
411 Implements ``TOS = TOS1 - TOS``.
412
413
Georg Brandl4833e5b2010-07-03 10:41:33 +0000414.. opcode:: BINARY_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000415
416 Implements ``TOS = TOS1[TOS]``.
417
418
Georg Brandl4833e5b2010-07-03 10:41:33 +0000419.. opcode:: BINARY_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000420
421 Implements ``TOS = TOS1 << TOS``.
422
423
Georg Brandl4833e5b2010-07-03 10:41:33 +0000424.. opcode:: BINARY_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000425
426 Implements ``TOS = TOS1 >> TOS``.
427
428
Georg Brandl4833e5b2010-07-03 10:41:33 +0000429.. opcode:: BINARY_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000430
431 Implements ``TOS = TOS1 & TOS``.
432
433
Georg Brandl4833e5b2010-07-03 10:41:33 +0000434.. opcode:: BINARY_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000435
436 Implements ``TOS = TOS1 ^ TOS``.
437
438
Georg Brandl4833e5b2010-07-03 10:41:33 +0000439.. opcode:: BINARY_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000440
441 Implements ``TOS = TOS1 | TOS``.
442
Georg Brandl4833e5b2010-07-03 10:41:33 +0000443
444**In-place operations**
445
Georg Brandl116aa622007-08-15 14:28:22 +0000446In-place operations are like binary operations, in that they remove TOS and
447TOS1, and push the result back on the stack, but the operation is done in-place
448when TOS1 supports it, and the resulting TOS may be (but does not have to be)
449the original TOS1.
450
Georg Brandl4833e5b2010-07-03 10:41:33 +0000451.. opcode:: INPLACE_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000452
453 Implements in-place ``TOS = TOS1 ** TOS``.
454
455
Georg Brandl4833e5b2010-07-03 10:41:33 +0000456.. opcode:: INPLACE_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000457
458 Implements in-place ``TOS = TOS1 * TOS``.
459
460
Benjamin Petersond51374e2014-04-09 23:55:56 -0400461.. opcode:: INPLACE_MATRIX_MULTIPLY
462
463 Implements in-place ``TOS = TOS1 @ TOS``.
464
Berker Peksagda0870c2015-03-12 20:56:45 +0200465 .. versionadded:: 3.5
466
Benjamin Petersond51374e2014-04-09 23:55:56 -0400467
Georg Brandl4833e5b2010-07-03 10:41:33 +0000468.. opcode:: INPLACE_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000469
470 Implements in-place ``TOS = TOS1 // TOS``.
471
472
Georg Brandl4833e5b2010-07-03 10:41:33 +0000473.. opcode:: INPLACE_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000474
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000475 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000476
477
Georg Brandl4833e5b2010-07-03 10:41:33 +0000478.. opcode:: INPLACE_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000479
480 Implements in-place ``TOS = TOS1 % TOS``.
481
482
Georg Brandl4833e5b2010-07-03 10:41:33 +0000483.. opcode:: INPLACE_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000484
485 Implements in-place ``TOS = TOS1 + TOS``.
486
487
Georg Brandl4833e5b2010-07-03 10:41:33 +0000488.. opcode:: INPLACE_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000489
490 Implements in-place ``TOS = TOS1 - TOS``.
491
492
Georg Brandl4833e5b2010-07-03 10:41:33 +0000493.. opcode:: INPLACE_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000494
495 Implements in-place ``TOS = TOS1 << TOS``.
496
497
Georg Brandl4833e5b2010-07-03 10:41:33 +0000498.. opcode:: INPLACE_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000499
500 Implements in-place ``TOS = TOS1 >> TOS``.
501
502
Georg Brandl4833e5b2010-07-03 10:41:33 +0000503.. opcode:: INPLACE_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000504
505 Implements in-place ``TOS = TOS1 & TOS``.
506
507
Georg Brandl4833e5b2010-07-03 10:41:33 +0000508.. opcode:: INPLACE_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000509
510 Implements in-place ``TOS = TOS1 ^ TOS``.
511
512
Georg Brandl4833e5b2010-07-03 10:41:33 +0000513.. opcode:: INPLACE_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000514
515 Implements in-place ``TOS = TOS1 | TOS``.
516
Georg Brandl116aa622007-08-15 14:28:22 +0000517
Georg Brandl4833e5b2010-07-03 10:41:33 +0000518.. opcode:: STORE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000519
520 Implements ``TOS1[TOS] = TOS2``.
521
522
Georg Brandl4833e5b2010-07-03 10:41:33 +0000523.. opcode:: DELETE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000524
525 Implements ``del TOS1[TOS]``.
526
Georg Brandl116aa622007-08-15 14:28:22 +0000527
Yury Selivanov66f88282015-06-24 11:04:15 -0400528**Coroutine opcodes**
Yury Selivanov75445082015-05-11 22:57:16 -0400529
530.. opcode:: GET_AWAITABLE
531
Yury Selivanov66f88282015-06-24 11:04:15 -0400532 Implements ``TOS = get_awaitable(TOS)``, where ``get_awaitable(o)``
533 returns ``o`` if ``o`` is a coroutine object or a generator object with
534 the CO_ITERABLE_COROUTINE flag, or resolves
535 ``o.__await__``.
Yury Selivanov75445082015-05-11 22:57:16 -0400536
537
538.. opcode:: GET_AITER
539
540 Implements ``TOS = get_awaitable(TOS.__aiter__())``. See ``GET_AWAITABLE``
541 for details about ``get_awaitable``
542
543
544.. opcode:: GET_ANEXT
545
546 Implements ``PUSH(get_awaitable(TOS.__anext__()))``. See ``GET_AWAITABLE``
547 for details about ``get_awaitable``
548
549
550.. opcode:: BEFORE_ASYNC_WITH
551
552 Resolves ``__aenter__`` and ``__aexit__`` from the object on top of the
553 stack. Pushes ``__aexit__`` and result of ``__aenter__()`` to the stack.
554
555
556.. opcode:: SETUP_ASYNC_WITH
557
558 Creates a new frame object.
559
560
561
Georg Brandl4833e5b2010-07-03 10:41:33 +0000562**Miscellaneous opcodes**
Georg Brandl116aa622007-08-15 14:28:22 +0000563
Georg Brandl4833e5b2010-07-03 10:41:33 +0000564.. opcode:: PRINT_EXPR
Georg Brandl116aa622007-08-15 14:28:22 +0000565
566 Implements the expression statement for the interactive mode. TOS is removed
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500567 from the stack and printed. In non-interactive mode, an expression statement
568 is terminated with :opcode:`POP_TOP`.
Georg Brandl116aa622007-08-15 14:28:22 +0000569
570
Georg Brandl4833e5b2010-07-03 10:41:33 +0000571.. opcode:: BREAK_LOOP
Georg Brandl116aa622007-08-15 14:28:22 +0000572
573 Terminates a loop due to a :keyword:`break` statement.
574
575
576.. opcode:: CONTINUE_LOOP (target)
577
578 Continues a loop due to a :keyword:`continue` statement. *target* is the
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200579 address to jump to (which should be a :opcode:`FOR_ITER` instruction).
Georg Brandl116aa622007-08-15 14:28:22 +0000580
581
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000582.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000583
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000584 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000585
586
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000587.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000588
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000589 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
590
591
592.. opcode:: MAP_ADD (i)
593
594 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
595 comprehensions.
596
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200597For all of the :opcode:`SET_ADD`, :opcode:`LIST_APPEND` and :opcode:`MAP_ADD`
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500598instructions, while the added value or key/value pair is popped off, the
599container object remains on the stack so that it is available for further
600iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000601
602
Georg Brandl4833e5b2010-07-03 10:41:33 +0000603.. opcode:: RETURN_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000604
605 Returns with TOS to the caller of the function.
606
607
Georg Brandl4833e5b2010-07-03 10:41:33 +0000608.. opcode:: YIELD_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000609
Berker Peksagab4040e2015-03-02 06:33:30 +0200610 Pops TOS and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000611
612
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000613.. opcode:: YIELD_FROM
614
Berker Peksagab4040e2015-03-02 06:33:30 +0200615 Pops TOS and delegates to it as a subiterator from a :term:`generator`.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000616
617 .. versionadded:: 3.3
618
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700619.. opcode:: SETUP_ANNOTATIONS
620
621 Checks whether ``__annotations__`` is defined in ``locals()``, if not it is
Martin Panterb1321fb2016-10-10 00:38:21 +0000622 set up to an empty ``dict``. This opcode is only emitted if a class
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700623 or module body contains :term:`variable annotations <variable annotation>`
624 statically.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000625
Berker Peksag34b74ff2016-09-12 08:00:01 +0300626 .. versionadded:: 3.6
627
Georg Brandl4833e5b2010-07-03 10:41:33 +0000628.. opcode:: IMPORT_STAR
Georg Brandl116aa622007-08-15 14:28:22 +0000629
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500630 Loads all symbols not starting with ``'_'`` directly from the module TOS to
631 the local namespace. The module is popped after loading all names. This
632 opcode implements ``from module import *``.
Georg Brandl116aa622007-08-15 14:28:22 +0000633
634
Georg Brandl4833e5b2010-07-03 10:41:33 +0000635.. opcode:: POP_BLOCK
Georg Brandl116aa622007-08-15 14:28:22 +0000636
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500637 Removes one block from the block stack. Per frame, there is a stack of
638 blocks, denoting nested loops, try statements, and such.
Georg Brandl116aa622007-08-15 14:28:22 +0000639
640
Georg Brandl4833e5b2010-07-03 10:41:33 +0000641.. opcode:: POP_EXCEPT
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000642
643 Removes one block from the block stack. The popped block must be an exception
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500644 handler block, as implicitly created when entering an except handler. In
645 addition to popping extraneous values from the frame stack, the last three
646 popped values are used to restore the exception state.
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000647
648
Georg Brandl4833e5b2010-07-03 10:41:33 +0000649.. opcode:: END_FINALLY
Georg Brandl116aa622007-08-15 14:28:22 +0000650
651 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
652 exception has to be re-raised, or whether the function returns, and continues
653 with the outer-next block.
654
655
Georg Brandl4833e5b2010-07-03 10:41:33 +0000656.. opcode:: LOAD_BUILD_CLASS
Georg Brandl116aa622007-08-15 14:28:22 +0000657
Georg Brandl5ac22302008-07-20 21:39:03 +0000658 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200659 by :opcode:`CALL_FUNCTION` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000660
Guido van Rossum04110fb2007-08-24 16:32:05 +0000661
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000662.. opcode:: SETUP_WITH (delta)
663
664 This opcode performs several operations before a with block starts. First,
665 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
666 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
667 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
668 is pushed. Finally, the result of calling the enter method is pushed onto
669 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
670 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
671 :opcode:`UNPACK_SEQUENCE`).
672
673
Yury Selivanov75445082015-05-11 22:57:16 -0400674.. opcode:: WITH_CLEANUP_START
Guido van Rossum04110fb2007-08-24 16:32:05 +0000675
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500676 Cleans up the stack when a :keyword:`with` statement block exits. TOS is the
677 context manager's :meth:`__exit__` bound method. Below TOS are 1--3 values
678 indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000679
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000680 * SECOND = ``None``
681 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
682 * SECOND = ``WHY_*``; no retval below it
683 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000684
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000685 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
Yury Selivanov75445082015-05-11 22:57:16 -0400686 ``TOS(None, None, None)``. Pushes SECOND and result of the call
687 to the stack.
688
689
690.. opcode:: WITH_CLEANUP_FINISH
691
692 Pops exception type and result of 'exit' function call from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000693
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500694 If the stack represents an exception, *and* the function call returns a
695 'true' value, this information is "zapped" and replaced with a single
696 ``WHY_SILENCED`` to prevent :opcode:`END_FINALLY` from re-raising the
697 exception. (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000698
Georg Brandl9afde1c2007-11-01 20:32:30 +0000699 .. XXX explain the WHY stuff!
700
Guido van Rossum04110fb2007-08-24 16:32:05 +0000701
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300702All of the following opcodes use their arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000703
Georg Brandl116aa622007-08-15 14:28:22 +0000704.. opcode:: STORE_NAME (namei)
705
706 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500707 :attr:`co_names` of the code object. The compiler tries to use
708 :opcode:`STORE_FAST` or :opcode:`STORE_GLOBAL` if possible.
Georg Brandl116aa622007-08-15 14:28:22 +0000709
710
711.. opcode:: DELETE_NAME (namei)
712
713 Implements ``del name``, where *namei* is the index into :attr:`co_names`
714 attribute of the code object.
715
716
717.. opcode:: UNPACK_SEQUENCE (count)
718
719 Unpacks TOS into *count* individual values, which are put onto the stack
720 right-to-left.
721
Georg Brandl116aa622007-08-15 14:28:22 +0000722
Georg Brandl5ac22302008-07-20 21:39:03 +0000723.. opcode:: UNPACK_EX (counts)
724
725 Implements assignment with a starred target: Unpacks an iterable in TOS into
726 individual values, where the total number of values can be smaller than the
Martin Pantercc71a792016-04-05 06:19:42 +0000727 number of items in the iterable: one of the new values will be a list of all
Georg Brandl5ac22302008-07-20 21:39:03 +0000728 leftover items.
729
730 The low byte of *counts* is the number of values before the list value, the
731 high byte of *counts* the number of values after it. The resulting values
732 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000733
Georg Brandl5ac22302008-07-20 21:39:03 +0000734
Georg Brandl116aa622007-08-15 14:28:22 +0000735.. opcode:: STORE_ATTR (namei)
736
737 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
738 :attr:`co_names`.
739
740
741.. opcode:: DELETE_ATTR (namei)
742
743 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
744
745
746.. opcode:: STORE_GLOBAL (namei)
747
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200748 Works as :opcode:`STORE_NAME`, but stores the name as a global.
Georg Brandl116aa622007-08-15 14:28:22 +0000749
750
751.. opcode:: DELETE_GLOBAL (namei)
752
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200753 Works as :opcode:`DELETE_NAME`, but deletes a global name.
Georg Brandl116aa622007-08-15 14:28:22 +0000754
Georg Brandl116aa622007-08-15 14:28:22 +0000755
756.. opcode:: LOAD_CONST (consti)
757
758 Pushes ``co_consts[consti]`` onto the stack.
759
760
761.. opcode:: LOAD_NAME (namei)
762
763 Pushes the value associated with ``co_names[namei]`` onto the stack.
764
765
766.. opcode:: BUILD_TUPLE (count)
767
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500768 Creates a tuple consuming *count* items from the stack, and pushes the
769 resulting tuple onto the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000770
771
772.. opcode:: BUILD_LIST (count)
773
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200774 Works as :opcode:`BUILD_TUPLE`, but creates a list.
Georg Brandl116aa622007-08-15 14:28:22 +0000775
776
777.. opcode:: BUILD_SET (count)
778
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200779 Works as :opcode:`BUILD_TUPLE`, but creates a set.
Georg Brandl116aa622007-08-15 14:28:22 +0000780
781
Christian Heimesa62da1d2008-01-12 19:39:10 +0000782.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000783
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100784 Pushes a new dictionary object onto the stack. Pops ``2 * count`` items
785 so that the dictionary holds *count* entries:
786 ``{..., TOS3: TOS2, TOS1: TOS}``.
787
788 .. versionchanged:: 3.5
789 The dictionary is created from stack items instead of creating an
790 empty dictionary pre-sized to hold *count* items.
Georg Brandl116aa622007-08-15 14:28:22 +0000791
792
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +0300793.. opcode:: BUILD_CONST_KEY_MAP (count)
794
795 The version of :opcode:`BUILD_MAP` specialized for constant keys. *count*
796 values are consumed from the stack. The top element on the stack contains
797 a tuple of keys.
798
799 .. versionadded:: 3.6
800
801
Serhiy Storchakaea525a22016-09-06 22:07:53 +0300802.. opcode:: BUILD_STRING (count)
803
804 Concatenates *count* strings from the stack and pushes the resulting string
805 onto the stack.
806
807 .. versionadded:: 3.6
808
809
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100810.. opcode:: BUILD_TUPLE_UNPACK (count)
811
812 Pops *count* iterables from the stack, joins them in a single tuple,
813 and pushes the result. Implements iterable unpacking in tuple
814 displays ``(*x, *y, *z)``.
815
816 .. versionadded:: 3.5
817
818
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100819.. opcode:: BUILD_TUPLE_UNPACK_WITH_CALL (count)
820
821 This is similar to :opcode:`BUILD_TUPLE_UNPACK`,
822 but is used for ``f(*x, *y, *z)`` call syntax. The stack item at position
823 ``count + 1`` should be the corresponding callable ``f``.
824
825 .. versionadded:: 3.6
826
827
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100828.. opcode:: BUILD_LIST_UNPACK (count)
829
830 This is similar to :opcode:`BUILD_TUPLE_UNPACK`, but pushes a list
831 instead of tuple. Implements iterable unpacking in list
832 displays ``[*x, *y, *z]``.
833
834 .. versionadded:: 3.5
835
836
837.. opcode:: BUILD_SET_UNPACK (count)
838
839 This is similar to :opcode:`BUILD_TUPLE_UNPACK`, but pushes a set
840 instead of tuple. Implements iterable unpacking in set
841 displays ``{*x, *y, *z}``.
842
843 .. versionadded:: 3.5
844
845
846.. opcode:: BUILD_MAP_UNPACK (count)
847
848 Pops *count* mappings from the stack, merges them into a single dictionary,
849 and pushes the result. Implements dictionary unpacking in dictionary
850 displays ``{**x, **y, **z}``.
851
852 .. versionadded:: 3.5
853
854
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100855.. opcode:: BUILD_MAP_UNPACK_WITH_CALL (count)
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100856
857 This is similar to :opcode:`BUILD_MAP_UNPACK`,
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100858 but is used for ``f(**x, **y, **z)`` call syntax. The stack item at
859 position ``count + 2`` should be the corresponding callable ``f``.
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100860
861 .. versionadded:: 3.5
Ivan Levkivskyi7e52c3e2017-03-10 23:16:44 +0100862 .. versionchanged:: 3.6
863 The position of the callable is determined by adding 2 to the opcode
864 argument instead of encoding it in the second byte of the argument.
Ivan Levkivskyi0705f662017-03-03 22:46:39 +0100865
866
Georg Brandl116aa622007-08-15 14:28:22 +0000867.. opcode:: LOAD_ATTR (namei)
868
869 Replaces TOS with ``getattr(TOS, co_names[namei])``.
870
871
872.. opcode:: COMPARE_OP (opname)
873
874 Performs a Boolean operation. The operation name can be found in
875 ``cmp_op[opname]``.
876
877
878.. opcode:: IMPORT_NAME (namei)
879
Christian Heimesa342c012008-04-20 21:01:16 +0000880 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
881 the *fromlist* and *level* arguments of :func:`__import__`. The module
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500882 object is pushed onto the stack. The current namespace is not affected: for
883 a proper import statement, a subsequent :opcode:`STORE_FAST` instruction
Christian Heimesa342c012008-04-20 21:01:16 +0000884 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000885
886
887.. opcode:: IMPORT_FROM (namei)
888
889 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
890 resulting object is pushed onto the stack, to be subsequently stored by a
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200891 :opcode:`STORE_FAST` instruction.
Georg Brandl116aa622007-08-15 14:28:22 +0000892
893
894.. opcode:: JUMP_FORWARD (delta)
895
Georg Brandl9afde1c2007-11-01 20:32:30 +0000896 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000897
898
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000899.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000900
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000901 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000902
903
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000904.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000905
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000906 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
907
908
909.. opcode:: JUMP_IF_TRUE_OR_POP (target)
910
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500911 If TOS is true, sets the bytecode counter to *target* and leaves TOS on the
912 stack. Otherwise (TOS is false), TOS is popped.
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000913
914
915.. opcode:: JUMP_IF_FALSE_OR_POP (target)
916
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500917 If TOS is false, sets the bytecode counter to *target* and leaves TOS on the
918 stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000919
920
921.. opcode:: JUMP_ABSOLUTE (target)
922
Georg Brandl9afde1c2007-11-01 20:32:30 +0000923 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000924
925
926.. opcode:: FOR_ITER (delta)
927
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500928 TOS is an :term:`iterator`. Call its :meth:`~iterator.__next__` method. If
929 this yields a new value, push it on the stack (leaving the iterator below
930 it). If the iterator indicates it is exhausted TOS is popped, and the byte
931 code counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000932
Georg Brandl116aa622007-08-15 14:28:22 +0000933
934.. opcode:: LOAD_GLOBAL (namei)
935
936 Loads the global named ``co_names[namei]`` onto the stack.
937
Georg Brandl116aa622007-08-15 14:28:22 +0000938
939.. opcode:: SETUP_LOOP (delta)
940
941 Pushes a block for a loop onto the block stack. The block spans from the
942 current instruction with a size of *delta* bytes.
943
944
945.. opcode:: SETUP_EXCEPT (delta)
946
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500947 Pushes a try block from a try-except clause onto the block stack. *delta*
948 points to the first except block.
Georg Brandl116aa622007-08-15 14:28:22 +0000949
950
951.. opcode:: SETUP_FINALLY (delta)
952
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500953 Pushes a try block from a try-except clause onto the block stack. *delta*
954 points to the finally block.
Georg Brandl116aa622007-08-15 14:28:22 +0000955
956
957.. opcode:: LOAD_FAST (var_num)
958
959 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
960
961
962.. opcode:: STORE_FAST (var_num)
963
964 Stores TOS into the local ``co_varnames[var_num]``.
965
966
967.. opcode:: DELETE_FAST (var_num)
968
969 Deletes local ``co_varnames[var_num]``.
970
971
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700972.. opcode:: STORE_ANNOTATION (namei)
973
974 Stores TOS as ``locals()['__annotations__'][co_names[namei]] = TOS``.
975
Berker Peksag34b74ff2016-09-12 08:00:01 +0300976 .. versionadded:: 3.6
977
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700978
Georg Brandl116aa622007-08-15 14:28:22 +0000979.. opcode:: LOAD_CLOSURE (i)
980
981 Pushes a reference to the cell contained in slot *i* of the cell and free
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500982 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
983 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
Georg Brandl116aa622007-08-15 14:28:22 +0000984 len(co_cellvars)]``.
985
986
987.. opcode:: LOAD_DEREF (i)
988
989 Loads the cell contained in slot *i* of the cell and free variable storage.
990 Pushes a reference to the object the cell contains on the stack.
991
992
Benjamin Peterson3b0431d2013-04-30 09:41:40 -0400993.. opcode:: LOAD_CLASSDEREF (i)
994
995 Much like :opcode:`LOAD_DEREF` but first checks the locals dictionary before
996 consulting the cell. This is used for loading free variables in class
997 bodies.
998
999
Georg Brandl116aa622007-08-15 14:28:22 +00001000.. opcode:: STORE_DEREF (i)
1001
1002 Stores TOS into the cell contained in slot *i* of the cell and free variable
1003 storage.
1004
1005
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001006.. opcode:: DELETE_DEREF (i)
1007
1008 Empties the cell contained in slot *i* of the cell and free variable storage.
1009 Used by the :keyword:`del` statement.
1010
1011
Georg Brandl116aa622007-08-15 14:28:22 +00001012.. opcode:: RAISE_VARARGS (argc)
1013
1014 Raises an exception. *argc* indicates the number of parameters to the raise
1015 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
1016 the parameter as TOS1, and the exception as TOS.
1017
1018
1019.. opcode:: CALL_FUNCTION (argc)
1020
Ivan Levkivskyi4b2a2a42017-03-10 23:52:35 +01001021 Calls a function. *argc* indicates the number of positional arguments.
1022 The positional arguments are on the stack, with the right-most argument
1023 on top. Below the arguments, the function object to call is on the stack.
1024 Pops all function arguments, and the function itself off the stack, and
1025 pushes the return value.
1026
1027 .. versionchanged:: 3.6
1028 This opcode is used only for calls with positional arguments.
1029
1030
1031.. opcode:: CALL_FUNCTION_KW (argc)
1032
1033 Calls a function. *argc* indicates the number of arguments (positional
1034 and keyword). The top element on the stack contains a tuple of keyword
1035 argument names. Below the tuple, keyword arguments are on the stack, in
1036 the order corresponding to the tuple. Below the keyword arguments, the
1037 positional arguments are on the stack, with the right-most parameter on
1038 top. Below the arguments, the function object to call is on the stack.
1039 Pops all function arguments, and the function itself off the stack, and
1040 pushes the return value.
1041
1042 .. versionchanged:: 3.6
1043 Keyword arguments are packed in a tuple instead of a dictionary,
1044 *argc* indicates the total number of arguments
1045
1046
1047.. opcode:: CALL_FUNCTION_EX (flags)
1048
1049 Calls a function. The lowest bit of *flags* indicates whether the
1050 var-keyword argument is placed at the top of the stack. Below the
1051 var-keyword argument, the var-positional argument is on the stack.
1052 Below the arguments, the function object to call is placed.
1053 Pops all function arguments, and the function itself off the stack, and
1054 pushes the return value. Note that this opcode pops at most three items
1055 from the stack. Var-positional and var-keyword arguments are packed
1056 by :opcode:`BUILD_MAP_UNPACK_WITH_CALL` and
1057 :opcode:`BUILD_MAP_UNPACK_WITH_CALL`.
1058
1059 .. versionadded:: 3.6
Georg Brandl116aa622007-08-15 14:28:22 +00001060
1061
INADA Naoki015bce62017-01-16 17:23:30 +09001062.. opcode:: LOAD_METHOD (namei)
1063
1064 Loads a method named ``co_names[namei]`` from TOS object. TOS is popped and
1065 method and TOS are pushed when interpreter can call unbound method directly.
Ivan Levkivskyi4b2a2a42017-03-10 23:52:35 +01001066 TOS will be used as the first argument (``self``) by :opcode:`CALL_METHOD`.
INADA Naoki015bce62017-01-16 17:23:30 +09001067 Otherwise, ``NULL`` and method is pushed (method is bound method or
1068 something else).
1069
1070 .. versionadded:: 3.7
1071
1072
1073.. opcode:: CALL_METHOD (argc)
1074
1075 Calls a method. *argc* is number of positional arguments.
1076 Keyword arguments are not supported. This opcode is designed to be used
1077 with :opcode:`LOAD_METHOD`. Positional arguments are on top of the stack.
1078 Below them, two items described in :opcode:`LOAD_METHOD` on the stack.
1079 All of them are popped and return value is pushed.
1080
1081 .. versionadded:: 3.7
1082
1083
Georg Brandl116aa622007-08-15 14:28:22 +00001084.. opcode:: MAKE_FUNCTION (argc)
1085
Georg Brandlc96ef1f2013-10-12 18:41:18 +02001086 Pushes a new function object on the stack. From bottom to top, the consumed
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001087 stack must consist of values if the argument carries a specified flag value
Georg Brandlc96ef1f2013-10-12 18:41:18 +02001088
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001089 * ``0x01`` a tuple of default argument objects in positional order
1090 * ``0x02`` a dictionary of keyword-only parameters' default values
1091 * ``0x04`` an annotation dictionary
1092 * ``0x08`` a tuple containing cells for free variables, making a closure
Georg Brandlc96ef1f2013-10-12 18:41:18 +02001093 * the code associated with the function (at TOS1)
1094 * the :term:`qualified name` of the function (at TOS)
Georg Brandl116aa622007-08-15 14:28:22 +00001095
1096
Georg Brandl116aa622007-08-15 14:28:22 +00001097.. opcode:: BUILD_SLICE (argc)
1098
1099 .. index:: builtin: slice
1100
1101 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
1102 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001103 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +00001104
1105
1106.. opcode:: EXTENDED_ARG (ext)
1107
1108 Prefixes any opcode which has an argument too big to fit into the default two
1109 bytes. *ext* holds two additional bytes which, taken together with the
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001110 subsequent opcode's argument, comprise a four-byte argument, *ext* being the
1111 two most-significant bytes.
Georg Brandl116aa622007-08-15 14:28:22 +00001112
1113
Eric V. Smith281d5322015-11-03 13:09:01 -05001114.. opcode:: FORMAT_VALUE (flags)
1115
1116 Used for implementing formatted literal strings (f-strings). Pops
1117 an optional *fmt_spec* from the stack, then a required *value*.
1118 *flags* is interpreted as follows:
1119
Eric V. Smith9ce52e32015-11-03 16:30:49 -05001120 * ``(flags & 0x03) == 0x00``: *value* is formatted as-is.
Eric V. Smith281d5322015-11-03 13:09:01 -05001121 * ``(flags & 0x03) == 0x01``: call :func:`str` on *value* before
1122 formatting it.
1123 * ``(flags & 0x03) == 0x02``: call :func:`repr` on *value* before
1124 formatting it.
1125 * ``(flags & 0x03) == 0x03``: call :func:`ascii` on *value* before
1126 formatting it.
1127 * ``(flags & 0x04) == 0x04``: pop *fmt_spec* from the stack and use
1128 it, else use an empty *fmt_spec*.
1129
Eric V. Smitha3a3d732015-11-04 07:11:13 -05001130 Formatting is performed using :c:func:`PyObject_Format`. The
1131 result is pushed on the stack.
Eric V. Smith281d5322015-11-03 13:09:01 -05001132
Eric V. Smith9ce52e32015-11-03 16:30:49 -05001133 .. versionadded:: 3.6
1134
Eric V. Smith281d5322015-11-03 13:09:01 -05001135
Georg Brandl4833e5b2010-07-03 10:41:33 +00001136.. opcode:: HAVE_ARGUMENT
Georg Brandl116aa622007-08-15 14:28:22 +00001137
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001138 This is not really an opcode. It identifies the dividing line between
Ivan Levkivskyi8f9e1bbf2017-03-24 22:05:04 +01001139 opcodes which don't use their argument and those that do
1140 (``< HAVE_ARGUMENT`` and ``>= HAVE_ARGUMENT``, respectively).
1141
1142 .. versionchanged:: 3.6
1143 Now every instruction has an argument, but opcodes ``< HAVE_ARGUMENT``
1144 ignore it. Before, only opcodes ``>= HAVE_ARGUMENT`` had an argument.
1145
Georg Brandl116aa622007-08-15 14:28:22 +00001146
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001147.. _opcode_collections:
1148
1149Opcode collections
1150------------------
1151
1152These collections are provided for automatic introspection of bytecode
1153instructions:
1154
1155.. data:: opname
1156
1157 Sequence of operation names, indexable using the bytecode.
1158
1159
1160.. data:: opmap
1161
1162 Dictionary mapping operation names to bytecodes.
1163
1164
1165.. data:: cmp_op
1166
1167 Sequence of all compare operation names.
1168
1169
1170.. data:: hasconst
1171
1172 Sequence of bytecodes that have a constant parameter.
1173
1174
1175.. data:: hasfree
1176
Benjamin Petersonbdf525b2015-03-02 09:31:40 -05001177 Sequence of bytecodes that access a free variable (note that 'free' in this
1178 context refers to names in the current scope that are referenced by inner
1179 scopes or names in outer scopes that are referenced from this scope. It does
1180 *not* include references to global or builtin scopes).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001181
1182
1183.. data:: hasname
1184
1185 Sequence of bytecodes that access an attribute by name.
1186
1187
1188.. data:: hasjrel
1189
1190 Sequence of bytecodes that have a relative jump target.
1191
1192
1193.. data:: hasjabs
1194
1195 Sequence of bytecodes that have an absolute jump target.
1196
1197
1198.. data:: haslocal
1199
1200 Sequence of bytecodes that access a local variable.
1201
1202
1203.. data:: hascompare
1204
1205 Sequence of bytecodes of Boolean operations.