blob: 273fb20634e3b641a20f16c3c3c784eedd7fa6f7 [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
Brett Cannon8315fd12010-07-02 22:03:00 +000023
Georg Brandl116aa622007-08-15 14:28:22 +000024Example: Given the function :func:`myfunc`::
25
26 def myfunc(alist):
27 return len(alist)
28
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100029the following command can be used to display the disassembly of
30:func:`myfunc`::
Georg Brandl116aa622007-08-15 14:28:22 +000031
32 >>> dis.dis(myfunc)
33 2 0 LOAD_GLOBAL 0 (len)
34 3 LOAD_FAST 0 (alist)
35 6 CALL_FUNCTION 1
36 9 RETURN_VALUE
37
38(The "2" is a line number).
39
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100040Bytecode analysis
41-----------------
Georg Brandl116aa622007-08-15 14:28:22 +000042
R David Murray0bce6e72014-01-07 14:30:17 -050043.. versionadded:: 3.4
44
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100045The bytecode analysis API allows pieces of Python code to be wrapped in a
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050046:class:`Bytecode` object that provides easy access to details of the compiled
47code.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100048
Nick Coghlan50c48b82013-11-23 00:57:00 +100049.. class:: Bytecode(x, *, first_line=None, current_offset=None)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100050
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050051 Analyse the bytecode corresponding to a function, method, string of source
52 code, or a code object (as returned by :func:`compile`).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100053
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050054 This is a convenience wrapper around many of the functions listed below, most
55 notably :func:`get_instructions`, as iterating over a :class:`Bytecode`
56 instance yields the bytecode operations as :class:`Instruction` instances.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100057
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050058 If *first_line* is not None, it indicates the line number that should be
59 reported for the first source line in the disassembled code. Otherwise, the
60 source line information (if any) is taken directly from the disassembled code
61 object.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100062
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050063 If *current_offset* is not None, it refers to an instruction offset in the
64 disassembled code. Setting this means :meth:`.dis` will display a "current
65 instruction" marker against the specified opcode.
Nick Coghlan50c48b82013-11-23 00:57:00 +100066
67 .. classmethod:: from_traceback(tb)
68
Benjamin Petersonbdf525b2015-03-02 09:31:40 -050069 Construct a :class:`Bytecode` instance from the given traceback, setting
70 *current_offset* to the instruction responsible for the exception.
Nick Coghlan50c48b82013-11-23 00:57:00 +100071
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100072 .. data:: codeobj
73
74 The compiled code object.
75
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100076 .. data:: first_line
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100077
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100078 The first source line of the code object (if available)
79
80 .. method:: dis()
81
Benjamin Peterson29fec922015-03-02 09:27:43 -050082 Return a formatted view of the bytecode operations (the same as printed by
83 :func:`dis.dis`, but returned as a multi-line string).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100084
85 .. method:: info()
86
87 Return a formatted multi-line string with detailed information about the
88 code object, like :func:`code_info`.
89
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100090Example::
91
92 >>> bytecode = dis.Bytecode(myfunc)
93 >>> for instr in bytecode:
94 ... print(instr.opname)
95 ...
96 LOAD_GLOBAL
97 LOAD_FAST
98 CALL_FUNCTION
99 RETURN_VALUE
100
101
102Analysis functions
103------------------
104
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500105The :mod:`dis` module also defines the following analysis functions that convert
106the input directly to the desired output. They can be useful if only a single
107operation is being performed, so the intermediate analysis object isn't useful:
Georg Brandl116aa622007-08-15 14:28:22 +0000108
Nick Coghlane8814fb2010-09-10 14:08:04 +0000109.. function:: code_info(x)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000110
Georg Brandl67b21b72010-08-17 15:07:14 +0000111 Return a formatted multi-line string with detailed code object information
112 for the supplied function, method, source code string or code object.
Nick Coghlaneae2da12010-08-17 08:03:36 +0000113
Georg Brandl67b21b72010-08-17 15:07:14 +0000114 Note that the exact contents of code info strings are highly implementation
115 dependent and they may change arbitrarily across Python VMs or Python
116 releases.
Nick Coghlaneae2da12010-08-17 08:03:36 +0000117
118 .. versionadded:: 3.2
119
Georg Brandl67b21b72010-08-17 15:07:14 +0000120
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000121.. function:: show_code(x, *, file=None)
Nick Coghlane8814fb2010-09-10 14:08:04 +0000122
123 Print detailed code object information for the supplied function, method,
Ezio Melotti6e6c6ac2013-08-23 22:41:39 +0300124 source code string or code object to *file* (or ``sys.stdout`` if *file*
125 is not specified).
Nick Coghlane8814fb2010-09-10 14:08:04 +0000126
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000127 This is a convenient shorthand for ``print(code_info(x), file=file)``,
128 intended for interactive exploration at the interpreter prompt.
Nick Coghlane8814fb2010-09-10 14:08:04 +0000129
130 .. versionadded:: 3.2
131
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000132 .. versionchanged:: 3.4
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200133 Added *file* parameter.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000134
135
136.. function:: dis(x=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000137
Georg Brandl67b21b72010-08-17 15:07:14 +0000138 Disassemble the *x* object. *x* can denote either a module, a class, a
139 method, a function, a code object, a string of source code or a byte sequence
140 of raw bytecode. For a module, it disassembles all functions. For a class,
141 it disassembles all methods. For a code object or sequence of raw bytecode,
142 it prints one line per bytecode instruction. Strings are first compiled to
143 code objects with the :func:`compile` built-in function before being
144 disassembled. If no object is provided, this function disassembles the last
145 traceback.
Georg Brandl116aa622007-08-15 14:28:22 +0000146
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200147 The disassembly is written as text to the supplied *file* argument if
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000148 provided and to ``sys.stdout`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000149
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000150 .. versionchanged:: 3.4
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200151 Added *file* parameter.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000152
153
154.. function:: distb(tb=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Georg Brandl4833e5b2010-07-03 10:41:33 +0000156 Disassemble the top-of-stack function of a traceback, using the last
157 traceback if none was passed. The instruction causing the exception is
158 indicated.
Georg Brandl116aa622007-08-15 14:28:22 +0000159
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200160 The disassembly is written as text to the supplied *file* argument if
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000161 provided and to ``sys.stdout`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000163 .. versionchanged:: 3.4
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200164 Added *file* parameter.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000165
166
167.. function:: disassemble(code, lasti=-1, *, file=None)
168 disco(code, lasti=-1, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000169
Georg Brandl4833e5b2010-07-03 10:41:33 +0000170 Disassemble a code object, indicating the last instruction if *lasti* was
Georg Brandl116aa622007-08-15 14:28:22 +0000171 provided. The output is divided in the following columns:
172
173 #. the line number, for the first instruction of each line
174 #. the current instruction, indicated as ``-->``,
175 #. a labelled instruction, indicated with ``>>``,
176 #. the address of the instruction,
177 #. the operation code name,
178 #. operation parameters, and
179 #. interpretation of the parameters in parentheses.
180
181 The parameter interpretation recognizes local and global variable names,
182 constant values, branch targets, and compare operators.
183
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200184 The disassembly is written as text to the supplied *file* argument if
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000185 provided and to ``sys.stdout`` otherwise.
186
187 .. versionchanged:: 3.4
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200188 Added *file* parameter.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000189
190
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000191.. function:: get_instructions(x, *, first_line=None)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000192
193 Return an iterator over the instructions in the supplied function, method,
194 source code string or code object.
195
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500196 The iterator generates a series of :class:`Instruction` named tuples giving
197 the details of each operation in the supplied code.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000198
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500199 If *first_line* is not None, it indicates the line number that should be
200 reported for the first source line in the disassembled code. Otherwise, the
201 source line information (if any) is taken directly from the disassembled code
202 object.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000203
204 .. versionadded:: 3.4
205
Georg Brandl116aa622007-08-15 14:28:22 +0000206
Benjamin Peterson75edad02009-01-01 15:05:06 +0000207.. function:: findlinestarts(code)
208
209 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
210 attributes of the code object *code* to find the offsets which are starts of
211 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
212
213
214.. function:: findlabels(code)
215
216 Detect all offsets in the code object *code* which are jump targets, and
217 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +0000218
Larry Hastings3a907972013-11-23 14:49:22 -0800219
220.. function:: stack_effect(opcode, [oparg])
221
222 Compute the stack effect of *opcode* with argument *oparg*.
223
224 .. versionadded:: 3.4
225
Georg Brandl116aa622007-08-15 14:28:22 +0000226.. _bytecodes:
227
Georg Brandl9afde1c2007-11-01 20:32:30 +0000228Python Bytecode Instructions
229----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000230
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000231The :func:`get_instructions` function and :class:`Bytecode` class provide
232details of bytecode instructions as :class:`Instruction` instances:
233
234.. class:: Instruction
235
236 Details for a bytecode operation
237
238 .. data:: opcode
239
240 numeric code for operation, corresponding to the opcode values listed
241 below and the bytecode values in the :ref:`opcode_collections`.
242
243
244 .. data:: opname
245
246 human readable name for operation
247
248
249 .. data:: arg
250
251 numeric argument to operation (if any), otherwise None
252
253
254 .. data:: argval
255
256 resolved arg value (if known), otherwise same as arg
257
258
259 .. data:: argrepr
260
261 human readable description of operation argument
262
263
264 .. data:: offset
265
266 start index of operation within bytecode sequence
267
268
269 .. data:: starts_line
270
271 line started by this opcode (if any), otherwise None
272
273
274 .. data:: is_jump_target
275
Serhiy Storchaka0e90e992013-11-29 12:19:53 +0200276 ``True`` if other code jumps to here, otherwise ``False``
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000277
278 .. versionadded:: 3.4
279
280
Georg Brandl9afde1c2007-11-01 20:32:30 +0000281The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000282
283
Georg Brandl4833e5b2010-07-03 10:41:33 +0000284**General instructions**
285
Georg Brandl4833e5b2010-07-03 10:41:33 +0000286.. opcode:: NOP
Georg Brandl116aa622007-08-15 14:28:22 +0000287
288 Do nothing code. Used as a placeholder by the bytecode optimizer.
289
290
Georg Brandl4833e5b2010-07-03 10:41:33 +0000291.. opcode:: POP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000292
293 Removes the top-of-stack (TOS) item.
294
295
Georg Brandl4833e5b2010-07-03 10:41:33 +0000296.. opcode:: ROT_TWO
Georg Brandl116aa622007-08-15 14:28:22 +0000297
298 Swaps the two top-most stack items.
299
300
Georg Brandl4833e5b2010-07-03 10:41:33 +0000301.. opcode:: ROT_THREE
Georg Brandl116aa622007-08-15 14:28:22 +0000302
303 Lifts second and third stack item one position up, moves top down to position
304 three.
305
306
Georg Brandl4833e5b2010-07-03 10:41:33 +0000307.. opcode:: DUP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000308
309 Duplicates the reference on top of the stack.
310
Georg Brandl4833e5b2010-07-03 10:41:33 +0000311
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000312.. opcode:: DUP_TOP_TWO
313
314 Duplicates the two references on top of the stack, leaving them in the
315 same order.
316
317
Georg Brandl4833e5b2010-07-03 10:41:33 +0000318**Unary operations**
319
320Unary operations take the top of the stack, apply the operation, and push the
Georg Brandl116aa622007-08-15 14:28:22 +0000321result back on the stack.
322
Georg Brandl4833e5b2010-07-03 10:41:33 +0000323.. opcode:: UNARY_POSITIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000324
325 Implements ``TOS = +TOS``.
326
327
Georg Brandl4833e5b2010-07-03 10:41:33 +0000328.. opcode:: UNARY_NEGATIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000329
330 Implements ``TOS = -TOS``.
331
332
Georg Brandl4833e5b2010-07-03 10:41:33 +0000333.. opcode:: UNARY_NOT
Georg Brandl116aa622007-08-15 14:28:22 +0000334
335 Implements ``TOS = not TOS``.
336
337
Georg Brandl4833e5b2010-07-03 10:41:33 +0000338.. opcode:: UNARY_INVERT
Georg Brandl116aa622007-08-15 14:28:22 +0000339
340 Implements ``TOS = ~TOS``.
341
342
Georg Brandl4833e5b2010-07-03 10:41:33 +0000343.. opcode:: GET_ITER
Georg Brandl116aa622007-08-15 14:28:22 +0000344
345 Implements ``TOS = iter(TOS)``.
346
Georg Brandl4833e5b2010-07-03 10:41:33 +0000347
348**Binary operations**
349
Georg Brandl116aa622007-08-15 14:28:22 +0000350Binary operations remove the top of the stack (TOS) and the second top-most
351stack item (TOS1) from the stack. They perform the operation, and put the
352result back on the stack.
353
Georg Brandl4833e5b2010-07-03 10:41:33 +0000354.. opcode:: BINARY_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000355
356 Implements ``TOS = TOS1 ** TOS``.
357
358
Georg Brandl4833e5b2010-07-03 10:41:33 +0000359.. opcode:: BINARY_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000360
361 Implements ``TOS = TOS1 * TOS``.
362
363
Georg Brandl4833e5b2010-07-03 10:41:33 +0000364.. opcode:: BINARY_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000365
366 Implements ``TOS = TOS1 // TOS``.
367
368
Georg Brandl4833e5b2010-07-03 10:41:33 +0000369.. opcode:: BINARY_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000370
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000371 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000372
373
Georg Brandl4833e5b2010-07-03 10:41:33 +0000374.. opcode:: BINARY_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000375
376 Implements ``TOS = TOS1 % TOS``.
377
378
Georg Brandl4833e5b2010-07-03 10:41:33 +0000379.. opcode:: BINARY_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000380
381 Implements ``TOS = TOS1 + TOS``.
382
383
Georg Brandl4833e5b2010-07-03 10:41:33 +0000384.. opcode:: BINARY_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000385
386 Implements ``TOS = TOS1 - TOS``.
387
388
Georg Brandl4833e5b2010-07-03 10:41:33 +0000389.. opcode:: BINARY_SUBSCR
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_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000395
396 Implements ``TOS = TOS1 << TOS``.
397
398
Georg Brandl4833e5b2010-07-03 10:41:33 +0000399.. opcode:: BINARY_RSHIFT
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_AND
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_XOR
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_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000415
416 Implements ``TOS = TOS1 | TOS``.
417
Georg Brandl4833e5b2010-07-03 10:41:33 +0000418
419**In-place operations**
420
Georg Brandl116aa622007-08-15 14:28:22 +0000421In-place operations are like binary operations, in that they remove TOS and
422TOS1, and push the result back on the stack, but the operation is done in-place
423when TOS1 supports it, and the resulting TOS may be (but does not have to be)
424the original TOS1.
425
Georg Brandl4833e5b2010-07-03 10:41:33 +0000426.. opcode:: INPLACE_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000427
428 Implements in-place ``TOS = TOS1 ** TOS``.
429
430
Georg Brandl4833e5b2010-07-03 10:41:33 +0000431.. opcode:: INPLACE_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000432
433 Implements in-place ``TOS = TOS1 * TOS``.
434
435
Georg Brandl4833e5b2010-07-03 10:41:33 +0000436.. opcode:: INPLACE_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000437
438 Implements in-place ``TOS = TOS1 // TOS``.
439
440
Georg Brandl4833e5b2010-07-03 10:41:33 +0000441.. opcode:: INPLACE_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000442
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000443 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000444
445
Georg Brandl4833e5b2010-07-03 10:41:33 +0000446.. opcode:: INPLACE_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000447
448 Implements in-place ``TOS = TOS1 % TOS``.
449
450
Georg Brandl4833e5b2010-07-03 10:41:33 +0000451.. opcode:: INPLACE_ADD
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_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000457
458 Implements in-place ``TOS = TOS1 - TOS``.
459
460
Georg Brandl4833e5b2010-07-03 10:41:33 +0000461.. opcode:: INPLACE_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000462
463 Implements in-place ``TOS = TOS1 << TOS``.
464
465
Georg Brandl4833e5b2010-07-03 10:41:33 +0000466.. opcode:: INPLACE_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000467
468 Implements in-place ``TOS = TOS1 >> TOS``.
469
470
Georg Brandl4833e5b2010-07-03 10:41:33 +0000471.. opcode:: INPLACE_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000472
473 Implements in-place ``TOS = TOS1 & TOS``.
474
475
Georg Brandl4833e5b2010-07-03 10:41:33 +0000476.. opcode:: INPLACE_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000477
478 Implements in-place ``TOS = TOS1 ^ TOS``.
479
480
Georg Brandl4833e5b2010-07-03 10:41:33 +0000481.. opcode:: INPLACE_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000482
483 Implements in-place ``TOS = TOS1 | TOS``.
484
Georg Brandl116aa622007-08-15 14:28:22 +0000485
Georg Brandl4833e5b2010-07-03 10:41:33 +0000486.. opcode:: STORE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000487
488 Implements ``TOS1[TOS] = TOS2``.
489
490
Georg Brandl4833e5b2010-07-03 10:41:33 +0000491.. opcode:: DELETE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000492
493 Implements ``del TOS1[TOS]``.
494
Georg Brandl116aa622007-08-15 14:28:22 +0000495
Georg Brandl4833e5b2010-07-03 10:41:33 +0000496**Miscellaneous opcodes**
Georg Brandl116aa622007-08-15 14:28:22 +0000497
Georg Brandl4833e5b2010-07-03 10:41:33 +0000498.. opcode:: PRINT_EXPR
Georg Brandl116aa622007-08-15 14:28:22 +0000499
500 Implements the expression statement for the interactive mode. TOS is removed
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500501 from the stack and printed. In non-interactive mode, an expression statement
502 is terminated with :opcode:`POP_TOP`.
Georg Brandl116aa622007-08-15 14:28:22 +0000503
504
Georg Brandl4833e5b2010-07-03 10:41:33 +0000505.. opcode:: BREAK_LOOP
Georg Brandl116aa622007-08-15 14:28:22 +0000506
507 Terminates a loop due to a :keyword:`break` statement.
508
509
510.. opcode:: CONTINUE_LOOP (target)
511
512 Continues a loop due to a :keyword:`continue` statement. *target* is the
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200513 address to jump to (which should be a :opcode:`FOR_ITER` instruction).
Georg Brandl116aa622007-08-15 14:28:22 +0000514
515
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000516.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000517
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000518 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000519
520
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000521.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000522
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000523 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
524
525
526.. opcode:: MAP_ADD (i)
527
528 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
529 comprehensions.
530
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200531For all of the :opcode:`SET_ADD`, :opcode:`LIST_APPEND` and :opcode:`MAP_ADD`
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500532instructions, while the added value or key/value pair is popped off, the
533container object remains on the stack so that it is available for further
534iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000535
536
Georg Brandl4833e5b2010-07-03 10:41:33 +0000537.. opcode:: RETURN_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000538
539 Returns with TOS to the caller of the function.
540
541
Georg Brandl4833e5b2010-07-03 10:41:33 +0000542.. opcode:: YIELD_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000543
Berker Peksagab4040e2015-03-02 06:33:30 +0200544 Pops TOS and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000545
546
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000547.. opcode:: YIELD_FROM
548
Berker Peksagab4040e2015-03-02 06:33:30 +0200549 Pops TOS and delegates to it as a subiterator from a :term:`generator`.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000550
551 .. versionadded:: 3.3
552
553
Georg Brandl4833e5b2010-07-03 10:41:33 +0000554.. opcode:: IMPORT_STAR
Georg Brandl116aa622007-08-15 14:28:22 +0000555
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500556 Loads all symbols not starting with ``'_'`` directly from the module TOS to
557 the local namespace. The module is popped after loading all names. This
558 opcode implements ``from module import *``.
Georg Brandl116aa622007-08-15 14:28:22 +0000559
560
Georg Brandl4833e5b2010-07-03 10:41:33 +0000561.. opcode:: POP_BLOCK
Georg Brandl116aa622007-08-15 14:28:22 +0000562
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500563 Removes one block from the block stack. Per frame, there is a stack of
564 blocks, denoting nested loops, try statements, and such.
Georg Brandl116aa622007-08-15 14:28:22 +0000565
566
Georg Brandl4833e5b2010-07-03 10:41:33 +0000567.. opcode:: POP_EXCEPT
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000568
569 Removes one block from the block stack. The popped block must be an exception
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500570 handler block, as implicitly created when entering an except handler. In
571 addition to popping extraneous values from the frame stack, the last three
572 popped values are used to restore the exception state.
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000573
574
Georg Brandl4833e5b2010-07-03 10:41:33 +0000575.. opcode:: END_FINALLY
Georg Brandl116aa622007-08-15 14:28:22 +0000576
577 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
578 exception has to be re-raised, or whether the function returns, and continues
579 with the outer-next block.
580
581
Georg Brandl4833e5b2010-07-03 10:41:33 +0000582.. opcode:: LOAD_BUILD_CLASS
Georg Brandl116aa622007-08-15 14:28:22 +0000583
Georg Brandl5ac22302008-07-20 21:39:03 +0000584 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200585 by :opcode:`CALL_FUNCTION` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000586
Guido van Rossum04110fb2007-08-24 16:32:05 +0000587
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000588.. opcode:: SETUP_WITH (delta)
589
590 This opcode performs several operations before a with block starts. First,
591 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
592 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
593 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
594 is pushed. Finally, the result of calling the enter method is pushed onto
595 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
596 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
597 :opcode:`UNPACK_SEQUENCE`).
598
599
Georg Brandl4833e5b2010-07-03 10:41:33 +0000600.. opcode:: WITH_CLEANUP
Guido van Rossum04110fb2007-08-24 16:32:05 +0000601
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500602 Cleans up the stack when a :keyword:`with` statement block exits. TOS is the
603 context manager's :meth:`__exit__` bound method. Below TOS are 1--3 values
604 indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000605
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000606 * SECOND = ``None``
607 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
608 * SECOND = ``WHY_*``; no retval below it
609 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000610
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000611 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
612 ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000613
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500614 If the stack represents an exception, *and* the function call returns a
615 'true' value, this information is "zapped" and replaced with a single
616 ``WHY_SILENCED`` to prevent :opcode:`END_FINALLY` from re-raising the
617 exception. (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000618
Georg Brandl9afde1c2007-11-01 20:32:30 +0000619 .. XXX explain the WHY stuff!
620
Guido van Rossum04110fb2007-08-24 16:32:05 +0000621
Georg Brandl116aa622007-08-15 14:28:22 +0000622All of the following opcodes expect arguments. An argument is two bytes, with
623the more significant byte last.
624
Georg Brandl116aa622007-08-15 14:28:22 +0000625.. opcode:: STORE_NAME (namei)
626
627 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500628 :attr:`co_names` of the code object. The compiler tries to use
629 :opcode:`STORE_FAST` or :opcode:`STORE_GLOBAL` if possible.
Georg Brandl116aa622007-08-15 14:28:22 +0000630
631
632.. opcode:: DELETE_NAME (namei)
633
634 Implements ``del name``, where *namei* is the index into :attr:`co_names`
635 attribute of the code object.
636
637
638.. opcode:: UNPACK_SEQUENCE (count)
639
640 Unpacks TOS into *count* individual values, which are put onto the stack
641 right-to-left.
642
Georg Brandl116aa622007-08-15 14:28:22 +0000643
Georg Brandl5ac22302008-07-20 21:39:03 +0000644.. opcode:: UNPACK_EX (counts)
645
646 Implements assignment with a starred target: Unpacks an iterable in TOS into
647 individual values, where the total number of values can be smaller than the
648 number of items in the iterable: one the new values will be a list of all
649 leftover items.
650
651 The low byte of *counts* is the number of values before the list value, the
652 high byte of *counts* the number of values after it. The resulting values
653 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000654
Georg Brandl5ac22302008-07-20 21:39:03 +0000655
Georg Brandl116aa622007-08-15 14:28:22 +0000656.. opcode:: STORE_ATTR (namei)
657
658 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
659 :attr:`co_names`.
660
661
662.. opcode:: DELETE_ATTR (namei)
663
664 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
665
666
667.. opcode:: STORE_GLOBAL (namei)
668
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200669 Works as :opcode:`STORE_NAME`, but stores the name as a global.
Georg Brandl116aa622007-08-15 14:28:22 +0000670
671
672.. opcode:: DELETE_GLOBAL (namei)
673
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200674 Works as :opcode:`DELETE_NAME`, but deletes a global name.
Georg Brandl116aa622007-08-15 14:28:22 +0000675
Georg Brandl116aa622007-08-15 14:28:22 +0000676
677.. opcode:: LOAD_CONST (consti)
678
679 Pushes ``co_consts[consti]`` onto the stack.
680
681
682.. opcode:: LOAD_NAME (namei)
683
684 Pushes the value associated with ``co_names[namei]`` onto the stack.
685
686
687.. opcode:: BUILD_TUPLE (count)
688
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500689 Creates a tuple consuming *count* items from the stack, and pushes the
690 resulting tuple onto the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000691
692
693.. opcode:: BUILD_LIST (count)
694
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200695 Works as :opcode:`BUILD_TUPLE`, but creates a list.
Georg Brandl116aa622007-08-15 14:28:22 +0000696
697
698.. opcode:: BUILD_SET (count)
699
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200700 Works as :opcode:`BUILD_TUPLE`, but creates a set.
Georg Brandl116aa622007-08-15 14:28:22 +0000701
702
Christian Heimesa62da1d2008-01-12 19:39:10 +0000703.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000704
Christian Heimesa62da1d2008-01-12 19:39:10 +0000705 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
706 to hold *count* entries.
Georg Brandl116aa622007-08-15 14:28:22 +0000707
708
709.. opcode:: LOAD_ATTR (namei)
710
711 Replaces TOS with ``getattr(TOS, co_names[namei])``.
712
713
714.. opcode:: COMPARE_OP (opname)
715
716 Performs a Boolean operation. The operation name can be found in
717 ``cmp_op[opname]``.
718
719
720.. opcode:: IMPORT_NAME (namei)
721
Christian Heimesa342c012008-04-20 21:01:16 +0000722 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
723 the *fromlist* and *level* arguments of :func:`__import__`. The module
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500724 object is pushed onto the stack. The current namespace is not affected: for
725 a proper import statement, a subsequent :opcode:`STORE_FAST` instruction
Christian Heimesa342c012008-04-20 21:01:16 +0000726 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000727
728
729.. opcode:: IMPORT_FROM (namei)
730
731 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
732 resulting object is pushed onto the stack, to be subsequently stored by a
Serhiy Storchakaf751a9e2014-11-11 10:02:11 +0200733 :opcode:`STORE_FAST` instruction.
Georg Brandl116aa622007-08-15 14:28:22 +0000734
735
736.. opcode:: JUMP_FORWARD (delta)
737
Georg Brandl9afde1c2007-11-01 20:32:30 +0000738 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000739
740
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000741.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000742
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000743 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000744
745
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000746.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000747
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000748 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
749
750
751.. opcode:: JUMP_IF_TRUE_OR_POP (target)
752
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500753 If TOS is true, sets the bytecode counter to *target* and leaves TOS on the
754 stack. Otherwise (TOS is false), TOS is popped.
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000755
756
757.. opcode:: JUMP_IF_FALSE_OR_POP (target)
758
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500759 If TOS is false, sets the bytecode counter to *target* and leaves TOS on the
760 stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000761
762
763.. opcode:: JUMP_ABSOLUTE (target)
764
Georg Brandl9afde1c2007-11-01 20:32:30 +0000765 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000766
767
768.. opcode:: FOR_ITER (delta)
769
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500770 TOS is an :term:`iterator`. Call its :meth:`~iterator.__next__` method. If
771 this yields a new value, push it on the stack (leaving the iterator below
772 it). If the iterator indicates it is exhausted TOS is popped, and the byte
773 code counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000774
Georg Brandl116aa622007-08-15 14:28:22 +0000775
776.. opcode:: LOAD_GLOBAL (namei)
777
778 Loads the global named ``co_names[namei]`` onto the stack.
779
Georg Brandl116aa622007-08-15 14:28:22 +0000780
781.. opcode:: SETUP_LOOP (delta)
782
783 Pushes a block for a loop onto the block stack. The block spans from the
784 current instruction with a size of *delta* bytes.
785
786
787.. opcode:: SETUP_EXCEPT (delta)
788
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500789 Pushes a try block from a try-except clause onto the block stack. *delta*
790 points to the first except block.
Georg Brandl116aa622007-08-15 14:28:22 +0000791
792
793.. opcode:: SETUP_FINALLY (delta)
794
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500795 Pushes a try block from a try-except clause onto the block stack. *delta*
796 points to the finally block.
Georg Brandl116aa622007-08-15 14:28:22 +0000797
Georg Brandl4833e5b2010-07-03 10:41:33 +0000798.. opcode:: STORE_MAP
Christian Heimesa62da1d2008-01-12 19:39:10 +0000799
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500800 Store a key and value pair in a dictionary. Pops the key and value while
801 leaving the dictionary on the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000802
803.. opcode:: LOAD_FAST (var_num)
804
805 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
806
807
808.. opcode:: STORE_FAST (var_num)
809
810 Stores TOS into the local ``co_varnames[var_num]``.
811
812
813.. opcode:: DELETE_FAST (var_num)
814
815 Deletes local ``co_varnames[var_num]``.
816
817
818.. opcode:: LOAD_CLOSURE (i)
819
820 Pushes a reference to the cell contained in slot *i* of the cell and free
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500821 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
822 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
Georg Brandl116aa622007-08-15 14:28:22 +0000823 len(co_cellvars)]``.
824
825
826.. opcode:: LOAD_DEREF (i)
827
828 Loads the cell contained in slot *i* of the cell and free variable storage.
829 Pushes a reference to the object the cell contains on the stack.
830
831
Benjamin Peterson3b0431d2013-04-30 09:41:40 -0400832.. opcode:: LOAD_CLASSDEREF (i)
833
834 Much like :opcode:`LOAD_DEREF` but first checks the locals dictionary before
835 consulting the cell. This is used for loading free variables in class
836 bodies.
837
838
Georg Brandl116aa622007-08-15 14:28:22 +0000839.. opcode:: STORE_DEREF (i)
840
841 Stores TOS into the cell contained in slot *i* of the cell and free variable
842 storage.
843
844
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000845.. opcode:: DELETE_DEREF (i)
846
847 Empties the cell contained in slot *i* of the cell and free variable storage.
848 Used by the :keyword:`del` statement.
849
850
Georg Brandl116aa622007-08-15 14:28:22 +0000851.. opcode:: RAISE_VARARGS (argc)
852
853 Raises an exception. *argc* indicates the number of parameters to the raise
854 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
855 the parameter as TOS1, and the exception as TOS.
856
857
858.. opcode:: CALL_FUNCTION (argc)
859
860 Calls a function. The low byte of *argc* indicates the number of positional
861 parameters, the high byte the number of keyword parameters. On the stack, the
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500862 opcode finds the keyword parameters first. For each keyword argument, the
863 value is on top of the key. Below the keyword parameters, the positional
864 parameters are on the stack, with the right-most parameter on top. Below the
865 parameters, the function object to call is on the stack. Pops all function
866 arguments, and the function itself off the stack, and pushes the return
867 value.
Georg Brandl116aa622007-08-15 14:28:22 +0000868
869
870.. opcode:: MAKE_FUNCTION (argc)
871
Georg Brandlc96ef1f2013-10-12 18:41:18 +0200872 Pushes a new function object on the stack. From bottom to top, the consumed
873 stack must consist of
874
875 * ``argc & 0xFF`` default argument objects in positional order
876 * ``(argc >> 8) & 0xFF`` pairs of name and default argument, with the name
877 just below the object on the stack, for keyword-only parameters
878 * ``(argc >> 16) & 0x7FFF`` parameter annotation objects
879 * a tuple listing the parameter names for the annotations (only if there are
880 ony annotation objects)
881 * the code associated with the function (at TOS1)
882 * the :term:`qualified name` of the function (at TOS)
Georg Brandl116aa622007-08-15 14:28:22 +0000883
884
885.. opcode:: MAKE_CLOSURE (argc)
886
Guido van Rossum04110fb2007-08-24 16:32:05 +0000887 Creates a new function object, sets its *__closure__* slot, and pushes it on
Andrew Svetlova5c43092012-11-23 15:28:34 +0200888 the stack. TOS is the :term:`qualified name` of the function, TOS1 is the
889 code associated with the function, and TOS2 is the tuple containing cells for
Antoine Pitrou4ce4f972015-08-13 20:37:08 +0200890 the closure's free variables. *argc* is interpreted as in ``MAKE_FUNCTION``;
891 the annotations and defaults are also in the same order below TOS2.
Georg Brandl116aa622007-08-15 14:28:22 +0000892
893
894.. opcode:: BUILD_SLICE (argc)
895
896 .. index:: builtin: slice
897
898 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
899 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000900 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000901
902
903.. opcode:: EXTENDED_ARG (ext)
904
905 Prefixes any opcode which has an argument too big to fit into the default two
906 bytes. *ext* holds two additional bytes which, taken together with the
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500907 subsequent opcode's argument, comprise a four-byte argument, *ext* being the
908 two most-significant bytes.
Georg Brandl116aa622007-08-15 14:28:22 +0000909
910
911.. opcode:: CALL_FUNCTION_VAR (argc)
912
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500913 Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
914 top element on the stack contains the variable argument list, followed by
915 keyword and positional arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000916
917
918.. opcode:: CALL_FUNCTION_KW (argc)
919
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500920 Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
921 top element on the stack contains the keyword arguments dictionary, followed
922 by explicit keyword and positional arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000923
924
925.. opcode:: CALL_FUNCTION_VAR_KW (argc)
926
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500927 Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
928 top element on the stack contains the keyword arguments dictionary, followed
929 by the variable-arguments tuple, followed by explicit keyword and positional
930 arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000931
932
Georg Brandl4833e5b2010-07-03 10:41:33 +0000933.. opcode:: HAVE_ARGUMENT
Georg Brandl116aa622007-08-15 14:28:22 +0000934
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500935 This is not really an opcode. It identifies the dividing line between
936 opcodes which don't take arguments ``< HAVE_ARGUMENT`` and those which do
937 ``>= HAVE_ARGUMENT``.
Georg Brandl116aa622007-08-15 14:28:22 +0000938
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000939.. _opcode_collections:
940
941Opcode collections
942------------------
943
944These collections are provided for automatic introspection of bytecode
945instructions:
946
947.. data:: opname
948
949 Sequence of operation names, indexable using the bytecode.
950
951
952.. data:: opmap
953
954 Dictionary mapping operation names to bytecodes.
955
956
957.. data:: cmp_op
958
959 Sequence of all compare operation names.
960
961
962.. data:: hasconst
963
964 Sequence of bytecodes that have a constant parameter.
965
966
967.. data:: hasfree
968
Benjamin Petersonbdf525b2015-03-02 09:31:40 -0500969 Sequence of bytecodes that access a free variable (note that 'free' in this
970 context refers to names in the current scope that are referenced by inner
971 scopes or names in outer scopes that are referenced from this scope. It does
972 *not* include references to global or builtin scopes).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000973
974
975.. data:: hasname
976
977 Sequence of bytecodes that access an attribute by name.
978
979
980.. data:: hasjrel
981
982 Sequence of bytecodes that have a relative jump target.
983
984
985.. data:: hasjabs
986
987 Sequence of bytecodes that have an absolute jump target.
988
989
990.. data:: haslocal
991
992 Sequence of bytecodes that access a local variable.
993
994
995.. data:: hascompare
996
997 Sequence of bytecodes of Boolean operations.