blob: 6d08a0c5e28358131297be4955d26315d6ad5003 [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
29the following command can be used to get the disassembly of :func:`myfunc`::
30
31 >>> dis.dis(myfunc)
32 2 0 LOAD_GLOBAL 0 (len)
33 3 LOAD_FAST 0 (alist)
34 6 CALL_FUNCTION 1
35 9 RETURN_VALUE
36
37(The "2" is a line number).
38
39The :mod:`dis` module defines the following functions and constants:
40
41
Nick Coghlane8814fb2010-09-10 14:08:04 +000042.. function:: code_info(x)
Nick Coghlaneae2da12010-08-17 08:03:36 +000043
Georg Brandl67b21b72010-08-17 15:07:14 +000044 Return a formatted multi-line string with detailed code object information
45 for the supplied function, method, source code string or code object.
Nick Coghlaneae2da12010-08-17 08:03:36 +000046
Georg Brandl67b21b72010-08-17 15:07:14 +000047 Note that the exact contents of code info strings are highly implementation
48 dependent and they may change arbitrarily across Python VMs or Python
49 releases.
Nick Coghlaneae2da12010-08-17 08:03:36 +000050
51 .. versionadded:: 3.2
52
Georg Brandl67b21b72010-08-17 15:07:14 +000053
Nick Coghlane8814fb2010-09-10 14:08:04 +000054.. function:: show_code(x)
55
56 Print detailed code object information for the supplied function, method,
57 source code string or code object to stdout.
58
59 This is a convenient shorthand for ``print(code_info(x))``, intended for
60 interactive exploration at the interpreter prompt.
61
62 .. versionadded:: 3.2
63
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000064.. function:: dis(x=None)
Georg Brandl116aa622007-08-15 14:28:22 +000065
Georg Brandl67b21b72010-08-17 15:07:14 +000066 Disassemble the *x* object. *x* can denote either a module, a class, a
67 method, a function, a code object, a string of source code or a byte sequence
68 of raw bytecode. For a module, it disassembles all functions. For a class,
69 it disassembles all methods. For a code object or sequence of raw bytecode,
70 it prints one line per bytecode instruction. Strings are first compiled to
71 code objects with the :func:`compile` built-in function before being
72 disassembled. If no object is provided, this function disassembles the last
73 traceback.
Georg Brandl116aa622007-08-15 14:28:22 +000074
75
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000076.. function:: distb(tb=None)
Georg Brandl116aa622007-08-15 14:28:22 +000077
Georg Brandl4833e5b2010-07-03 10:41:33 +000078 Disassemble the top-of-stack function of a traceback, using the last
79 traceback if none was passed. The instruction causing the exception is
80 indicated.
Georg Brandl116aa622007-08-15 14:28:22 +000081
82
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000083.. function:: disassemble(code, lasti=-1)
84 disco(code, lasti=-1)
Georg Brandl116aa622007-08-15 14:28:22 +000085
Georg Brandl4833e5b2010-07-03 10:41:33 +000086 Disassemble a code object, indicating the last instruction if *lasti* was
Georg Brandl116aa622007-08-15 14:28:22 +000087 provided. The output is divided in the following columns:
88
89 #. the line number, for the first instruction of each line
90 #. the current instruction, indicated as ``-->``,
91 #. a labelled instruction, indicated with ``>>``,
92 #. the address of the instruction,
93 #. the operation code name,
94 #. operation parameters, and
95 #. interpretation of the parameters in parentheses.
96
97 The parameter interpretation recognizes local and global variable names,
98 constant values, branch targets, and compare operators.
99
100
Benjamin Peterson75edad02009-01-01 15:05:06 +0000101.. function:: findlinestarts(code)
102
103 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
104 attributes of the code object *code* to find the offsets which are starts of
105 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
106
107
108.. function:: findlabels(code)
109
110 Detect all offsets in the code object *code* which are jump targets, and
111 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +0000112
113
Georg Brandl116aa622007-08-15 14:28:22 +0000114.. data:: opname
115
Georg Brandl9afde1c2007-11-01 20:32:30 +0000116 Sequence of operation names, indexable using the bytecode.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118
119.. data:: opmap
120
Georg Brandl23798772010-10-17 11:29:07 +0000121 Dictionary mapping operation names to bytecodes.
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123
124.. data:: cmp_op
125
126 Sequence of all compare operation names.
127
128
129.. data:: hasconst
130
Georg Brandl9afde1c2007-11-01 20:32:30 +0000131 Sequence of bytecodes that have a constant parameter.
Georg Brandl116aa622007-08-15 14:28:22 +0000132
133
134.. data:: hasfree
135
Georg Brandl9afde1c2007-11-01 20:32:30 +0000136 Sequence of bytecodes that access a free variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138
139.. data:: hasname
140
Georg Brandl9afde1c2007-11-01 20:32:30 +0000141 Sequence of bytecodes that access an attribute by name.
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143
144.. data:: hasjrel
145
Georg Brandl9afde1c2007-11-01 20:32:30 +0000146 Sequence of bytecodes that have a relative jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000147
148
149.. data:: hasjabs
150
Georg Brandl9afde1c2007-11-01 20:32:30 +0000151 Sequence of bytecodes that have an absolute jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153
154.. data:: haslocal
155
Georg Brandl9afde1c2007-11-01 20:32:30 +0000156 Sequence of bytecodes that access a local variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158
159.. data:: hascompare
160
Georg Brandl9afde1c2007-11-01 20:32:30 +0000161 Sequence of bytecodes of Boolean operations.
Georg Brandl116aa622007-08-15 14:28:22 +0000162
163
164.. _bytecodes:
165
Georg Brandl9afde1c2007-11-01 20:32:30 +0000166Python Bytecode Instructions
167----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000168
Georg Brandl9afde1c2007-11-01 20:32:30 +0000169The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000170
171
Georg Brandl4833e5b2010-07-03 10:41:33 +0000172**General instructions**
173
Georg Brandl4833e5b2010-07-03 10:41:33 +0000174.. opcode:: NOP
Georg Brandl116aa622007-08-15 14:28:22 +0000175
176 Do nothing code. Used as a placeholder by the bytecode optimizer.
177
178
Georg Brandl4833e5b2010-07-03 10:41:33 +0000179.. opcode:: POP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000180
181 Removes the top-of-stack (TOS) item.
182
183
Georg Brandl4833e5b2010-07-03 10:41:33 +0000184.. opcode:: ROT_TWO
Georg Brandl116aa622007-08-15 14:28:22 +0000185
186 Swaps the two top-most stack items.
187
188
Georg Brandl4833e5b2010-07-03 10:41:33 +0000189.. opcode:: ROT_THREE
Georg Brandl116aa622007-08-15 14:28:22 +0000190
191 Lifts second and third stack item one position up, moves top down to position
192 three.
193
194
Georg Brandl4833e5b2010-07-03 10:41:33 +0000195.. opcode:: DUP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000196
197 Duplicates the reference on top of the stack.
198
Georg Brandl4833e5b2010-07-03 10:41:33 +0000199
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000200.. opcode:: DUP_TOP_TWO
201
202 Duplicates the two references on top of the stack, leaving them in the
203 same order.
204
205
Georg Brandl4833e5b2010-07-03 10:41:33 +0000206**Unary operations**
207
208Unary operations take the top of the stack, apply the operation, and push the
Georg Brandl116aa622007-08-15 14:28:22 +0000209result back on the stack.
210
Georg Brandl4833e5b2010-07-03 10:41:33 +0000211.. opcode:: UNARY_POSITIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000212
213 Implements ``TOS = +TOS``.
214
215
Georg Brandl4833e5b2010-07-03 10:41:33 +0000216.. opcode:: UNARY_NEGATIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000217
218 Implements ``TOS = -TOS``.
219
220
Georg Brandl4833e5b2010-07-03 10:41:33 +0000221.. opcode:: UNARY_NOT
Georg Brandl116aa622007-08-15 14:28:22 +0000222
223 Implements ``TOS = not TOS``.
224
225
Georg Brandl4833e5b2010-07-03 10:41:33 +0000226.. opcode:: UNARY_INVERT
Georg Brandl116aa622007-08-15 14:28:22 +0000227
228 Implements ``TOS = ~TOS``.
229
230
Georg Brandl4833e5b2010-07-03 10:41:33 +0000231.. opcode:: GET_ITER
Georg Brandl116aa622007-08-15 14:28:22 +0000232
233 Implements ``TOS = iter(TOS)``.
234
Georg Brandl4833e5b2010-07-03 10:41:33 +0000235
236**Binary operations**
237
Georg Brandl116aa622007-08-15 14:28:22 +0000238Binary operations remove the top of the stack (TOS) and the second top-most
239stack item (TOS1) from the stack. They perform the operation, and put the
240result back on the stack.
241
Georg Brandl4833e5b2010-07-03 10:41:33 +0000242.. opcode:: BINARY_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000243
244 Implements ``TOS = TOS1 ** TOS``.
245
246
Georg Brandl4833e5b2010-07-03 10:41:33 +0000247.. opcode:: BINARY_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000248
249 Implements ``TOS = TOS1 * TOS``.
250
251
Georg Brandl4833e5b2010-07-03 10:41:33 +0000252.. opcode:: BINARY_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000253
254 Implements ``TOS = TOS1 // TOS``.
255
256
Georg Brandl4833e5b2010-07-03 10:41:33 +0000257.. opcode:: BINARY_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000258
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000259 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000260
261
Georg Brandl4833e5b2010-07-03 10:41:33 +0000262.. opcode:: BINARY_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264 Implements ``TOS = TOS1 % TOS``.
265
266
Georg Brandl4833e5b2010-07-03 10:41:33 +0000267.. opcode:: BINARY_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000268
269 Implements ``TOS = TOS1 + TOS``.
270
271
Georg Brandl4833e5b2010-07-03 10:41:33 +0000272.. opcode:: BINARY_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000273
274 Implements ``TOS = TOS1 - TOS``.
275
276
Georg Brandl4833e5b2010-07-03 10:41:33 +0000277.. opcode:: BINARY_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000278
279 Implements ``TOS = TOS1[TOS]``.
280
281
Georg Brandl4833e5b2010-07-03 10:41:33 +0000282.. opcode:: BINARY_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000283
284 Implements ``TOS = TOS1 << TOS``.
285
286
Georg Brandl4833e5b2010-07-03 10:41:33 +0000287.. opcode:: BINARY_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000288
289 Implements ``TOS = TOS1 >> TOS``.
290
291
Georg Brandl4833e5b2010-07-03 10:41:33 +0000292.. opcode:: BINARY_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000293
294 Implements ``TOS = TOS1 & TOS``.
295
296
Georg Brandl4833e5b2010-07-03 10:41:33 +0000297.. opcode:: BINARY_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000298
299 Implements ``TOS = TOS1 ^ TOS``.
300
301
Georg Brandl4833e5b2010-07-03 10:41:33 +0000302.. opcode:: BINARY_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000303
304 Implements ``TOS = TOS1 | TOS``.
305
Georg Brandl4833e5b2010-07-03 10:41:33 +0000306
307**In-place operations**
308
Georg Brandl116aa622007-08-15 14:28:22 +0000309In-place operations are like binary operations, in that they remove TOS and
310TOS1, and push the result back on the stack, but the operation is done in-place
311when TOS1 supports it, and the resulting TOS may be (but does not have to be)
312the original TOS1.
313
Georg Brandl4833e5b2010-07-03 10:41:33 +0000314.. opcode:: INPLACE_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000315
316 Implements in-place ``TOS = TOS1 ** TOS``.
317
318
Georg Brandl4833e5b2010-07-03 10:41:33 +0000319.. opcode:: INPLACE_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000320
321 Implements in-place ``TOS = TOS1 * TOS``.
322
323
Georg Brandl4833e5b2010-07-03 10:41:33 +0000324.. opcode:: INPLACE_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000325
326 Implements in-place ``TOS = TOS1 // TOS``.
327
328
Georg Brandl4833e5b2010-07-03 10:41:33 +0000329.. opcode:: INPLACE_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000330
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000331 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000332
333
Georg Brandl4833e5b2010-07-03 10:41:33 +0000334.. opcode:: INPLACE_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000335
336 Implements in-place ``TOS = TOS1 % TOS``.
337
338
Georg Brandl4833e5b2010-07-03 10:41:33 +0000339.. opcode:: INPLACE_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000340
341 Implements in-place ``TOS = TOS1 + TOS``.
342
343
Georg Brandl4833e5b2010-07-03 10:41:33 +0000344.. opcode:: INPLACE_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000345
346 Implements in-place ``TOS = TOS1 - TOS``.
347
348
Georg Brandl4833e5b2010-07-03 10:41:33 +0000349.. opcode:: INPLACE_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000350
351 Implements in-place ``TOS = TOS1 << TOS``.
352
353
Georg Brandl4833e5b2010-07-03 10:41:33 +0000354.. opcode:: INPLACE_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000355
356 Implements in-place ``TOS = TOS1 >> TOS``.
357
358
Georg Brandl4833e5b2010-07-03 10:41:33 +0000359.. opcode:: INPLACE_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000360
361 Implements in-place ``TOS = TOS1 & TOS``.
362
363
Georg Brandl4833e5b2010-07-03 10:41:33 +0000364.. opcode:: INPLACE_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000365
366 Implements in-place ``TOS = TOS1 ^ TOS``.
367
368
Georg Brandl4833e5b2010-07-03 10:41:33 +0000369.. opcode:: INPLACE_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000370
371 Implements in-place ``TOS = TOS1 | TOS``.
372
Georg Brandl116aa622007-08-15 14:28:22 +0000373
Georg Brandl4833e5b2010-07-03 10:41:33 +0000374.. opcode:: STORE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000375
376 Implements ``TOS1[TOS] = TOS2``.
377
378
Georg Brandl4833e5b2010-07-03 10:41:33 +0000379.. opcode:: DELETE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000380
381 Implements ``del TOS1[TOS]``.
382
Georg Brandl116aa622007-08-15 14:28:22 +0000383
Georg Brandl4833e5b2010-07-03 10:41:33 +0000384**Miscellaneous opcodes**
Georg Brandl116aa622007-08-15 14:28:22 +0000385
Georg Brandl4833e5b2010-07-03 10:41:33 +0000386.. opcode:: PRINT_EXPR
Georg Brandl116aa622007-08-15 14:28:22 +0000387
388 Implements the expression statement for the interactive mode. TOS is removed
389 from the stack and printed. In non-interactive mode, an expression statement is
390 terminated with ``POP_STACK``.
391
392
Georg Brandl4833e5b2010-07-03 10:41:33 +0000393.. opcode:: BREAK_LOOP
Georg Brandl116aa622007-08-15 14:28:22 +0000394
395 Terminates a loop due to a :keyword:`break` statement.
396
397
398.. opcode:: CONTINUE_LOOP (target)
399
400 Continues a loop due to a :keyword:`continue` statement. *target* is the
401 address to jump to (which should be a ``FOR_ITER`` instruction).
402
403
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000404.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000405
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000406 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000407
408
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000409.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000410
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000411 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
412
413
414.. opcode:: MAP_ADD (i)
415
416 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
417 comprehensions.
418
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000419For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the
420added value or key/value pair is popped off, the container object remains on
421the stack so that it is available for further iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000422
423
Georg Brandl4833e5b2010-07-03 10:41:33 +0000424.. opcode:: RETURN_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000425
426 Returns with TOS to the caller of the function.
427
428
Georg Brandl4833e5b2010-07-03 10:41:33 +0000429.. opcode:: YIELD_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000430
Georg Brandl9afde1c2007-11-01 20:32:30 +0000431 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000432
433
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000434.. opcode:: YIELD_FROM
435
436 Pops ``TOS`` and delegates to it as a subiterator from a :term:`generator`.
437
438 .. versionadded:: 3.3
439
440
Georg Brandl4833e5b2010-07-03 10:41:33 +0000441.. opcode:: IMPORT_STAR
Georg Brandl116aa622007-08-15 14:28:22 +0000442
443 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
444 local namespace. The module is popped after loading all names. This opcode
445 implements ``from module import *``.
446
447
Georg Brandl4833e5b2010-07-03 10:41:33 +0000448.. opcode:: POP_BLOCK
Georg Brandl116aa622007-08-15 14:28:22 +0000449
450 Removes one block from the block stack. Per frame, there is a stack of blocks,
451 denoting nested loops, try statements, and such.
452
453
Georg Brandl4833e5b2010-07-03 10:41:33 +0000454.. opcode:: POP_EXCEPT
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000455
456 Removes one block from the block stack. The popped block must be an exception
457 handler block, as implicitly created when entering an except handler.
458 In addition to popping extraneous values from the frame stack, the
459 last three popped values are used to restore the exception state.
460
461
Georg Brandl4833e5b2010-07-03 10:41:33 +0000462.. opcode:: END_FINALLY
Georg Brandl116aa622007-08-15 14:28:22 +0000463
464 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
465 exception has to be re-raised, or whether the function returns, and continues
466 with the outer-next block.
467
468
Georg Brandl4833e5b2010-07-03 10:41:33 +0000469.. opcode:: LOAD_BUILD_CLASS
Georg Brandl116aa622007-08-15 14:28:22 +0000470
Georg Brandl5ac22302008-07-20 21:39:03 +0000471 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Benjamin Petersonaac8fd32008-07-20 22:02:26 +0000472 by ``CALL_FUNCTION`` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000473
Guido van Rossum04110fb2007-08-24 16:32:05 +0000474
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000475.. opcode:: SETUP_WITH (delta)
476
477 This opcode performs several operations before a with block starts. First,
478 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
479 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
480 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
481 is pushed. Finally, the result of calling the enter method is pushed onto
482 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
483 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
484 :opcode:`UNPACK_SEQUENCE`).
485
486
Georg Brandl4833e5b2010-07-03 10:41:33 +0000487.. opcode:: WITH_CLEANUP
Guido van Rossum04110fb2007-08-24 16:32:05 +0000488
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000489 Cleans up the stack when a :keyword:`with` statement block exits. TOS is
490 the context manager's :meth:`__exit__` bound method. Below TOS are 1--3
491 values indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000492
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000493 * SECOND = ``None``
494 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
495 * SECOND = ``WHY_*``; no retval below it
496 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000497
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000498 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
499 ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000500
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000501 If the stack represents an exception, *and* the function call returns
502 a 'true' value, this information is "zapped" and replaced with a single
503 ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception.
504 (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000505
Georg Brandl9afde1c2007-11-01 20:32:30 +0000506 .. XXX explain the WHY stuff!
507
Guido van Rossum04110fb2007-08-24 16:32:05 +0000508
Georg Brandl5ac22302008-07-20 21:39:03 +0000509.. opcode:: STORE_LOCALS
510
511 Pops TOS from the stack and stores it as the current frame's ``f_locals``.
512 This is used in class construction.
513
514
Georg Brandl116aa622007-08-15 14:28:22 +0000515All of the following opcodes expect arguments. An argument is two bytes, with
516the more significant byte last.
517
Georg Brandl116aa622007-08-15 14:28:22 +0000518.. opcode:: STORE_NAME (namei)
519
520 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Christian Heimes8640e742008-02-23 16:23:06 +0000521 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl116aa622007-08-15 14:28:22 +0000522 or ``STORE_GLOBAL`` if possible.
523
524
525.. opcode:: DELETE_NAME (namei)
526
527 Implements ``del name``, where *namei* is the index into :attr:`co_names`
528 attribute of the code object.
529
530
531.. opcode:: UNPACK_SEQUENCE (count)
532
533 Unpacks TOS into *count* individual values, which are put onto the stack
534 right-to-left.
535
Georg Brandl116aa622007-08-15 14:28:22 +0000536
Georg Brandl5ac22302008-07-20 21:39:03 +0000537.. opcode:: UNPACK_EX (counts)
538
539 Implements assignment with a starred target: Unpacks an iterable in TOS into
540 individual values, where the total number of values can be smaller than the
541 number of items in the iterable: one the new values will be a list of all
542 leftover items.
543
544 The low byte of *counts* is the number of values before the list value, the
545 high byte of *counts* the number of values after it. The resulting values
546 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000547
Georg Brandl5ac22302008-07-20 21:39:03 +0000548
Georg Brandl116aa622007-08-15 14:28:22 +0000549.. opcode:: STORE_ATTR (namei)
550
551 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
552 :attr:`co_names`.
553
554
555.. opcode:: DELETE_ATTR (namei)
556
557 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
558
559
560.. opcode:: STORE_GLOBAL (namei)
561
562 Works as ``STORE_NAME``, but stores the name as a global.
563
564
565.. opcode:: DELETE_GLOBAL (namei)
566
567 Works as ``DELETE_NAME``, but deletes a global name.
568
Georg Brandl116aa622007-08-15 14:28:22 +0000569
570.. opcode:: LOAD_CONST (consti)
571
572 Pushes ``co_consts[consti]`` onto the stack.
573
574
575.. opcode:: LOAD_NAME (namei)
576
577 Pushes the value associated with ``co_names[namei]`` onto the stack.
578
579
580.. opcode:: BUILD_TUPLE (count)
581
582 Creates a tuple consuming *count* items from the stack, and pushes the resulting
583 tuple onto the stack.
584
585
586.. opcode:: BUILD_LIST (count)
587
588 Works as ``BUILD_TUPLE``, but creates a list.
589
590
591.. opcode:: BUILD_SET (count)
592
593 Works as ``BUILD_TUPLE``, but creates a set.
594
595
Christian Heimesa62da1d2008-01-12 19:39:10 +0000596.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000597
Christian Heimesa62da1d2008-01-12 19:39:10 +0000598 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
599 to hold *count* entries.
Georg Brandl116aa622007-08-15 14:28:22 +0000600
601
602.. opcode:: LOAD_ATTR (namei)
603
604 Replaces TOS with ``getattr(TOS, co_names[namei])``.
605
606
607.. opcode:: COMPARE_OP (opname)
608
609 Performs a Boolean operation. The operation name can be found in
610 ``cmp_op[opname]``.
611
612
613.. opcode:: IMPORT_NAME (namei)
614
Christian Heimesa342c012008-04-20 21:01:16 +0000615 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
616 the *fromlist* and *level* arguments of :func:`__import__`. The module
617 object is pushed onto the stack. The current namespace is not affected:
618 for a proper import statement, a subsequent ``STORE_FAST`` instruction
619 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000620
621
622.. opcode:: IMPORT_FROM (namei)
623
624 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
625 resulting object is pushed onto the stack, to be subsequently stored by a
626 ``STORE_FAST`` instruction.
627
628
629.. opcode:: JUMP_FORWARD (delta)
630
Georg Brandl9afde1c2007-11-01 20:32:30 +0000631 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000632
633
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000634.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000635
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000636 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000637
638
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000639.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000640
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000641 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
642
643
644.. opcode:: JUMP_IF_TRUE_OR_POP (target)
645
646 If TOS is true, sets the bytecode counter to *target* and leaves TOS
647 on the stack. Otherwise (TOS is false), TOS is popped.
648
649
650.. opcode:: JUMP_IF_FALSE_OR_POP (target)
651
652 If TOS is false, sets the bytecode counter to *target* and leaves
653 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000654
655
656.. opcode:: JUMP_ABSOLUTE (target)
657
Georg Brandl9afde1c2007-11-01 20:32:30 +0000658 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000659
660
661.. opcode:: FOR_ITER (delta)
662
Georg Brandl9afde1c2007-11-01 20:32:30 +0000663 ``TOS`` is an :term:`iterator`. Call its :meth:`__next__` method. If this
664 yields a new value, push it on the stack (leaving the iterator below it). If
665 the iterator indicates it is exhausted ``TOS`` is popped, and the byte code
666 counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000667
Georg Brandl116aa622007-08-15 14:28:22 +0000668
669.. opcode:: LOAD_GLOBAL (namei)
670
671 Loads the global named ``co_names[namei]`` onto the stack.
672
Georg Brandl116aa622007-08-15 14:28:22 +0000673
674.. opcode:: SETUP_LOOP (delta)
675
676 Pushes a block for a loop onto the block stack. The block spans from the
677 current instruction with a size of *delta* bytes.
678
679
680.. opcode:: SETUP_EXCEPT (delta)
681
682 Pushes a try block from a try-except clause onto the block stack. *delta* points
683 to the first except block.
684
685
686.. opcode:: SETUP_FINALLY (delta)
687
688 Pushes a try block from a try-except clause onto the block stack. *delta* points
689 to the finally block.
690
Georg Brandl4833e5b2010-07-03 10:41:33 +0000691.. opcode:: STORE_MAP
Christian Heimesa62da1d2008-01-12 19:39:10 +0000692
693 Store a key and value pair in a dictionary. Pops the key and value while leaving
694 the dictionary on the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000695
696.. opcode:: LOAD_FAST (var_num)
697
698 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
699
700
701.. opcode:: STORE_FAST (var_num)
702
703 Stores TOS into the local ``co_varnames[var_num]``.
704
705
706.. opcode:: DELETE_FAST (var_num)
707
708 Deletes local ``co_varnames[var_num]``.
709
710
711.. opcode:: LOAD_CLOSURE (i)
712
713 Pushes a reference to the cell contained in slot *i* of the cell and free
714 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
715 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
716 len(co_cellvars)]``.
717
718
719.. opcode:: LOAD_DEREF (i)
720
721 Loads the cell contained in slot *i* of the cell and free variable storage.
722 Pushes a reference to the object the cell contains on the stack.
723
724
725.. opcode:: STORE_DEREF (i)
726
727 Stores TOS into the cell contained in slot *i* of the cell and free variable
728 storage.
729
730
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000731.. opcode:: DELETE_DEREF (i)
732
733 Empties the cell contained in slot *i* of the cell and free variable storage.
734 Used by the :keyword:`del` statement.
735
736
Georg Brandl116aa622007-08-15 14:28:22 +0000737.. opcode:: RAISE_VARARGS (argc)
738
739 Raises an exception. *argc* indicates the number of parameters to the raise
740 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
741 the parameter as TOS1, and the exception as TOS.
742
743
744.. opcode:: CALL_FUNCTION (argc)
745
746 Calls a function. The low byte of *argc* indicates the number of positional
747 parameters, the high byte the number of keyword parameters. On the stack, the
748 opcode finds the keyword parameters first. For each keyword argument, the value
749 is on top of the key. Below the keyword parameters, the positional parameters
750 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl48310cd2009-01-03 21:18:54 +0000751 the function object to call is on the stack. Pops all function arguments, and
Benjamin Peterson206e3072008-10-19 14:07:49 +0000752 the function itself off the stack, and pushes the return value.
Georg Brandl116aa622007-08-15 14:28:22 +0000753
754
755.. opcode:: MAKE_FUNCTION (argc)
756
Eli Bendersky60ee0492012-03-24 18:52:45 +0200757 Pushes a new function object on the stack. TOS is the
758 :term:`qualified name` of the function; TOS1 is the code associated with
759 the function. The function object is defined to have *argc* default parameters,
760 which are found below TOS1.
Georg Brandl116aa622007-08-15 14:28:22 +0000761
762
763.. opcode:: MAKE_CLOSURE (argc)
764
Guido van Rossum04110fb2007-08-24 16:32:05 +0000765 Creates a new function object, sets its *__closure__* slot, and pushes it on
766 the stack. TOS is the code associated with the function, TOS1 the tuple
767 containing cells for the closure's free variables. The function also has
768 *argc* default parameters, which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000769
770
771.. opcode:: BUILD_SLICE (argc)
772
773 .. index:: builtin: slice
774
775 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
776 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000777 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000778
779
780.. opcode:: EXTENDED_ARG (ext)
781
782 Prefixes any opcode which has an argument too big to fit into the default two
783 bytes. *ext* holds two additional bytes which, taken together with the
784 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
785 most-significant bytes.
786
787
788.. opcode:: CALL_FUNCTION_VAR (argc)
789
790 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
791 on the stack contains the variable argument list, followed by keyword and
792 positional arguments.
793
794
795.. opcode:: CALL_FUNCTION_KW (argc)
796
797 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
798 on the stack contains the keyword arguments dictionary, followed by explicit
799 keyword and positional arguments.
800
801
802.. opcode:: CALL_FUNCTION_VAR_KW (argc)
803
804 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
805 element on the stack contains the keyword arguments dictionary, followed by the
806 variable-arguments tuple, followed by explicit keyword and positional arguments.
807
808
Georg Brandl4833e5b2010-07-03 10:41:33 +0000809.. opcode:: HAVE_ARGUMENT
Georg Brandl116aa622007-08-15 14:28:22 +0000810
811 This is not really an opcode. It identifies the dividing line between opcodes
812 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
813 HAVE_ARGUMENT``.
814