blob: d86550f182cbcf4d6db411c6b8e218b6d46787ce [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
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
46:class:`Bytecode` object that provides easy access to details of the
47compiled code.
48
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
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100051 Analyse the bytecode corresponding to a function, method, string of
52 source code, or a code object (as returned by :func:`compile`).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100053
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100054 This is a convenience wrapper around many of the functions listed below,
55 most notably :func:`get_instructions`, as iterating over a
Nick Coghlan07155c92013-11-06 22:12:07 +100056 :class:`Bytecode` instance yields the bytecode operations as
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100057 :class:`Instruction` instances.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100058
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100059 If *first_line* is not None, it indicates the line number that should
60 be reported for the first source line in the disassembled code.
61 Otherwise, the source line information (if any) is taken directly from
62 the disassembled code object.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100063
Nick Coghlan50c48b82013-11-23 00:57:00 +100064 If *current_offset* is not None, it refers to an instruction offset
65 in the disassembled code. Setting this means :meth:`dis` will display
66 a "current instruction" marker against the specified opcode.
67
68 .. classmethod:: from_traceback(tb)
69
70 Construct a :class:`Bytecode` instance from the given traceback,
71 setting *current_offset* to the instruction responsible for the
72 exception.
73
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100074 .. data:: codeobj
75
76 The compiled code object.
77
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100078 .. data:: first_line
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100079
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100080 The first source line of the code object (if available)
81
82 .. method:: dis()
83
84 Return a formatted view of the bytecode operations (the same as
85 printed by :func:`dis`, but returned as a multi-line string).
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100086
87 .. method:: info()
88
89 Return a formatted multi-line string with detailed information about the
90 code object, like :func:`code_info`.
91
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100092Example::
93
94 >>> bytecode = dis.Bytecode(myfunc)
95 >>> for instr in bytecode:
96 ... print(instr.opname)
97 ...
98 LOAD_GLOBAL
99 LOAD_FAST
100 CALL_FUNCTION
101 RETURN_VALUE
102
103
104Analysis functions
105------------------
106
107The :mod:`dis` module also defines the following analysis functions that
108convert the input directly to the desired output. They can be useful if
109only a single operation is being performed, so the intermediate analysis
110object isn't useful:
Georg Brandl116aa622007-08-15 14:28:22 +0000111
Nick Coghlane8814fb2010-09-10 14:08:04 +0000112.. function:: code_info(x)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000113
Georg Brandl67b21b72010-08-17 15:07:14 +0000114 Return a formatted multi-line string with detailed code object information
115 for the supplied function, method, source code string or code object.
Nick Coghlaneae2da12010-08-17 08:03:36 +0000116
Georg Brandl67b21b72010-08-17 15:07:14 +0000117 Note that the exact contents of code info strings are highly implementation
118 dependent and they may change arbitrarily across Python VMs or Python
119 releases.
Nick Coghlaneae2da12010-08-17 08:03:36 +0000120
121 .. versionadded:: 3.2
122
Georg Brandl67b21b72010-08-17 15:07:14 +0000123
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000124.. function:: show_code(x, *, file=None)
Nick Coghlane8814fb2010-09-10 14:08:04 +0000125
126 Print detailed code object information for the supplied function, method,
Ezio Melotti6e6c6ac2013-08-23 22:41:39 +0300127 source code string or code object to *file* (or ``sys.stdout`` if *file*
128 is not specified).
Nick Coghlane8814fb2010-09-10 14:08:04 +0000129
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000130 This is a convenient shorthand for ``print(code_info(x), file=file)``,
131 intended for interactive exploration at the interpreter prompt.
Nick Coghlane8814fb2010-09-10 14:08:04 +0000132
133 .. versionadded:: 3.2
134
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000135 .. versionchanged:: 3.4
136 Added ``file`` parameter
137
138
139.. function:: dis(x=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000140
Georg Brandl67b21b72010-08-17 15:07:14 +0000141 Disassemble the *x* object. *x* can denote either a module, a class, a
142 method, a function, a code object, a string of source code or a byte sequence
143 of raw bytecode. For a module, it disassembles all functions. For a class,
144 it disassembles all methods. For a code object or sequence of raw bytecode,
145 it prints one line per bytecode instruction. Strings are first compiled to
146 code objects with the :func:`compile` built-in function before being
147 disassembled. If no object is provided, this function disassembles the last
148 traceback.
Georg Brandl116aa622007-08-15 14:28:22 +0000149
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000150 The disassembly is written as text to the supplied ``file`` argument if
151 provided and to ``sys.stdout`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000152
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000153 .. versionchanged:: 3.4
154 Added ``file`` parameter
155
156
157.. function:: distb(tb=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000158
Georg Brandl4833e5b2010-07-03 10:41:33 +0000159 Disassemble the top-of-stack function of a traceback, using the last
160 traceback if none was passed. The instruction causing the exception is
161 indicated.
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000163 The disassembly is written as text to the supplied ``file`` argument if
164 provided and to ``sys.stdout`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000165
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000166 .. versionchanged:: 3.4
167 Added ``file`` parameter
168
169
170.. function:: disassemble(code, lasti=-1, *, file=None)
171 disco(code, lasti=-1, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000172
Georg Brandl4833e5b2010-07-03 10:41:33 +0000173 Disassemble a code object, indicating the last instruction if *lasti* was
Georg Brandl116aa622007-08-15 14:28:22 +0000174 provided. The output is divided in the following columns:
175
176 #. the line number, for the first instruction of each line
177 #. the current instruction, indicated as ``-->``,
178 #. a labelled instruction, indicated with ``>>``,
179 #. the address of the instruction,
180 #. the operation code name,
181 #. operation parameters, and
182 #. interpretation of the parameters in parentheses.
183
184 The parameter interpretation recognizes local and global variable names,
185 constant values, branch targets, and compare operators.
186
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000187 The disassembly is written as text to the supplied ``file`` argument if
188 provided and to ``sys.stdout`` otherwise.
189
190 .. versionchanged:: 3.4
191 Added ``file`` parameter
192
193
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000194.. function:: get_instructions(x, *, first_line=None)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000195
196 Return an iterator over the instructions in the supplied function, method,
197 source code string or code object.
198
199 The iterator generates a series of :class:`Instruction` named tuples
200 giving the details of each operation in the supplied code.
201
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000202 If *first_line* is not None, it indicates the line number that should
203 be reported for the first source line in the disassembled code.
204 Otherwise, the source line information (if any) is taken directly from
205 the disassembled code object.
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000206
207 .. versionadded:: 3.4
208
Georg Brandl116aa622007-08-15 14:28:22 +0000209
Benjamin Peterson75edad02009-01-01 15:05:06 +0000210.. function:: findlinestarts(code)
211
212 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
213 attributes of the code object *code* to find the offsets which are starts of
214 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
215
216
217.. function:: findlabels(code)
218
219 Detect all offsets in the code object *code* which are jump targets, and
220 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +0000221
Larry Hastings3a907972013-11-23 14:49:22 -0800222
223.. function:: stack_effect(opcode, [oparg])
224
225 Compute the stack effect of *opcode* with argument *oparg*.
226
227 .. versionadded:: 3.4
228
Georg Brandl116aa622007-08-15 14:28:22 +0000229.. _bytecodes:
230
Georg Brandl9afde1c2007-11-01 20:32:30 +0000231Python Bytecode Instructions
232----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000233
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000234The :func:`get_instructions` function and :class:`Bytecode` class provide
235details of bytecode instructions as :class:`Instruction` instances:
236
237.. class:: Instruction
238
239 Details for a bytecode operation
240
241 .. data:: opcode
242
243 numeric code for operation, corresponding to the opcode values listed
244 below and the bytecode values in the :ref:`opcode_collections`.
245
246
247 .. data:: opname
248
249 human readable name for operation
250
251
252 .. data:: arg
253
254 numeric argument to operation (if any), otherwise None
255
256
257 .. data:: argval
258
259 resolved arg value (if known), otherwise same as arg
260
261
262 .. data:: argrepr
263
264 human readable description of operation argument
265
266
267 .. data:: offset
268
269 start index of operation within bytecode sequence
270
271
272 .. data:: starts_line
273
274 line started by this opcode (if any), otherwise None
275
276
277 .. data:: is_jump_target
278
Serhiy Storchaka0e90e992013-11-29 12:19:53 +0200279 ``True`` if other code jumps to here, otherwise ``False``
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000280
281 .. versionadded:: 3.4
282
283
Georg Brandl9afde1c2007-11-01 20:32:30 +0000284The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000285
286
Georg Brandl4833e5b2010-07-03 10:41:33 +0000287**General instructions**
288
Georg Brandl4833e5b2010-07-03 10:41:33 +0000289.. opcode:: NOP
Georg Brandl116aa622007-08-15 14:28:22 +0000290
291 Do nothing code. Used as a placeholder by the bytecode optimizer.
292
293
Georg Brandl4833e5b2010-07-03 10:41:33 +0000294.. opcode:: POP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000295
296 Removes the top-of-stack (TOS) item.
297
298
Georg Brandl4833e5b2010-07-03 10:41:33 +0000299.. opcode:: ROT_TWO
Georg Brandl116aa622007-08-15 14:28:22 +0000300
301 Swaps the two top-most stack items.
302
303
Georg Brandl4833e5b2010-07-03 10:41:33 +0000304.. opcode:: ROT_THREE
Georg Brandl116aa622007-08-15 14:28:22 +0000305
306 Lifts second and third stack item one position up, moves top down to position
307 three.
308
309
Georg Brandl4833e5b2010-07-03 10:41:33 +0000310.. opcode:: DUP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000311
312 Duplicates the reference on top of the stack.
313
Georg Brandl4833e5b2010-07-03 10:41:33 +0000314
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000315.. opcode:: DUP_TOP_TWO
316
317 Duplicates the two references on top of the stack, leaving them in the
318 same order.
319
320
Georg Brandl4833e5b2010-07-03 10:41:33 +0000321**Unary operations**
322
323Unary operations take the top of the stack, apply the operation, and push the
Georg Brandl116aa622007-08-15 14:28:22 +0000324result back on the stack.
325
Georg Brandl4833e5b2010-07-03 10:41:33 +0000326.. opcode:: UNARY_POSITIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000327
328 Implements ``TOS = +TOS``.
329
330
Georg Brandl4833e5b2010-07-03 10:41:33 +0000331.. opcode:: UNARY_NEGATIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000332
333 Implements ``TOS = -TOS``.
334
335
Georg Brandl4833e5b2010-07-03 10:41:33 +0000336.. opcode:: UNARY_NOT
Georg Brandl116aa622007-08-15 14:28:22 +0000337
338 Implements ``TOS = not TOS``.
339
340
Georg Brandl4833e5b2010-07-03 10:41:33 +0000341.. opcode:: UNARY_INVERT
Georg Brandl116aa622007-08-15 14:28:22 +0000342
343 Implements ``TOS = ~TOS``.
344
345
Georg Brandl4833e5b2010-07-03 10:41:33 +0000346.. opcode:: GET_ITER
Georg Brandl116aa622007-08-15 14:28:22 +0000347
348 Implements ``TOS = iter(TOS)``.
349
Georg Brandl4833e5b2010-07-03 10:41:33 +0000350
351**Binary operations**
352
Georg Brandl116aa622007-08-15 14:28:22 +0000353Binary operations remove the top of the stack (TOS) and the second top-most
354stack item (TOS1) from the stack. They perform the operation, and put the
355result back on the stack.
356
Georg Brandl4833e5b2010-07-03 10:41:33 +0000357.. opcode:: BINARY_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000358
359 Implements ``TOS = TOS1 ** TOS``.
360
361
Georg Brandl4833e5b2010-07-03 10:41:33 +0000362.. opcode:: BINARY_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000363
364 Implements ``TOS = TOS1 * TOS``.
365
366
Georg Brandl4833e5b2010-07-03 10:41:33 +0000367.. opcode:: BINARY_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000368
369 Implements ``TOS = TOS1 // TOS``.
370
371
Georg Brandl4833e5b2010-07-03 10:41:33 +0000372.. opcode:: BINARY_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000373
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000374 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000375
376
Georg Brandl4833e5b2010-07-03 10:41:33 +0000377.. opcode:: BINARY_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000378
379 Implements ``TOS = TOS1 % TOS``.
380
381
Georg Brandl4833e5b2010-07-03 10:41:33 +0000382.. opcode:: BINARY_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000383
384 Implements ``TOS = TOS1 + TOS``.
385
386
Georg Brandl4833e5b2010-07-03 10:41:33 +0000387.. opcode:: BINARY_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000388
389 Implements ``TOS = TOS1 - TOS``.
390
391
Georg Brandl4833e5b2010-07-03 10:41:33 +0000392.. opcode:: BINARY_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000393
394 Implements ``TOS = TOS1[TOS]``.
395
396
Georg Brandl4833e5b2010-07-03 10:41:33 +0000397.. opcode:: BINARY_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000398
399 Implements ``TOS = TOS1 << TOS``.
400
401
Georg Brandl4833e5b2010-07-03 10:41:33 +0000402.. opcode:: BINARY_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000403
404 Implements ``TOS = TOS1 >> TOS``.
405
406
Georg Brandl4833e5b2010-07-03 10:41:33 +0000407.. opcode:: BINARY_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000408
409 Implements ``TOS = TOS1 & TOS``.
410
411
Georg Brandl4833e5b2010-07-03 10:41:33 +0000412.. opcode:: BINARY_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000413
414 Implements ``TOS = TOS1 ^ TOS``.
415
416
Georg Brandl4833e5b2010-07-03 10:41:33 +0000417.. opcode:: BINARY_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000418
419 Implements ``TOS = TOS1 | TOS``.
420
Georg Brandl4833e5b2010-07-03 10:41:33 +0000421
422**In-place operations**
423
Georg Brandl116aa622007-08-15 14:28:22 +0000424In-place operations are like binary operations, in that they remove TOS and
425TOS1, and push the result back on the stack, but the operation is done in-place
426when TOS1 supports it, and the resulting TOS may be (but does not have to be)
427the original TOS1.
428
Georg Brandl4833e5b2010-07-03 10:41:33 +0000429.. opcode:: INPLACE_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000430
431 Implements in-place ``TOS = TOS1 ** TOS``.
432
433
Georg Brandl4833e5b2010-07-03 10:41:33 +0000434.. opcode:: INPLACE_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000435
436 Implements in-place ``TOS = TOS1 * TOS``.
437
438
Georg Brandl4833e5b2010-07-03 10:41:33 +0000439.. opcode:: INPLACE_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000440
441 Implements in-place ``TOS = TOS1 // TOS``.
442
443
Georg Brandl4833e5b2010-07-03 10:41:33 +0000444.. opcode:: INPLACE_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000445
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000446 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000447
448
Georg Brandl4833e5b2010-07-03 10:41:33 +0000449.. opcode:: INPLACE_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000450
451 Implements in-place ``TOS = TOS1 % TOS``.
452
453
Georg Brandl4833e5b2010-07-03 10:41:33 +0000454.. opcode:: INPLACE_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000455
456 Implements in-place ``TOS = TOS1 + TOS``.
457
458
Georg Brandl4833e5b2010-07-03 10:41:33 +0000459.. opcode:: INPLACE_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000460
461 Implements in-place ``TOS = TOS1 - TOS``.
462
463
Georg Brandl4833e5b2010-07-03 10:41:33 +0000464.. opcode:: INPLACE_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000465
466 Implements in-place ``TOS = TOS1 << TOS``.
467
468
Georg Brandl4833e5b2010-07-03 10:41:33 +0000469.. opcode:: INPLACE_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000470
471 Implements in-place ``TOS = TOS1 >> TOS``.
472
473
Georg Brandl4833e5b2010-07-03 10:41:33 +0000474.. opcode:: INPLACE_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000475
476 Implements in-place ``TOS = TOS1 & TOS``.
477
478
Georg Brandl4833e5b2010-07-03 10:41:33 +0000479.. opcode:: INPLACE_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000480
481 Implements in-place ``TOS = TOS1 ^ TOS``.
482
483
Georg Brandl4833e5b2010-07-03 10:41:33 +0000484.. opcode:: INPLACE_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000485
486 Implements in-place ``TOS = TOS1 | TOS``.
487
Georg Brandl116aa622007-08-15 14:28:22 +0000488
Georg Brandl4833e5b2010-07-03 10:41:33 +0000489.. opcode:: STORE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000490
491 Implements ``TOS1[TOS] = TOS2``.
492
493
Georg Brandl4833e5b2010-07-03 10:41:33 +0000494.. opcode:: DELETE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000495
496 Implements ``del TOS1[TOS]``.
497
Georg Brandl116aa622007-08-15 14:28:22 +0000498
Georg Brandl4833e5b2010-07-03 10:41:33 +0000499**Miscellaneous opcodes**
Georg Brandl116aa622007-08-15 14:28:22 +0000500
Georg Brandl4833e5b2010-07-03 10:41:33 +0000501.. opcode:: PRINT_EXPR
Georg Brandl116aa622007-08-15 14:28:22 +0000502
503 Implements the expression statement for the interactive mode. TOS is removed
504 from the stack and printed. In non-interactive mode, an expression statement is
505 terminated with ``POP_STACK``.
506
507
Georg Brandl4833e5b2010-07-03 10:41:33 +0000508.. opcode:: BREAK_LOOP
Georg Brandl116aa622007-08-15 14:28:22 +0000509
510 Terminates a loop due to a :keyword:`break` statement.
511
512
513.. opcode:: CONTINUE_LOOP (target)
514
515 Continues a loop due to a :keyword:`continue` statement. *target* is the
516 address to jump to (which should be a ``FOR_ITER`` instruction).
517
518
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000519.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000520
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000521 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000522
523
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000524.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000525
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000526 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
527
528
529.. opcode:: MAP_ADD (i)
530
531 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
532 comprehensions.
533
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000534For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the
535added value or key/value pair is popped off, the container object remains on
536the stack so that it is available for further iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000537
538
Georg Brandl4833e5b2010-07-03 10:41:33 +0000539.. opcode:: RETURN_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000540
541 Returns with TOS to the caller of the function.
542
543
Georg Brandl4833e5b2010-07-03 10:41:33 +0000544.. opcode:: YIELD_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000545
Georg Brandl9afde1c2007-11-01 20:32:30 +0000546 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000547
548
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000549.. opcode:: YIELD_FROM
550
551 Pops ``TOS`` and delegates to it as a subiterator from a :term:`generator`.
552
553 .. versionadded:: 3.3
554
555
Georg Brandl4833e5b2010-07-03 10:41:33 +0000556.. opcode:: IMPORT_STAR
Georg Brandl116aa622007-08-15 14:28:22 +0000557
558 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
559 local namespace. The module is popped after loading all names. This opcode
560 implements ``from module import *``.
561
562
Georg Brandl4833e5b2010-07-03 10:41:33 +0000563.. opcode:: POP_BLOCK
Georg Brandl116aa622007-08-15 14:28:22 +0000564
565 Removes one block from the block stack. Per frame, there is a stack of blocks,
566 denoting nested loops, try statements, and such.
567
568
Georg Brandl4833e5b2010-07-03 10:41:33 +0000569.. opcode:: POP_EXCEPT
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000570
571 Removes one block from the block stack. The popped block must be an exception
572 handler block, as implicitly created when entering an except handler.
573 In addition to popping extraneous values from the frame stack, the
574 last three popped values are used to restore the exception state.
575
576
Georg Brandl4833e5b2010-07-03 10:41:33 +0000577.. opcode:: END_FINALLY
Georg Brandl116aa622007-08-15 14:28:22 +0000578
579 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
580 exception has to be re-raised, or whether the function returns, and continues
581 with the outer-next block.
582
583
Georg Brandl4833e5b2010-07-03 10:41:33 +0000584.. opcode:: LOAD_BUILD_CLASS
Georg Brandl116aa622007-08-15 14:28:22 +0000585
Georg Brandl5ac22302008-07-20 21:39:03 +0000586 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Benjamin Petersonaac8fd32008-07-20 22:02:26 +0000587 by ``CALL_FUNCTION`` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000588
Guido van Rossum04110fb2007-08-24 16:32:05 +0000589
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000590.. opcode:: SETUP_WITH (delta)
591
592 This opcode performs several operations before a with block starts. First,
593 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
594 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
595 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
596 is pushed. Finally, the result of calling the enter method is pushed onto
597 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
598 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
599 :opcode:`UNPACK_SEQUENCE`).
600
601
Georg Brandl4833e5b2010-07-03 10:41:33 +0000602.. opcode:: WITH_CLEANUP
Guido van Rossum04110fb2007-08-24 16:32:05 +0000603
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000604 Cleans up the stack when a :keyword:`with` statement block exits. TOS is
605 the context manager's :meth:`__exit__` bound method. Below TOS are 1--3
606 values indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000607
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000608 * SECOND = ``None``
609 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
610 * SECOND = ``WHY_*``; no retval below it
611 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000612
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000613 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
614 ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000615
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000616 If the stack represents an exception, *and* the function call returns
617 a 'true' value, this information is "zapped" and replaced with a single
618 ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception.
619 (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000620
Georg Brandl9afde1c2007-11-01 20:32:30 +0000621 .. XXX explain the WHY stuff!
622
Guido van Rossum04110fb2007-08-24 16:32:05 +0000623
Georg Brandl116aa622007-08-15 14:28:22 +0000624All of the following opcodes expect arguments. An argument is two bytes, with
625the more significant byte last.
626
Georg Brandl116aa622007-08-15 14:28:22 +0000627.. opcode:: STORE_NAME (namei)
628
629 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Christian Heimes8640e742008-02-23 16:23:06 +0000630 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl116aa622007-08-15 14:28:22 +0000631 or ``STORE_GLOBAL`` if possible.
632
633
634.. opcode:: DELETE_NAME (namei)
635
636 Implements ``del name``, where *namei* is the index into :attr:`co_names`
637 attribute of the code object.
638
639
640.. opcode:: UNPACK_SEQUENCE (count)
641
642 Unpacks TOS into *count* individual values, which are put onto the stack
643 right-to-left.
644
Georg Brandl116aa622007-08-15 14:28:22 +0000645
Georg Brandl5ac22302008-07-20 21:39:03 +0000646.. opcode:: UNPACK_EX (counts)
647
648 Implements assignment with a starred target: Unpacks an iterable in TOS into
649 individual values, where the total number of values can be smaller than the
650 number of items in the iterable: one the new values will be a list of all
651 leftover items.
652
653 The low byte of *counts* is the number of values before the list value, the
654 high byte of *counts* the number of values after it. The resulting values
655 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000656
Georg Brandl5ac22302008-07-20 21:39:03 +0000657
Georg Brandl116aa622007-08-15 14:28:22 +0000658.. opcode:: STORE_ATTR (namei)
659
660 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
661 :attr:`co_names`.
662
663
664.. opcode:: DELETE_ATTR (namei)
665
666 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
667
668
669.. opcode:: STORE_GLOBAL (namei)
670
671 Works as ``STORE_NAME``, but stores the name as a global.
672
673
674.. opcode:: DELETE_GLOBAL (namei)
675
676 Works as ``DELETE_NAME``, but deletes a global name.
677
Georg Brandl116aa622007-08-15 14:28:22 +0000678
679.. opcode:: LOAD_CONST (consti)
680
681 Pushes ``co_consts[consti]`` onto the stack.
682
683
684.. opcode:: LOAD_NAME (namei)
685
686 Pushes the value associated with ``co_names[namei]`` onto the stack.
687
688
689.. opcode:: BUILD_TUPLE (count)
690
691 Creates a tuple consuming *count* items from the stack, and pushes the resulting
692 tuple onto the stack.
693
694
695.. opcode:: BUILD_LIST (count)
696
697 Works as ``BUILD_TUPLE``, but creates a list.
698
699
700.. opcode:: BUILD_SET (count)
701
702 Works as ``BUILD_TUPLE``, but creates a set.
703
704
Christian Heimesa62da1d2008-01-12 19:39:10 +0000705.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000706
Christian Heimesa62da1d2008-01-12 19:39:10 +0000707 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
708 to hold *count* entries.
Georg Brandl116aa622007-08-15 14:28:22 +0000709
710
711.. opcode:: LOAD_ATTR (namei)
712
713 Replaces TOS with ``getattr(TOS, co_names[namei])``.
714
715
716.. opcode:: COMPARE_OP (opname)
717
718 Performs a Boolean operation. The operation name can be found in
719 ``cmp_op[opname]``.
720
721
722.. opcode:: IMPORT_NAME (namei)
723
Christian Heimesa342c012008-04-20 21:01:16 +0000724 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
725 the *fromlist* and *level* arguments of :func:`__import__`. The module
726 object is pushed onto the stack. The current namespace is not affected:
727 for a proper import statement, a subsequent ``STORE_FAST`` instruction
728 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000729
730
731.. opcode:: IMPORT_FROM (namei)
732
733 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
734 resulting object is pushed onto the stack, to be subsequently stored by a
735 ``STORE_FAST`` instruction.
736
737
738.. opcode:: JUMP_FORWARD (delta)
739
Georg Brandl9afde1c2007-11-01 20:32:30 +0000740 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000741
742
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000743.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000744
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000745 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000746
747
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000748.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000749
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000750 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
751
752
753.. opcode:: JUMP_IF_TRUE_OR_POP (target)
754
755 If TOS is true, sets the bytecode counter to *target* and leaves TOS
756 on the stack. Otherwise (TOS is false), TOS is popped.
757
758
759.. opcode:: JUMP_IF_FALSE_OR_POP (target)
760
761 If TOS is false, sets the bytecode counter to *target* and leaves
762 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000763
764
765.. opcode:: JUMP_ABSOLUTE (target)
766
Georg Brandl9afde1c2007-11-01 20:32:30 +0000767 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000768
769
770.. opcode:: FOR_ITER (delta)
771
Ezio Melotti7fa82222012-10-12 13:42:08 +0300772 ``TOS`` is an :term:`iterator`. Call its :meth:`~iterator.__next__` method.
773 If this yields a new value, push it on the stack (leaving the iterator below
774 it). If the iterator indicates it is exhausted ``TOS`` is popped, and the
775 byte code counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000776
Georg Brandl116aa622007-08-15 14:28:22 +0000777
778.. opcode:: LOAD_GLOBAL (namei)
779
780 Loads the global named ``co_names[namei]`` onto the stack.
781
Georg Brandl116aa622007-08-15 14:28:22 +0000782
783.. opcode:: SETUP_LOOP (delta)
784
785 Pushes a block for a loop onto the block stack. The block spans from the
786 current instruction with a size of *delta* bytes.
787
788
789.. opcode:: SETUP_EXCEPT (delta)
790
791 Pushes a try block from a try-except clause onto the block stack. *delta* points
792 to the first except block.
793
794
795.. opcode:: SETUP_FINALLY (delta)
796
797 Pushes a try block from a try-except clause onto the block stack. *delta* points
798 to the finally block.
799
Georg Brandl4833e5b2010-07-03 10:41:33 +0000800.. opcode:: STORE_MAP
Christian Heimesa62da1d2008-01-12 19:39:10 +0000801
802 Store a key and value pair in a dictionary. Pops the key and value while leaving
803 the dictionary on the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000804
805.. opcode:: LOAD_FAST (var_num)
806
807 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
808
809
810.. opcode:: STORE_FAST (var_num)
811
812 Stores TOS into the local ``co_varnames[var_num]``.
813
814
815.. opcode:: DELETE_FAST (var_num)
816
817 Deletes local ``co_varnames[var_num]``.
818
819
820.. opcode:: LOAD_CLOSURE (i)
821
822 Pushes a reference to the cell contained in slot *i* of the cell and free
823 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
824 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
825 len(co_cellvars)]``.
826
827
828.. opcode:: LOAD_DEREF (i)
829
830 Loads the cell contained in slot *i* of the cell and free variable storage.
831 Pushes a reference to the object the cell contains on the stack.
832
833
Benjamin Peterson3b0431d2013-04-30 09:41:40 -0400834.. opcode:: LOAD_CLASSDEREF (i)
835
836 Much like :opcode:`LOAD_DEREF` but first checks the locals dictionary before
837 consulting the cell. This is used for loading free variables in class
838 bodies.
839
840
Georg Brandl116aa622007-08-15 14:28:22 +0000841.. opcode:: STORE_DEREF (i)
842
843 Stores TOS into the cell contained in slot *i* of the cell and free variable
844 storage.
845
846
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000847.. opcode:: DELETE_DEREF (i)
848
849 Empties the cell contained in slot *i* of the cell and free variable storage.
850 Used by the :keyword:`del` statement.
851
852
Georg Brandl116aa622007-08-15 14:28:22 +0000853.. opcode:: RAISE_VARARGS (argc)
854
855 Raises an exception. *argc* indicates the number of parameters to the raise
856 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
857 the parameter as TOS1, and the exception as TOS.
858
859
860.. opcode:: CALL_FUNCTION (argc)
861
862 Calls a function. The low byte of *argc* indicates the number of positional
863 parameters, the high byte the number of keyword parameters. On the stack, the
864 opcode finds the keyword parameters first. For each keyword argument, the value
865 is on top of the key. Below the keyword parameters, the positional parameters
866 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl48310cd2009-01-03 21:18:54 +0000867 the function object to call is on the stack. Pops all function arguments, and
Benjamin Peterson206e3072008-10-19 14:07:49 +0000868 the function itself off the stack, and pushes the return value.
Georg Brandl116aa622007-08-15 14:28:22 +0000869
870
871.. opcode:: MAKE_FUNCTION (argc)
872
Georg Brandlc96ef1f2013-10-12 18:41:18 +0200873 Pushes a new function object on the stack. From bottom to top, the consumed
874 stack must consist of
875
876 * ``argc & 0xFF`` default argument objects in positional order
877 * ``(argc >> 8) & 0xFF`` pairs of name and default argument, with the name
878 just below the object on the stack, for keyword-only parameters
879 * ``(argc >> 16) & 0x7FFF`` parameter annotation objects
880 * a tuple listing the parameter names for the annotations (only if there are
881 ony annotation objects)
882 * the code associated with the function (at TOS1)
883 * the :term:`qualified name` of the function (at TOS)
Georg Brandl116aa622007-08-15 14:28:22 +0000884
885
886.. opcode:: MAKE_CLOSURE (argc)
887
Guido van Rossum04110fb2007-08-24 16:32:05 +0000888 Creates a new function object, sets its *__closure__* slot, and pushes it on
Andrew Svetlova5c43092012-11-23 15:28:34 +0200889 the stack. TOS is the :term:`qualified name` of the function, TOS1 is the
890 code associated with the function, and TOS2 is the tuple containing cells for
891 the closure's free variables. The function also has *argc* default parameters,
892 which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000893
894
895.. opcode:: BUILD_SLICE (argc)
896
897 .. index:: builtin: slice
898
899 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
900 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000901 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000902
903
904.. opcode:: EXTENDED_ARG (ext)
905
906 Prefixes any opcode which has an argument too big to fit into the default two
907 bytes. *ext* holds two additional bytes which, taken together with the
908 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
909 most-significant bytes.
910
911
912.. opcode:: CALL_FUNCTION_VAR (argc)
913
914 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
915 on the stack contains the variable argument list, followed by keyword and
916 positional arguments.
917
918
919.. opcode:: CALL_FUNCTION_KW (argc)
920
921 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
922 on the stack contains the keyword arguments dictionary, followed by explicit
923 keyword and positional arguments.
924
925
926.. opcode:: CALL_FUNCTION_VAR_KW (argc)
927
928 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
929 element on the stack contains the keyword arguments dictionary, followed by the
930 variable-arguments tuple, followed by explicit keyword and positional arguments.
931
932
Georg Brandl4833e5b2010-07-03 10:41:33 +0000933.. opcode:: HAVE_ARGUMENT
Georg Brandl116aa622007-08-15 14:28:22 +0000934
935 This is not really an opcode. It identifies the dividing line between opcodes
936 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
937 HAVE_ARGUMENT``.
938
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
969 Sequence of bytecodes that access a free variable (note that 'free' in
970 this context refers to names in the current scope that are referenced by
971 inner scopes or names in outer scopes that are referenced from this scope.
972 It does *not* include references to global or builtin scopes).
973
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.