blob: a377fc8394791ad409c4675b972ec1eaae43e2b0 [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
7
Brett Cannon8315fd12010-07-02 22:03:00 +00008The :mod:`dis` module supports the analysis of CPython :term:`bytecode` by
9disassembling it. The CPython bytecode which this module takes as an
Georg Brandl71515ca2009-05-17 12:29:12 +000010input is defined in the file :file:`Include/opcode.h` and used by the compiler
11and the interpreter.
Georg Brandl116aa622007-08-15 14:28:22 +000012
Georg Brandl19b7a872010-07-03 10:21:50 +000013.. impl-detail::
14
15 Bytecode is an implementation detail of the CPython interpreter! No
16 guarantees are made that bytecode will not be added, removed, or changed
17 between versions of Python. Use of this module should not be considered to
18 work across Python VMs or Python releases.
19
Brett Cannon8315fd12010-07-02 22:03:00 +000020
Georg Brandl116aa622007-08-15 14:28:22 +000021Example: Given the function :func:`myfunc`::
22
23 def myfunc(alist):
24 return len(alist)
25
26the following command can be used to get the disassembly of :func:`myfunc`::
27
28 >>> dis.dis(myfunc)
29 2 0 LOAD_GLOBAL 0 (len)
30 3 LOAD_FAST 0 (alist)
31 6 CALL_FUNCTION 1
32 9 RETURN_VALUE
33
34(The "2" is a line number).
35
36The :mod:`dis` module defines the following functions and constants:
37
38
Nick Coghlaneae2da12010-08-17 08:03:36 +000039.. function:: code_info(x=None)
40
Georg Brandl67b21b72010-08-17 15:07:14 +000041 Return a formatted multi-line string with detailed code object information
42 for the supplied function, method, source code string or code object.
Nick Coghlaneae2da12010-08-17 08:03:36 +000043
Georg Brandl67b21b72010-08-17 15:07:14 +000044 Note that the exact contents of code info strings are highly implementation
45 dependent and they may change arbitrarily across Python VMs or Python
46 releases.
Nick Coghlaneae2da12010-08-17 08:03:36 +000047
48 .. versionadded:: 3.2
49
Georg Brandl67b21b72010-08-17 15:07:14 +000050
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000051.. function:: dis(x=None)
Georg Brandl116aa622007-08-15 14:28:22 +000052
Georg Brandl67b21b72010-08-17 15:07:14 +000053 Disassemble the *x* object. *x* can denote either a module, a class, a
54 method, a function, a code object, a string of source code or a byte sequence
55 of raw bytecode. For a module, it disassembles all functions. For a class,
56 it disassembles all methods. For a code object or sequence of raw bytecode,
57 it prints one line per bytecode instruction. Strings are first compiled to
58 code objects with the :func:`compile` built-in function before being
59 disassembled. If no object is provided, this function disassembles the last
60 traceback.
Georg Brandl116aa622007-08-15 14:28:22 +000061
62
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000063.. function:: distb(tb=None)
Georg Brandl116aa622007-08-15 14:28:22 +000064
Georg Brandl4833e5b2010-07-03 10:41:33 +000065 Disassemble the top-of-stack function of a traceback, using the last
66 traceback if none was passed. The instruction causing the exception is
67 indicated.
Georg Brandl116aa622007-08-15 14:28:22 +000068
69
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000070.. function:: disassemble(code, lasti=-1)
71 disco(code, lasti=-1)
Georg Brandl116aa622007-08-15 14:28:22 +000072
Georg Brandl4833e5b2010-07-03 10:41:33 +000073 Disassemble a code object, indicating the last instruction if *lasti* was
Georg Brandl116aa622007-08-15 14:28:22 +000074 provided. The output is divided in the following columns:
75
76 #. the line number, for the first instruction of each line
77 #. the current instruction, indicated as ``-->``,
78 #. a labelled instruction, indicated with ``>>``,
79 #. the address of the instruction,
80 #. the operation code name,
81 #. operation parameters, and
82 #. interpretation of the parameters in parentheses.
83
84 The parameter interpretation recognizes local and global variable names,
85 constant values, branch targets, and compare operators.
86
87
Benjamin Peterson75edad02009-01-01 15:05:06 +000088.. function:: findlinestarts(code)
89
90 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
91 attributes of the code object *code* to find the offsets which are starts of
92 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
93
94
95.. function:: findlabels(code)
96
97 Detect all offsets in the code object *code* which are jump targets, and
98 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +000099
100
Georg Brandl116aa622007-08-15 14:28:22 +0000101.. data:: opname
102
Georg Brandl9afde1c2007-11-01 20:32:30 +0000103 Sequence of operation names, indexable using the bytecode.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
105
106.. data:: opmap
107
Georg Brandl9afde1c2007-11-01 20:32:30 +0000108 Dictionary mapping bytecodes to operation names.
Georg Brandl116aa622007-08-15 14:28:22 +0000109
110
111.. data:: cmp_op
112
113 Sequence of all compare operation names.
114
115
116.. data:: hasconst
117
Georg Brandl9afde1c2007-11-01 20:32:30 +0000118 Sequence of bytecodes that have a constant parameter.
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120
121.. data:: hasfree
122
Georg Brandl9afde1c2007-11-01 20:32:30 +0000123 Sequence of bytecodes that access a free variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125
126.. data:: hasname
127
Georg Brandl9afde1c2007-11-01 20:32:30 +0000128 Sequence of bytecodes that access an attribute by name.
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130
131.. data:: hasjrel
132
Georg Brandl9afde1c2007-11-01 20:32:30 +0000133 Sequence of bytecodes that have a relative jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135
136.. data:: hasjabs
137
Georg Brandl9afde1c2007-11-01 20:32:30 +0000138 Sequence of bytecodes that have an absolute jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000139
140
141.. data:: haslocal
142
Georg Brandl9afde1c2007-11-01 20:32:30 +0000143 Sequence of bytecodes that access a local variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000144
145
146.. data:: hascompare
147
Georg Brandl9afde1c2007-11-01 20:32:30 +0000148 Sequence of bytecodes of Boolean operations.
Georg Brandl116aa622007-08-15 14:28:22 +0000149
150
151.. _bytecodes:
152
Georg Brandl9afde1c2007-11-01 20:32:30 +0000153Python Bytecode Instructions
154----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Georg Brandl9afde1c2007-11-01 20:32:30 +0000156The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158
Georg Brandl4833e5b2010-07-03 10:41:33 +0000159**General instructions**
160
161.. opcode:: STOP_CODE
Georg Brandl116aa622007-08-15 14:28:22 +0000162
163 Indicates end-of-code to the compiler, not used by the interpreter.
164
165
Georg Brandl4833e5b2010-07-03 10:41:33 +0000166.. opcode:: NOP
Georg Brandl116aa622007-08-15 14:28:22 +0000167
168 Do nothing code. Used as a placeholder by the bytecode optimizer.
169
170
Georg Brandl4833e5b2010-07-03 10:41:33 +0000171.. opcode:: POP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000172
173 Removes the top-of-stack (TOS) item.
174
175
Georg Brandl4833e5b2010-07-03 10:41:33 +0000176.. opcode:: ROT_TWO
Georg Brandl116aa622007-08-15 14:28:22 +0000177
178 Swaps the two top-most stack items.
179
180
Georg Brandl4833e5b2010-07-03 10:41:33 +0000181.. opcode:: ROT_THREE
Georg Brandl116aa622007-08-15 14:28:22 +0000182
183 Lifts second and third stack item one position up, moves top down to position
184 three.
185
186
Georg Brandl4833e5b2010-07-03 10:41:33 +0000187.. opcode:: DUP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000188
189 Duplicates the reference on top of the stack.
190
Georg Brandl4833e5b2010-07-03 10:41:33 +0000191
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000192.. opcode:: DUP_TOP_TWO
193
194 Duplicates the two references on top of the stack, leaving them in the
195 same order.
196
197
Georg Brandl4833e5b2010-07-03 10:41:33 +0000198**Unary operations**
199
200Unary operations take the top of the stack, apply the operation, and push the
Georg Brandl116aa622007-08-15 14:28:22 +0000201result back on the stack.
202
Georg Brandl4833e5b2010-07-03 10:41:33 +0000203.. opcode:: UNARY_POSITIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000204
205 Implements ``TOS = +TOS``.
206
207
Georg Brandl4833e5b2010-07-03 10:41:33 +0000208.. opcode:: UNARY_NEGATIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000209
210 Implements ``TOS = -TOS``.
211
212
Georg Brandl4833e5b2010-07-03 10:41:33 +0000213.. opcode:: UNARY_NOT
Georg Brandl116aa622007-08-15 14:28:22 +0000214
215 Implements ``TOS = not TOS``.
216
217
Georg Brandl4833e5b2010-07-03 10:41:33 +0000218.. opcode:: UNARY_INVERT
Georg Brandl116aa622007-08-15 14:28:22 +0000219
220 Implements ``TOS = ~TOS``.
221
222
Georg Brandl4833e5b2010-07-03 10:41:33 +0000223.. opcode:: GET_ITER
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225 Implements ``TOS = iter(TOS)``.
226
Georg Brandl4833e5b2010-07-03 10:41:33 +0000227
228**Binary operations**
229
Georg Brandl116aa622007-08-15 14:28:22 +0000230Binary operations remove the top of the stack (TOS) and the second top-most
231stack item (TOS1) from the stack. They perform the operation, and put the
232result back on the stack.
233
Georg Brandl4833e5b2010-07-03 10:41:33 +0000234.. opcode:: BINARY_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000235
236 Implements ``TOS = TOS1 ** TOS``.
237
238
Georg Brandl4833e5b2010-07-03 10:41:33 +0000239.. opcode:: BINARY_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000240
241 Implements ``TOS = TOS1 * TOS``.
242
243
Georg Brandl4833e5b2010-07-03 10:41:33 +0000244.. opcode:: BINARY_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000245
246 Implements ``TOS = TOS1 // TOS``.
247
248
Georg Brandl4833e5b2010-07-03 10:41:33 +0000249.. opcode:: BINARY_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000250
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000251 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000252
253
Georg Brandl4833e5b2010-07-03 10:41:33 +0000254.. opcode:: BINARY_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000255
256 Implements ``TOS = TOS1 % TOS``.
257
258
Georg Brandl4833e5b2010-07-03 10:41:33 +0000259.. opcode:: BINARY_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000260
261 Implements ``TOS = TOS1 + TOS``.
262
263
Georg Brandl4833e5b2010-07-03 10:41:33 +0000264.. opcode:: BINARY_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000265
266 Implements ``TOS = TOS1 - TOS``.
267
268
Georg Brandl4833e5b2010-07-03 10:41:33 +0000269.. opcode:: BINARY_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000270
271 Implements ``TOS = TOS1[TOS]``.
272
273
Georg Brandl4833e5b2010-07-03 10:41:33 +0000274.. opcode:: BINARY_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000275
276 Implements ``TOS = TOS1 << TOS``.
277
278
Georg Brandl4833e5b2010-07-03 10:41:33 +0000279.. opcode:: BINARY_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000280
281 Implements ``TOS = TOS1 >> TOS``.
282
283
Georg Brandl4833e5b2010-07-03 10:41:33 +0000284.. opcode:: BINARY_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000285
286 Implements ``TOS = TOS1 & TOS``.
287
288
Georg Brandl4833e5b2010-07-03 10:41:33 +0000289.. opcode:: BINARY_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000290
291 Implements ``TOS = TOS1 ^ TOS``.
292
293
Georg Brandl4833e5b2010-07-03 10:41:33 +0000294.. opcode:: BINARY_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000295
296 Implements ``TOS = TOS1 | TOS``.
297
Georg Brandl4833e5b2010-07-03 10:41:33 +0000298
299**In-place operations**
300
Georg Brandl116aa622007-08-15 14:28:22 +0000301In-place operations are like binary operations, in that they remove TOS and
302TOS1, and push the result back on the stack, but the operation is done in-place
303when TOS1 supports it, and the resulting TOS may be (but does not have to be)
304the original TOS1.
305
Georg Brandl4833e5b2010-07-03 10:41:33 +0000306.. opcode:: INPLACE_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000307
308 Implements in-place ``TOS = TOS1 ** TOS``.
309
310
Georg Brandl4833e5b2010-07-03 10:41:33 +0000311.. opcode:: INPLACE_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000312
313 Implements in-place ``TOS = TOS1 * TOS``.
314
315
Georg Brandl4833e5b2010-07-03 10:41:33 +0000316.. opcode:: INPLACE_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000317
318 Implements in-place ``TOS = TOS1 // TOS``.
319
320
Georg Brandl4833e5b2010-07-03 10:41:33 +0000321.. opcode:: INPLACE_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000322
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000323 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000324
325
Georg Brandl4833e5b2010-07-03 10:41:33 +0000326.. opcode:: INPLACE_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000327
328 Implements in-place ``TOS = TOS1 % TOS``.
329
330
Georg Brandl4833e5b2010-07-03 10:41:33 +0000331.. opcode:: INPLACE_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000332
333 Implements in-place ``TOS = TOS1 + TOS``.
334
335
Georg Brandl4833e5b2010-07-03 10:41:33 +0000336.. opcode:: INPLACE_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000337
338 Implements in-place ``TOS = TOS1 - TOS``.
339
340
Georg Brandl4833e5b2010-07-03 10:41:33 +0000341.. opcode:: INPLACE_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000342
343 Implements in-place ``TOS = TOS1 << TOS``.
344
345
Georg Brandl4833e5b2010-07-03 10:41:33 +0000346.. opcode:: INPLACE_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000347
348 Implements in-place ``TOS = TOS1 >> TOS``.
349
350
Georg Brandl4833e5b2010-07-03 10:41:33 +0000351.. opcode:: INPLACE_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000352
353 Implements in-place ``TOS = TOS1 & TOS``.
354
355
Georg Brandl4833e5b2010-07-03 10:41:33 +0000356.. opcode:: INPLACE_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000357
358 Implements in-place ``TOS = TOS1 ^ TOS``.
359
360
Georg Brandl4833e5b2010-07-03 10:41:33 +0000361.. opcode:: INPLACE_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000362
363 Implements in-place ``TOS = TOS1 | TOS``.
364
Georg Brandl116aa622007-08-15 14:28:22 +0000365
Georg Brandl4833e5b2010-07-03 10:41:33 +0000366.. opcode:: STORE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000367
368 Implements ``TOS1[TOS] = TOS2``.
369
370
Georg Brandl4833e5b2010-07-03 10:41:33 +0000371.. opcode:: DELETE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000372
373 Implements ``del TOS1[TOS]``.
374
Georg Brandl116aa622007-08-15 14:28:22 +0000375
Georg Brandl4833e5b2010-07-03 10:41:33 +0000376**Miscellaneous opcodes**
Georg Brandl116aa622007-08-15 14:28:22 +0000377
Georg Brandl4833e5b2010-07-03 10:41:33 +0000378.. opcode:: PRINT_EXPR
Georg Brandl116aa622007-08-15 14:28:22 +0000379
380 Implements the expression statement for the interactive mode. TOS is removed
381 from the stack and printed. In non-interactive mode, an expression statement is
382 terminated with ``POP_STACK``.
383
384
Georg Brandl4833e5b2010-07-03 10:41:33 +0000385.. opcode:: BREAK_LOOP
Georg Brandl116aa622007-08-15 14:28:22 +0000386
387 Terminates a loop due to a :keyword:`break` statement.
388
389
390.. opcode:: CONTINUE_LOOP (target)
391
392 Continues a loop due to a :keyword:`continue` statement. *target* is the
393 address to jump to (which should be a ``FOR_ITER`` instruction).
394
395
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000396.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000397
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000398 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000399
400
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000401.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000402
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000403 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
404
405
406.. opcode:: MAP_ADD (i)
407
408 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
409 comprehensions.
410
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000411For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the
412added value or key/value pair is popped off, the container object remains on
413the stack so that it is available for further iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000414
415
Georg Brandl4833e5b2010-07-03 10:41:33 +0000416.. opcode:: RETURN_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000417
418 Returns with TOS to the caller of the function.
419
420
Georg Brandl4833e5b2010-07-03 10:41:33 +0000421.. opcode:: YIELD_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000422
Georg Brandl9afde1c2007-11-01 20:32:30 +0000423 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000424
425
Georg Brandl4833e5b2010-07-03 10:41:33 +0000426.. opcode:: IMPORT_STAR
Georg Brandl116aa622007-08-15 14:28:22 +0000427
428 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
429 local namespace. The module is popped after loading all names. This opcode
430 implements ``from module import *``.
431
432
Georg Brandl4833e5b2010-07-03 10:41:33 +0000433.. opcode:: POP_BLOCK
Georg Brandl116aa622007-08-15 14:28:22 +0000434
435 Removes one block from the block stack. Per frame, there is a stack of blocks,
436 denoting nested loops, try statements, and such.
437
438
Georg Brandl4833e5b2010-07-03 10:41:33 +0000439.. opcode:: POP_EXCEPT
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000440
441 Removes one block from the block stack. The popped block must be an exception
442 handler block, as implicitly created when entering an except handler.
443 In addition to popping extraneous values from the frame stack, the
444 last three popped values are used to restore the exception state.
445
446
Georg Brandl4833e5b2010-07-03 10:41:33 +0000447.. opcode:: END_FINALLY
Georg Brandl116aa622007-08-15 14:28:22 +0000448
449 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
450 exception has to be re-raised, or whether the function returns, and continues
451 with the outer-next block.
452
453
Georg Brandl4833e5b2010-07-03 10:41:33 +0000454.. opcode:: LOAD_BUILD_CLASS
Georg Brandl116aa622007-08-15 14:28:22 +0000455
Georg Brandl5ac22302008-07-20 21:39:03 +0000456 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Benjamin Petersonaac8fd32008-07-20 22:02:26 +0000457 by ``CALL_FUNCTION`` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000458
Guido van Rossum04110fb2007-08-24 16:32:05 +0000459
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000460.. opcode:: SETUP_WITH (delta)
461
462 This opcode performs several operations before a with block starts. First,
463 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
464 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
465 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
466 is pushed. Finally, the result of calling the enter method is pushed onto
467 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
468 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
469 :opcode:`UNPACK_SEQUENCE`).
470
471
Georg Brandl4833e5b2010-07-03 10:41:33 +0000472.. opcode:: WITH_CLEANUP
Guido van Rossum04110fb2007-08-24 16:32:05 +0000473
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000474 Cleans up the stack when a :keyword:`with` statement block exits. TOS is
475 the context manager's :meth:`__exit__` bound method. Below TOS are 1--3
476 values indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000477
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000478 * SECOND = ``None``
479 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
480 * SECOND = ``WHY_*``; no retval below it
481 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000482
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000483 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
484 ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000485
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000486 If the stack represents an exception, *and* the function call returns
487 a 'true' value, this information is "zapped" and replaced with a single
488 ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception.
489 (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000490
Georg Brandl9afde1c2007-11-01 20:32:30 +0000491 .. XXX explain the WHY stuff!
492
Guido van Rossum04110fb2007-08-24 16:32:05 +0000493
Georg Brandl5ac22302008-07-20 21:39:03 +0000494.. opcode:: STORE_LOCALS
495
496 Pops TOS from the stack and stores it as the current frame's ``f_locals``.
497 This is used in class construction.
498
499
Georg Brandl116aa622007-08-15 14:28:22 +0000500All of the following opcodes expect arguments. An argument is two bytes, with
501the more significant byte last.
502
Georg Brandl116aa622007-08-15 14:28:22 +0000503.. opcode:: STORE_NAME (namei)
504
505 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Christian Heimes8640e742008-02-23 16:23:06 +0000506 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl116aa622007-08-15 14:28:22 +0000507 or ``STORE_GLOBAL`` if possible.
508
509
510.. opcode:: DELETE_NAME (namei)
511
512 Implements ``del name``, where *namei* is the index into :attr:`co_names`
513 attribute of the code object.
514
515
516.. opcode:: UNPACK_SEQUENCE (count)
517
518 Unpacks TOS into *count* individual values, which are put onto the stack
519 right-to-left.
520
Georg Brandl116aa622007-08-15 14:28:22 +0000521
Georg Brandl5ac22302008-07-20 21:39:03 +0000522.. opcode:: UNPACK_EX (counts)
523
524 Implements assignment with a starred target: Unpacks an iterable in TOS into
525 individual values, where the total number of values can be smaller than the
526 number of items in the iterable: one the new values will be a list of all
527 leftover items.
528
529 The low byte of *counts* is the number of values before the list value, the
530 high byte of *counts* the number of values after it. The resulting values
531 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000532
Georg Brandl5ac22302008-07-20 21:39:03 +0000533
Georg Brandl116aa622007-08-15 14:28:22 +0000534.. opcode:: STORE_ATTR (namei)
535
536 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
537 :attr:`co_names`.
538
539
540.. opcode:: DELETE_ATTR (namei)
541
542 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
543
544
545.. opcode:: STORE_GLOBAL (namei)
546
547 Works as ``STORE_NAME``, but stores the name as a global.
548
549
550.. opcode:: DELETE_GLOBAL (namei)
551
552 Works as ``DELETE_NAME``, but deletes a global name.
553
Georg Brandl116aa622007-08-15 14:28:22 +0000554
555.. opcode:: LOAD_CONST (consti)
556
557 Pushes ``co_consts[consti]`` onto the stack.
558
559
560.. opcode:: LOAD_NAME (namei)
561
562 Pushes the value associated with ``co_names[namei]`` onto the stack.
563
564
565.. opcode:: BUILD_TUPLE (count)
566
567 Creates a tuple consuming *count* items from the stack, and pushes the resulting
568 tuple onto the stack.
569
570
571.. opcode:: BUILD_LIST (count)
572
573 Works as ``BUILD_TUPLE``, but creates a list.
574
575
576.. opcode:: BUILD_SET (count)
577
578 Works as ``BUILD_TUPLE``, but creates a set.
579
580
Christian Heimesa62da1d2008-01-12 19:39:10 +0000581.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000582
Christian Heimesa62da1d2008-01-12 19:39:10 +0000583 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
584 to hold *count* entries.
Georg Brandl116aa622007-08-15 14:28:22 +0000585
586
587.. opcode:: LOAD_ATTR (namei)
588
589 Replaces TOS with ``getattr(TOS, co_names[namei])``.
590
591
592.. opcode:: COMPARE_OP (opname)
593
594 Performs a Boolean operation. The operation name can be found in
595 ``cmp_op[opname]``.
596
597
598.. opcode:: IMPORT_NAME (namei)
599
Christian Heimesa342c012008-04-20 21:01:16 +0000600 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
601 the *fromlist* and *level* arguments of :func:`__import__`. The module
602 object is pushed onto the stack. The current namespace is not affected:
603 for a proper import statement, a subsequent ``STORE_FAST`` instruction
604 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000605
606
607.. opcode:: IMPORT_FROM (namei)
608
609 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
610 resulting object is pushed onto the stack, to be subsequently stored by a
611 ``STORE_FAST`` instruction.
612
613
614.. opcode:: JUMP_FORWARD (delta)
615
Georg Brandl9afde1c2007-11-01 20:32:30 +0000616 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000617
618
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000619.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000620
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000621 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000622
623
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000624.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000625
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000626 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
627
628
629.. opcode:: JUMP_IF_TRUE_OR_POP (target)
630
631 If TOS is true, sets the bytecode counter to *target* and leaves TOS
632 on the stack. Otherwise (TOS is false), TOS is popped.
633
634
635.. opcode:: JUMP_IF_FALSE_OR_POP (target)
636
637 If TOS is false, sets the bytecode counter to *target* and leaves
638 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000639
640
641.. opcode:: JUMP_ABSOLUTE (target)
642
Georg Brandl9afde1c2007-11-01 20:32:30 +0000643 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000644
645
646.. opcode:: FOR_ITER (delta)
647
Georg Brandl9afde1c2007-11-01 20:32:30 +0000648 ``TOS`` is an :term:`iterator`. Call its :meth:`__next__` method. If this
649 yields a new value, push it on the stack (leaving the iterator below it). If
650 the iterator indicates it is exhausted ``TOS`` is popped, and the byte code
651 counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000652
Georg Brandl116aa622007-08-15 14:28:22 +0000653
654.. opcode:: LOAD_GLOBAL (namei)
655
656 Loads the global named ``co_names[namei]`` onto the stack.
657
Georg Brandl116aa622007-08-15 14:28:22 +0000658
659.. opcode:: SETUP_LOOP (delta)
660
661 Pushes a block for a loop onto the block stack. The block spans from the
662 current instruction with a size of *delta* bytes.
663
664
665.. opcode:: SETUP_EXCEPT (delta)
666
667 Pushes a try block from a try-except clause onto the block stack. *delta* points
668 to the first except block.
669
670
671.. opcode:: SETUP_FINALLY (delta)
672
673 Pushes a try block from a try-except clause onto the block stack. *delta* points
674 to the finally block.
675
Georg Brandl4833e5b2010-07-03 10:41:33 +0000676.. opcode:: STORE_MAP
Christian Heimesa62da1d2008-01-12 19:39:10 +0000677
678 Store a key and value pair in a dictionary. Pops the key and value while leaving
679 the dictionary on the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000680
681.. opcode:: LOAD_FAST (var_num)
682
683 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
684
685
686.. opcode:: STORE_FAST (var_num)
687
688 Stores TOS into the local ``co_varnames[var_num]``.
689
690
691.. opcode:: DELETE_FAST (var_num)
692
693 Deletes local ``co_varnames[var_num]``.
694
695
696.. opcode:: LOAD_CLOSURE (i)
697
698 Pushes a reference to the cell contained in slot *i* of the cell and free
699 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
700 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
701 len(co_cellvars)]``.
702
703
704.. opcode:: LOAD_DEREF (i)
705
706 Loads the cell contained in slot *i* of the cell and free variable storage.
707 Pushes a reference to the object the cell contains on the stack.
708
709
710.. opcode:: STORE_DEREF (i)
711
712 Stores TOS into the cell contained in slot *i* of the cell and free variable
713 storage.
714
715
716.. opcode:: SET_LINENO (lineno)
717
718 This opcode is obsolete.
719
720
721.. opcode:: RAISE_VARARGS (argc)
722
723 Raises an exception. *argc* indicates the number of parameters to the raise
724 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
725 the parameter as TOS1, and the exception as TOS.
726
727
728.. opcode:: CALL_FUNCTION (argc)
729
730 Calls a function. The low byte of *argc* indicates the number of positional
731 parameters, the high byte the number of keyword parameters. On the stack, the
732 opcode finds the keyword parameters first. For each keyword argument, the value
733 is on top of the key. Below the keyword parameters, the positional parameters
734 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl48310cd2009-01-03 21:18:54 +0000735 the function object to call is on the stack. Pops all function arguments, and
Benjamin Peterson206e3072008-10-19 14:07:49 +0000736 the function itself off the stack, and pushes the return value.
Georg Brandl116aa622007-08-15 14:28:22 +0000737
738
739.. opcode:: MAKE_FUNCTION (argc)
740
741 Pushes a new function object on the stack. TOS is the code associated with the
742 function. The function object is defined to have *argc* default parameters,
743 which are found below TOS.
744
745
746.. opcode:: MAKE_CLOSURE (argc)
747
Guido van Rossum04110fb2007-08-24 16:32:05 +0000748 Creates a new function object, sets its *__closure__* slot, and pushes it on
749 the stack. TOS is the code associated with the function, TOS1 the tuple
750 containing cells for the closure's free variables. The function also has
751 *argc* default parameters, which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000752
753
754.. opcode:: BUILD_SLICE (argc)
755
756 .. index:: builtin: slice
757
758 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
759 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000760 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000761
762
763.. opcode:: EXTENDED_ARG (ext)
764
765 Prefixes any opcode which has an argument too big to fit into the default two
766 bytes. *ext* holds two additional bytes which, taken together with the
767 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
768 most-significant bytes.
769
770
771.. opcode:: CALL_FUNCTION_VAR (argc)
772
773 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
774 on the stack contains the variable argument list, followed by keyword and
775 positional arguments.
776
777
778.. opcode:: CALL_FUNCTION_KW (argc)
779
780 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
781 on the stack contains the keyword arguments dictionary, followed by explicit
782 keyword and positional arguments.
783
784
785.. opcode:: CALL_FUNCTION_VAR_KW (argc)
786
787 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
788 element on the stack contains the keyword arguments dictionary, followed by the
789 variable-arguments tuple, followed by explicit keyword and positional arguments.
790
791
Georg Brandl4833e5b2010-07-03 10:41:33 +0000792.. opcode:: HAVE_ARGUMENT
Georg Brandl116aa622007-08-15 14:28:22 +0000793
794 This is not really an opcode. It identifies the dividing line between opcodes
795 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
796 HAVE_ARGUMENT``.
797