blob: 8293d498a362f171eb575fc61f26e39e54d721d1 [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
12disassembling it. The CPython bytecode which this module takes as an
Georg Brandl71515ca2009-05-17 12:29:12 +000013input is defined in the file :file:`Include/opcode.h` and used by the compiler
14and the interpreter.
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
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100043The bytecode analysis API allows pieces of Python code to be wrapped in a
44:class:`Bytecode` object that provides easy access to details of the
45compiled code.
46
Nick Coghlan50c48b82013-11-23 00:57:00 +100047.. class:: Bytecode(x, *, first_line=None, current_offset=None)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100048
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100049 Analyse the bytecode corresponding to a function, method, string of
50 source code, or a code object (as returned by :func:`compile`).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100051
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100052 This is a convenience wrapper around many of the functions listed below,
53 most notably :func:`get_instructions`, as iterating over a
Nick Coghlan07155c92013-11-06 22:12:07 +100054 :class:`Bytecode` instance yields the bytecode operations as
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100055 :class:`Instruction` instances.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100056
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100057 If *first_line* is not None, it indicates the line number that should
58 be reported for the first source line in the disassembled code.
59 Otherwise, the source line information (if any) is taken directly from
60 the disassembled code object.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100061
Nick Coghlan50c48b82013-11-23 00:57:00 +100062 If *current_offset* is not None, it refers to an instruction offset
63 in the disassembled code. Setting this means :meth:`dis` will display
64 a "current instruction" marker against the specified opcode.
65
66 .. classmethod:: from_traceback(tb)
67
68 Construct a :class:`Bytecode` instance from the given traceback,
69 setting *current_offset* to the instruction responsible for the
70 exception.
71
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
82 Return a formatted view of the bytecode operations (the same as
83 printed by :func:`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
105The :mod:`dis` module also defines the following analysis functions that
106convert the input directly to the desired output. They can be useful if
107only a single operation is being performed, so the intermediate analysis
108object isn't useful:
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Nick Coghlane8814fb2010-09-10 14:08:04 +0000110.. function:: code_info(x)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000111
Georg Brandl67b21b72010-08-17 15:07:14 +0000112 Return a formatted multi-line string with detailed code object information
113 for the supplied function, method, source code string or code object.
Nick Coghlaneae2da12010-08-17 08:03:36 +0000114
Georg Brandl67b21b72010-08-17 15:07:14 +0000115 Note that the exact contents of code info strings are highly implementation
116 dependent and they may change arbitrarily across Python VMs or Python
117 releases.
Nick Coghlaneae2da12010-08-17 08:03:36 +0000118
119 .. versionadded:: 3.2
120
Georg Brandl67b21b72010-08-17 15:07:14 +0000121
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000122.. function:: show_code(x, *, file=None)
Nick Coghlane8814fb2010-09-10 14:08:04 +0000123
124 Print detailed code object information for the supplied function, method,
Ezio Melotti6e6c6ac2013-08-23 22:41:39 +0300125 source code string or code object to *file* (or ``sys.stdout`` if *file*
126 is not specified).
Nick Coghlane8814fb2010-09-10 14:08:04 +0000127
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000128 This is a convenient shorthand for ``print(code_info(x), file=file)``,
129 intended for interactive exploration at the interpreter prompt.
Nick Coghlane8814fb2010-09-10 14:08:04 +0000130
131 .. versionadded:: 3.2
132
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000133 .. versionchanged:: 3.4
134 Added ``file`` parameter
135
136
137.. function:: dis(x=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000138
Georg Brandl67b21b72010-08-17 15:07:14 +0000139 Disassemble the *x* object. *x* can denote either a module, a class, a
140 method, a function, a code object, a string of source code or a byte sequence
141 of raw bytecode. For a module, it disassembles all functions. For a class,
142 it disassembles all methods. For a code object or sequence of raw bytecode,
143 it prints one line per bytecode instruction. Strings are first compiled to
144 code objects with the :func:`compile` built-in function before being
145 disassembled. If no object is provided, this function disassembles the last
146 traceback.
Georg Brandl116aa622007-08-15 14:28:22 +0000147
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000148 The disassembly is written as text to the supplied ``file`` argument if
149 provided and to ``sys.stdout`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000151 .. versionchanged:: 3.4
152 Added ``file`` parameter
153
154
155.. function:: distb(tb=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000156
Georg Brandl4833e5b2010-07-03 10:41:33 +0000157 Disassemble the top-of-stack function of a traceback, using the last
158 traceback if none was passed. The instruction causing the exception is
159 indicated.
Georg Brandl116aa622007-08-15 14:28:22 +0000160
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000161 The disassembly is written as text to the supplied ``file`` argument if
162 provided and to ``sys.stdout`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000164 .. versionchanged:: 3.4
165 Added ``file`` parameter
166
167
168.. function:: disassemble(code, lasti=-1, *, file=None)
169 disco(code, lasti=-1, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000170
Georg Brandl4833e5b2010-07-03 10:41:33 +0000171 Disassemble a code object, indicating the last instruction if *lasti* was
Georg Brandl116aa622007-08-15 14:28:22 +0000172 provided. The output is divided in the following columns:
173
174 #. the line number, for the first instruction of each line
175 #. the current instruction, indicated as ``-->``,
176 #. a labelled instruction, indicated with ``>>``,
177 #. the address of the instruction,
178 #. the operation code name,
179 #. operation parameters, and
180 #. interpretation of the parameters in parentheses.
181
182 The parameter interpretation recognizes local and global variable names,
183 constant values, branch targets, and compare operators.
184
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000185 The disassembly is written as text to the supplied ``file`` argument if
186 provided and to ``sys.stdout`` otherwise.
187
188 .. versionchanged:: 3.4
189 Added ``file`` parameter
190
191
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000192.. function:: get_instructions(x, *, first_line=None)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000193
194 Return an iterator over the instructions in the supplied function, method,
195 source code string or code object.
196
197 The iterator generates a series of :class:`Instruction` named tuples
198 giving the details of each operation in the supplied code.
199
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000200 If *first_line* is not None, it indicates the line number that should
201 be reported for the first source line in the disassembled code.
202 Otherwise, the source line information (if any) is taken directly from
203 the disassembled code object.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000204
205 .. versionadded:: 3.4
206
Georg Brandl116aa622007-08-15 14:28:22 +0000207
Benjamin Peterson75edad02009-01-01 15:05:06 +0000208.. function:: findlinestarts(code)
209
210 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
211 attributes of the code object *code* to find the offsets which are starts of
212 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
213
214
215.. function:: findlabels(code)
216
217 Detect all offsets in the code object *code* which are jump targets, and
218 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +0000219
Larry Hastings3a907972013-11-23 14:49:22 -0800220
221.. function:: stack_effect(opcode, [oparg])
222
223 Compute the stack effect of *opcode* with argument *oparg*.
224
225 .. versionadded:: 3.4
226
Georg Brandl116aa622007-08-15 14:28:22 +0000227.. _bytecodes:
228
Georg Brandl9afde1c2007-11-01 20:32:30 +0000229Python Bytecode Instructions
230----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000231
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000232The :func:`get_instructions` function and :class:`Bytecode` class provide
233details of bytecode instructions as :class:`Instruction` instances:
234
235.. class:: Instruction
236
237 Details for a bytecode operation
238
239 .. data:: opcode
240
241 numeric code for operation, corresponding to the opcode values listed
242 below and the bytecode values in the :ref:`opcode_collections`.
243
244
245 .. data:: opname
246
247 human readable name for operation
248
249
250 .. data:: arg
251
252 numeric argument to operation (if any), otherwise None
253
254
255 .. data:: argval
256
257 resolved arg value (if known), otherwise same as arg
258
259
260 .. data:: argrepr
261
262 human readable description of operation argument
263
264
265 .. data:: offset
266
267 start index of operation within bytecode sequence
268
269
270 .. data:: starts_line
271
272 line started by this opcode (if any), otherwise None
273
274
275 .. data:: is_jump_target
276
Serhiy Storchaka0e90e992013-11-29 12:19:53 +0200277 ``True`` if other code jumps to here, otherwise ``False``
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000278
279 .. versionadded:: 3.4
280
281
Georg Brandl9afde1c2007-11-01 20:32:30 +0000282The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000283
284
Georg Brandl4833e5b2010-07-03 10:41:33 +0000285**General instructions**
286
Georg Brandl4833e5b2010-07-03 10:41:33 +0000287.. opcode:: NOP
Georg Brandl116aa622007-08-15 14:28:22 +0000288
289 Do nothing code. Used as a placeholder by the bytecode optimizer.
290
291
Georg Brandl4833e5b2010-07-03 10:41:33 +0000292.. opcode:: POP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000293
294 Removes the top-of-stack (TOS) item.
295
296
Georg Brandl4833e5b2010-07-03 10:41:33 +0000297.. opcode:: ROT_TWO
Georg Brandl116aa622007-08-15 14:28:22 +0000298
299 Swaps the two top-most stack items.
300
301
Georg Brandl4833e5b2010-07-03 10:41:33 +0000302.. opcode:: ROT_THREE
Georg Brandl116aa622007-08-15 14:28:22 +0000303
304 Lifts second and third stack item one position up, moves top down to position
305 three.
306
307
Georg Brandl4833e5b2010-07-03 10:41:33 +0000308.. opcode:: DUP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000309
310 Duplicates the reference on top of the stack.
311
Georg Brandl4833e5b2010-07-03 10:41:33 +0000312
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000313.. opcode:: DUP_TOP_TWO
314
315 Duplicates the two references on top of the stack, leaving them in the
316 same order.
317
318
Georg Brandl4833e5b2010-07-03 10:41:33 +0000319**Unary operations**
320
321Unary operations take the top of the stack, apply the operation, and push the
Georg Brandl116aa622007-08-15 14:28:22 +0000322result back on the stack.
323
Georg Brandl4833e5b2010-07-03 10:41:33 +0000324.. opcode:: UNARY_POSITIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000325
326 Implements ``TOS = +TOS``.
327
328
Georg Brandl4833e5b2010-07-03 10:41:33 +0000329.. opcode:: UNARY_NEGATIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000330
331 Implements ``TOS = -TOS``.
332
333
Georg Brandl4833e5b2010-07-03 10:41:33 +0000334.. opcode:: UNARY_NOT
Georg Brandl116aa622007-08-15 14:28:22 +0000335
336 Implements ``TOS = not TOS``.
337
338
Georg Brandl4833e5b2010-07-03 10:41:33 +0000339.. opcode:: UNARY_INVERT
Georg Brandl116aa622007-08-15 14:28:22 +0000340
341 Implements ``TOS = ~TOS``.
342
343
Georg Brandl4833e5b2010-07-03 10:41:33 +0000344.. opcode:: GET_ITER
Georg Brandl116aa622007-08-15 14:28:22 +0000345
346 Implements ``TOS = iter(TOS)``.
347
Georg Brandl4833e5b2010-07-03 10:41:33 +0000348
349**Binary operations**
350
Georg Brandl116aa622007-08-15 14:28:22 +0000351Binary operations remove the top of the stack (TOS) and the second top-most
352stack item (TOS1) from the stack. They perform the operation, and put the
353result back on the stack.
354
Georg Brandl4833e5b2010-07-03 10:41:33 +0000355.. opcode:: BINARY_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000356
357 Implements ``TOS = TOS1 ** TOS``.
358
359
Georg Brandl4833e5b2010-07-03 10:41:33 +0000360.. opcode:: BINARY_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000361
362 Implements ``TOS = TOS1 * TOS``.
363
364
Georg Brandl4833e5b2010-07-03 10:41:33 +0000365.. opcode:: BINARY_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000366
367 Implements ``TOS = TOS1 // TOS``.
368
369
Georg Brandl4833e5b2010-07-03 10:41:33 +0000370.. opcode:: BINARY_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000371
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000372 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000373
374
Georg Brandl4833e5b2010-07-03 10:41:33 +0000375.. opcode:: BINARY_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000376
377 Implements ``TOS = TOS1 % TOS``.
378
379
Georg Brandl4833e5b2010-07-03 10:41:33 +0000380.. opcode:: BINARY_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000381
382 Implements ``TOS = TOS1 + TOS``.
383
384
Georg Brandl4833e5b2010-07-03 10:41:33 +0000385.. opcode:: BINARY_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000386
387 Implements ``TOS = TOS1 - TOS``.
388
389
Georg Brandl4833e5b2010-07-03 10:41:33 +0000390.. opcode:: BINARY_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000391
392 Implements ``TOS = TOS1[TOS]``.
393
394
Georg Brandl4833e5b2010-07-03 10:41:33 +0000395.. opcode:: BINARY_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000396
397 Implements ``TOS = TOS1 << TOS``.
398
399
Georg Brandl4833e5b2010-07-03 10:41:33 +0000400.. opcode:: BINARY_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000401
402 Implements ``TOS = TOS1 >> TOS``.
403
404
Georg Brandl4833e5b2010-07-03 10:41:33 +0000405.. opcode:: BINARY_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000406
407 Implements ``TOS = TOS1 & TOS``.
408
409
Georg Brandl4833e5b2010-07-03 10:41:33 +0000410.. opcode:: BINARY_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000411
412 Implements ``TOS = TOS1 ^ TOS``.
413
414
Georg Brandl4833e5b2010-07-03 10:41:33 +0000415.. opcode:: BINARY_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000416
417 Implements ``TOS = TOS1 | TOS``.
418
Georg Brandl4833e5b2010-07-03 10:41:33 +0000419
420**In-place operations**
421
Georg Brandl116aa622007-08-15 14:28:22 +0000422In-place operations are like binary operations, in that they remove TOS and
423TOS1, and push the result back on the stack, but the operation is done in-place
424when TOS1 supports it, and the resulting TOS may be (but does not have to be)
425the original TOS1.
426
Georg Brandl4833e5b2010-07-03 10:41:33 +0000427.. opcode:: INPLACE_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000428
429 Implements in-place ``TOS = TOS1 ** TOS``.
430
431
Georg Brandl4833e5b2010-07-03 10:41:33 +0000432.. opcode:: INPLACE_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000433
434 Implements in-place ``TOS = TOS1 * TOS``.
435
436
Georg Brandl4833e5b2010-07-03 10:41:33 +0000437.. opcode:: INPLACE_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000438
439 Implements in-place ``TOS = TOS1 // TOS``.
440
441
Georg Brandl4833e5b2010-07-03 10:41:33 +0000442.. opcode:: INPLACE_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000443
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000444 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000445
446
Georg Brandl4833e5b2010-07-03 10:41:33 +0000447.. opcode:: INPLACE_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000448
449 Implements in-place ``TOS = TOS1 % TOS``.
450
451
Georg Brandl4833e5b2010-07-03 10:41:33 +0000452.. opcode:: INPLACE_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000453
454 Implements in-place ``TOS = TOS1 + TOS``.
455
456
Georg Brandl4833e5b2010-07-03 10:41:33 +0000457.. opcode:: INPLACE_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000458
459 Implements in-place ``TOS = TOS1 - TOS``.
460
461
Georg Brandl4833e5b2010-07-03 10:41:33 +0000462.. opcode:: INPLACE_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000463
464 Implements in-place ``TOS = TOS1 << TOS``.
465
466
Georg Brandl4833e5b2010-07-03 10:41:33 +0000467.. opcode:: INPLACE_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000468
469 Implements in-place ``TOS = TOS1 >> TOS``.
470
471
Georg Brandl4833e5b2010-07-03 10:41:33 +0000472.. opcode:: INPLACE_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000473
474 Implements in-place ``TOS = TOS1 & TOS``.
475
476
Georg Brandl4833e5b2010-07-03 10:41:33 +0000477.. opcode:: INPLACE_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000478
479 Implements in-place ``TOS = TOS1 ^ TOS``.
480
481
Georg Brandl4833e5b2010-07-03 10:41:33 +0000482.. opcode:: INPLACE_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000483
484 Implements in-place ``TOS = TOS1 | TOS``.
485
Georg Brandl116aa622007-08-15 14:28:22 +0000486
Georg Brandl4833e5b2010-07-03 10:41:33 +0000487.. opcode:: STORE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000488
489 Implements ``TOS1[TOS] = TOS2``.
490
491
Georg Brandl4833e5b2010-07-03 10:41:33 +0000492.. opcode:: DELETE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000493
494 Implements ``del TOS1[TOS]``.
495
Georg Brandl116aa622007-08-15 14:28:22 +0000496
Georg Brandl4833e5b2010-07-03 10:41:33 +0000497**Miscellaneous opcodes**
Georg Brandl116aa622007-08-15 14:28:22 +0000498
Georg Brandl4833e5b2010-07-03 10:41:33 +0000499.. opcode:: PRINT_EXPR
Georg Brandl116aa622007-08-15 14:28:22 +0000500
501 Implements the expression statement for the interactive mode. TOS is removed
502 from the stack and printed. In non-interactive mode, an expression statement is
503 terminated with ``POP_STACK``.
504
505
Georg Brandl4833e5b2010-07-03 10:41:33 +0000506.. opcode:: BREAK_LOOP
Georg Brandl116aa622007-08-15 14:28:22 +0000507
508 Terminates a loop due to a :keyword:`break` statement.
509
510
511.. opcode:: CONTINUE_LOOP (target)
512
513 Continues a loop due to a :keyword:`continue` statement. *target* is the
514 address to jump to (which should be a ``FOR_ITER`` instruction).
515
516
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000517.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000518
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000519 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000520
521
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000522.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000523
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000524 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
525
526
527.. opcode:: MAP_ADD (i)
528
529 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
530 comprehensions.
531
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000532For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the
533added value or key/value pair is popped off, the container object remains on
534the stack so that it is available for further iterations 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
Georg Brandl9afde1c2007-11-01 20:32:30 +0000544 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
549 Pops ``TOS`` and delegates to it as a subiterator from a :term:`generator`.
550
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
556 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
557 local namespace. The module is popped after loading all names. This opcode
558 implements ``from module import *``.
559
560
Georg Brandl4833e5b2010-07-03 10:41:33 +0000561.. opcode:: POP_BLOCK
Georg Brandl116aa622007-08-15 14:28:22 +0000562
563 Removes one block from the block stack. Per frame, there is a stack of blocks,
564 denoting nested loops, try statements, and such.
565
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
570 handler block, as implicitly created when entering an except handler.
571 In addition to popping extraneous values from the frame stack, the
572 last three popped values are used to restore the exception state.
573
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
Benjamin Petersonaac8fd32008-07-20 22:02:26 +0000585 by ``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 Petersoneec3d712008-06-11 15:59:43 +0000602 Cleans up the stack when a :keyword:`with` statement block exits. TOS is
603 the context manager's :meth:`__exit__` bound method. Below TOS are 1--3
604 values 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 Petersoneec3d712008-06-11 15:59:43 +0000614 If the stack represents an exception, *and* the function call returns
615 a 'true' value, this information is "zapped" and replaced with a single
616 ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception.
617 (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
Christian Heimes8640e742008-02-23 16:23:06 +0000628 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl116aa622007-08-15 14:28:22 +0000629 or ``STORE_GLOBAL`` if possible.
630
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
669 Works as ``STORE_NAME``, but stores the name as a global.
670
671
672.. opcode:: DELETE_GLOBAL (namei)
673
674 Works as ``DELETE_NAME``, but deletes a global name.
675
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
689 Creates a tuple consuming *count* items from the stack, and pushes the resulting
690 tuple onto the stack.
691
692
693.. opcode:: BUILD_LIST (count)
694
695 Works as ``BUILD_TUPLE``, but creates a list.
696
697
698.. opcode:: BUILD_SET (count)
699
700 Works as ``BUILD_TUPLE``, but creates a set.
701
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
724 object is pushed onto the stack. The current namespace is not affected:
725 for a proper import statement, a subsequent ``STORE_FAST`` instruction
726 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
733 ``STORE_FAST`` instruction.
734
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
753 If TOS is true, sets the bytecode counter to *target* and leaves TOS
754 on the stack. Otherwise (TOS is false), TOS is popped.
755
756
757.. opcode:: JUMP_IF_FALSE_OR_POP (target)
758
759 If TOS is false, sets the bytecode counter to *target* and leaves
760 TOS on the 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
Ezio Melotti7fa82222012-10-12 13:42:08 +0300770 ``TOS`` is an :term:`iterator`. Call its :meth:`~iterator.__next__` method.
771 If 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
773 byte 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
789 Pushes a try block from a try-except clause onto the block stack. *delta* points
790 to the first except block.
791
792
793.. opcode:: SETUP_FINALLY (delta)
794
795 Pushes a try block from a try-except clause onto the block stack. *delta* points
796 to the finally block.
797
Georg Brandl4833e5b2010-07-03 10:41:33 +0000798.. opcode:: STORE_MAP
Christian Heimesa62da1d2008-01-12 19:39:10 +0000799
800 Store a key and value pair in a dictionary. Pops the key and value while leaving
801 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
821 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 -
823 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
862 opcode finds the keyword parameters first. For each keyword argument, the value
863 is on top of the key. Below the keyword parameters, the positional parameters
864 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl48310cd2009-01-03 21:18:54 +0000865 the function object to call is on the stack. Pops all function arguments, and
Benjamin Peterson206e3072008-10-19 14:07:49 +0000866 the function itself off the stack, and pushes the return value.
Georg Brandl116aa622007-08-15 14:28:22 +0000867
868
869.. opcode:: MAKE_FUNCTION (argc)
870
Georg Brandlc96ef1f2013-10-12 18:41:18 +0200871 Pushes a new function object on the stack. From bottom to top, the consumed
872 stack must consist of
873
874 * ``argc & 0xFF`` default argument objects in positional order
875 * ``(argc >> 8) & 0xFF`` pairs of name and default argument, with the name
876 just below the object on the stack, for keyword-only parameters
877 * ``(argc >> 16) & 0x7FFF`` parameter annotation objects
878 * a tuple listing the parameter names for the annotations (only if there are
879 ony annotation objects)
880 * the code associated with the function (at TOS1)
881 * the :term:`qualified name` of the function (at TOS)
Georg Brandl116aa622007-08-15 14:28:22 +0000882
883
884.. opcode:: MAKE_CLOSURE (argc)
885
Guido van Rossum04110fb2007-08-24 16:32:05 +0000886 Creates a new function object, sets its *__closure__* slot, and pushes it on
Andrew Svetlova5c43092012-11-23 15:28:34 +0200887 the stack. TOS is the :term:`qualified name` of the function, TOS1 is the
888 code associated with the function, and TOS2 is the tuple containing cells for
889 the closure's free variables. The function also has *argc* default parameters,
890 which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000891
892
893.. opcode:: BUILD_SLICE (argc)
894
895 .. index:: builtin: slice
896
897 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
898 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000899 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000900
901
902.. opcode:: EXTENDED_ARG (ext)
903
904 Prefixes any opcode which has an argument too big to fit into the default two
905 bytes. *ext* holds two additional bytes which, taken together with the
906 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
907 most-significant bytes.
908
909
910.. opcode:: CALL_FUNCTION_VAR (argc)
911
912 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
913 on the stack contains the variable argument list, followed by keyword and
914 positional arguments.
915
916
917.. opcode:: CALL_FUNCTION_KW (argc)
918
919 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
920 on the stack contains the keyword arguments dictionary, followed by explicit
921 keyword and positional arguments.
922
923
924.. opcode:: CALL_FUNCTION_VAR_KW (argc)
925
926 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
927 element on the stack contains the keyword arguments dictionary, followed by the
928 variable-arguments tuple, followed by explicit keyword and positional arguments.
929
930
Georg Brandl4833e5b2010-07-03 10:41:33 +0000931.. opcode:: HAVE_ARGUMENT
Georg Brandl116aa622007-08-15 14:28:22 +0000932
933 This is not really an opcode. It identifies the dividing line between opcodes
934 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
935 HAVE_ARGUMENT``.
936
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000937.. _opcode_collections:
938
939Opcode collections
940------------------
941
942These collections are provided for automatic introspection of bytecode
943instructions:
944
945.. data:: opname
946
947 Sequence of operation names, indexable using the bytecode.
948
949
950.. data:: opmap
951
952 Dictionary mapping operation names to bytecodes.
953
954
955.. data:: cmp_op
956
957 Sequence of all compare operation names.
958
959
960.. data:: hasconst
961
962 Sequence of bytecodes that have a constant parameter.
963
964
965.. data:: hasfree
966
967 Sequence of bytecodes that access a free variable (note that 'free' in
968 this context refers to names in the current scope that are referenced by
969 inner scopes or names in outer scopes that are referenced from this scope.
970 It does *not* include references to global or builtin scopes).
971
972
973.. data:: hasname
974
975 Sequence of bytecodes that access an attribute by name.
976
977
978.. data:: hasjrel
979
980 Sequence of bytecodes that have a relative jump target.
981
982
983.. data:: hasjabs
984
985 Sequence of bytecodes that have an absolute jump target.
986
987
988.. data:: haslocal
989
990 Sequence of bytecodes that access a local variable.
991
992
993.. data:: hascompare
994
995 Sequence of bytecodes of Boolean operations.