blob: 1d21c49f3bd62fcf3701a18cdf9b8968910f640c [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
Brett Cannon8315fd12010-07-02 22:03:00 +00009The :mod:`dis` module supports the analysis of CPython :term:`bytecode` by
10disassembling it. The CPython bytecode which this module takes as an
Georg Brandl71515ca2009-05-17 12:29:12 +000011input is defined in the file :file:`Include/opcode.h` and used by the compiler
12and the interpreter.
Georg Brandl116aa622007-08-15 14:28:22 +000013
Georg Brandl19b7a872010-07-03 10:21:50 +000014.. impl-detail::
15
Raymond Hettinger10480942011-01-10 03:26:08 +000016 Bytecode is an implementation detail of the CPython interpreter. No
Georg Brandl19b7a872010-07-03 10:21:50 +000017 guarantees are made that bytecode will not be added, removed, or changed
18 between versions of Python. Use of this module should not be considered to
19 work across Python VMs or Python releases.
20
Brett Cannon8315fd12010-07-02 22:03:00 +000021
Georg Brandl116aa622007-08-15 14:28:22 +000022Example: Given the function :func:`myfunc`::
23
24 def myfunc(alist):
25 return len(alist)
26
27the following command can be used to get the disassembly of :func:`myfunc`::
28
29 >>> dis.dis(myfunc)
30 2 0 LOAD_GLOBAL 0 (len)
31 3 LOAD_FAST 0 (alist)
32 6 CALL_FUNCTION 1
33 9 RETURN_VALUE
34
35(The "2" is a line number).
36
37The :mod:`dis` module defines the following functions and constants:
38
39
Nick Coghlane8814fb2010-09-10 14:08:04 +000040.. function:: code_info(x)
Nick Coghlaneae2da12010-08-17 08:03:36 +000041
Georg Brandl67b21b72010-08-17 15:07:14 +000042 Return a formatted multi-line string with detailed code object information
43 for the supplied function, method, source code string or code object.
Nick Coghlaneae2da12010-08-17 08:03:36 +000044
Georg Brandl67b21b72010-08-17 15:07:14 +000045 Note that the exact contents of code info strings are highly implementation
46 dependent and they may change arbitrarily across Python VMs or Python
47 releases.
Nick Coghlaneae2da12010-08-17 08:03:36 +000048
49 .. versionadded:: 3.2
50
Georg Brandl67b21b72010-08-17 15:07:14 +000051
Nick Coghlane8814fb2010-09-10 14:08:04 +000052.. function:: show_code(x)
53
54 Print detailed code object information for the supplied function, method,
55 source code string or code object to stdout.
56
57 This is a convenient shorthand for ``print(code_info(x))``, intended for
58 interactive exploration at the interpreter prompt.
59
60 .. versionadded:: 3.2
61
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000062.. function:: dis(x=None)
Georg Brandl116aa622007-08-15 14:28:22 +000063
Georg Brandl67b21b72010-08-17 15:07:14 +000064 Disassemble the *x* object. *x* can denote either a module, a class, a
65 method, a function, a code object, a string of source code or a byte sequence
66 of raw bytecode. For a module, it disassembles all functions. For a class,
67 it disassembles all methods. For a code object or sequence of raw bytecode,
68 it prints one line per bytecode instruction. Strings are first compiled to
69 code objects with the :func:`compile` built-in function before being
70 disassembled. If no object is provided, this function disassembles the last
71 traceback.
Georg Brandl116aa622007-08-15 14:28:22 +000072
73
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000074.. function:: distb(tb=None)
Georg Brandl116aa622007-08-15 14:28:22 +000075
Georg Brandl4833e5b2010-07-03 10:41:33 +000076 Disassemble the top-of-stack function of a traceback, using the last
77 traceback if none was passed. The instruction causing the exception is
78 indicated.
Georg Brandl116aa622007-08-15 14:28:22 +000079
80
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000081.. function:: disassemble(code, lasti=-1)
82 disco(code, lasti=-1)
Georg Brandl116aa622007-08-15 14:28:22 +000083
Georg Brandl4833e5b2010-07-03 10:41:33 +000084 Disassemble a code object, indicating the last instruction if *lasti* was
Georg Brandl116aa622007-08-15 14:28:22 +000085 provided. The output is divided in the following columns:
86
87 #. the line number, for the first instruction of each line
88 #. the current instruction, indicated as ``-->``,
89 #. a labelled instruction, indicated with ``>>``,
90 #. the address of the instruction,
91 #. the operation code name,
92 #. operation parameters, and
93 #. interpretation of the parameters in parentheses.
94
95 The parameter interpretation recognizes local and global variable names,
96 constant values, branch targets, and compare operators.
97
98
Benjamin Peterson75edad02009-01-01 15:05:06 +000099.. function:: findlinestarts(code)
100
101 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
102 attributes of the code object *code* to find the offsets which are starts of
103 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
104
105
106.. function:: findlabels(code)
107
108 Detect all offsets in the code object *code* which are jump targets, and
109 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +0000110
111
Georg Brandl116aa622007-08-15 14:28:22 +0000112.. data:: opname
113
Georg Brandl9afde1c2007-11-01 20:32:30 +0000114 Sequence of operation names, indexable using the bytecode.
Georg Brandl116aa622007-08-15 14:28:22 +0000115
116
117.. data:: opmap
118
Georg Brandl23798772010-10-17 11:29:07 +0000119 Dictionary mapping operation names to bytecodes.
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121
122.. data:: cmp_op
123
124 Sequence of all compare operation names.
125
126
127.. data:: hasconst
128
Georg Brandl9afde1c2007-11-01 20:32:30 +0000129 Sequence of bytecodes that have a constant parameter.
Georg Brandl116aa622007-08-15 14:28:22 +0000130
131
132.. data:: hasfree
133
Georg Brandl9afde1c2007-11-01 20:32:30 +0000134 Sequence of bytecodes that access a free variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136
137.. data:: hasname
138
Georg Brandl9afde1c2007-11-01 20:32:30 +0000139 Sequence of bytecodes that access an attribute by name.
Georg Brandl116aa622007-08-15 14:28:22 +0000140
141
142.. data:: hasjrel
143
Georg Brandl9afde1c2007-11-01 20:32:30 +0000144 Sequence of bytecodes that have a relative jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146
147.. data:: hasjabs
148
Georg Brandl9afde1c2007-11-01 20:32:30 +0000149 Sequence of bytecodes that have an absolute jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000150
151
152.. data:: haslocal
153
Georg Brandl9afde1c2007-11-01 20:32:30 +0000154 Sequence of bytecodes that access a local variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
156
157.. data:: hascompare
158
Georg Brandl9afde1c2007-11-01 20:32:30 +0000159 Sequence of bytecodes of Boolean operations.
Georg Brandl116aa622007-08-15 14:28:22 +0000160
161
162.. _bytecodes:
163
Georg Brandl9afde1c2007-11-01 20:32:30 +0000164Python Bytecode Instructions
165----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000166
Georg Brandl9afde1c2007-11-01 20:32:30 +0000167The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000168
169
Georg Brandl4833e5b2010-07-03 10:41:33 +0000170**General instructions**
171
172.. opcode:: STOP_CODE
Georg Brandl116aa622007-08-15 14:28:22 +0000173
174 Indicates end-of-code to the compiler, not used by the interpreter.
175
176
Georg Brandl4833e5b2010-07-03 10:41:33 +0000177.. opcode:: NOP
Georg Brandl116aa622007-08-15 14:28:22 +0000178
179 Do nothing code. Used as a placeholder by the bytecode optimizer.
180
181
Georg Brandl4833e5b2010-07-03 10:41:33 +0000182.. opcode:: POP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000183
184 Removes the top-of-stack (TOS) item.
185
186
Georg Brandl4833e5b2010-07-03 10:41:33 +0000187.. opcode:: ROT_TWO
Georg Brandl116aa622007-08-15 14:28:22 +0000188
189 Swaps the two top-most stack items.
190
191
Georg Brandl4833e5b2010-07-03 10:41:33 +0000192.. opcode:: ROT_THREE
Georg Brandl116aa622007-08-15 14:28:22 +0000193
194 Lifts second and third stack item one position up, moves top down to position
195 three.
196
197
Georg Brandl4833e5b2010-07-03 10:41:33 +0000198.. opcode:: DUP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000199
200 Duplicates the reference on top of the stack.
201
Georg Brandl4833e5b2010-07-03 10:41:33 +0000202
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000203.. opcode:: DUP_TOP_TWO
204
205 Duplicates the two references on top of the stack, leaving them in the
206 same order.
207
208
Georg Brandl4833e5b2010-07-03 10:41:33 +0000209**Unary operations**
210
211Unary operations take the top of the stack, apply the operation, and push the
Georg Brandl116aa622007-08-15 14:28:22 +0000212result back on the stack.
213
Georg Brandl4833e5b2010-07-03 10:41:33 +0000214.. opcode:: UNARY_POSITIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000215
216 Implements ``TOS = +TOS``.
217
218
Georg Brandl4833e5b2010-07-03 10:41:33 +0000219.. opcode:: UNARY_NEGATIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000220
221 Implements ``TOS = -TOS``.
222
223
Georg Brandl4833e5b2010-07-03 10:41:33 +0000224.. opcode:: UNARY_NOT
Georg Brandl116aa622007-08-15 14:28:22 +0000225
226 Implements ``TOS = not TOS``.
227
228
Georg Brandl4833e5b2010-07-03 10:41:33 +0000229.. opcode:: UNARY_INVERT
Georg Brandl116aa622007-08-15 14:28:22 +0000230
231 Implements ``TOS = ~TOS``.
232
233
Georg Brandl4833e5b2010-07-03 10:41:33 +0000234.. opcode:: GET_ITER
Georg Brandl116aa622007-08-15 14:28:22 +0000235
236 Implements ``TOS = iter(TOS)``.
237
Georg Brandl4833e5b2010-07-03 10:41:33 +0000238
239**Binary operations**
240
Georg Brandl116aa622007-08-15 14:28:22 +0000241Binary operations remove the top of the stack (TOS) and the second top-most
242stack item (TOS1) from the stack. They perform the operation, and put the
243result back on the stack.
244
Georg Brandl4833e5b2010-07-03 10:41:33 +0000245.. opcode:: BINARY_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000246
247 Implements ``TOS = TOS1 ** TOS``.
248
249
Georg Brandl4833e5b2010-07-03 10:41:33 +0000250.. opcode:: BINARY_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000251
252 Implements ``TOS = TOS1 * TOS``.
253
254
Georg Brandl4833e5b2010-07-03 10:41:33 +0000255.. opcode:: BINARY_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000256
257 Implements ``TOS = TOS1 // TOS``.
258
259
Georg Brandl4833e5b2010-07-03 10:41:33 +0000260.. opcode:: BINARY_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000261
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000262 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264
Georg Brandl4833e5b2010-07-03 10:41:33 +0000265.. opcode:: BINARY_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000266
267 Implements ``TOS = TOS1 % TOS``.
268
269
Georg Brandl4833e5b2010-07-03 10:41:33 +0000270.. opcode:: BINARY_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000271
272 Implements ``TOS = TOS1 + TOS``.
273
274
Georg Brandl4833e5b2010-07-03 10:41:33 +0000275.. opcode:: BINARY_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000276
277 Implements ``TOS = TOS1 - TOS``.
278
279
Georg Brandl4833e5b2010-07-03 10:41:33 +0000280.. opcode:: BINARY_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000281
282 Implements ``TOS = TOS1[TOS]``.
283
284
Georg Brandl4833e5b2010-07-03 10:41:33 +0000285.. opcode:: BINARY_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000286
287 Implements ``TOS = TOS1 << TOS``.
288
289
Georg Brandl4833e5b2010-07-03 10:41:33 +0000290.. opcode:: BINARY_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000291
292 Implements ``TOS = TOS1 >> TOS``.
293
294
Georg Brandl4833e5b2010-07-03 10:41:33 +0000295.. opcode:: BINARY_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000296
297 Implements ``TOS = TOS1 & TOS``.
298
299
Georg Brandl4833e5b2010-07-03 10:41:33 +0000300.. opcode:: BINARY_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000301
302 Implements ``TOS = TOS1 ^ TOS``.
303
304
Georg Brandl4833e5b2010-07-03 10:41:33 +0000305.. opcode:: BINARY_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000306
307 Implements ``TOS = TOS1 | TOS``.
308
Georg Brandl4833e5b2010-07-03 10:41:33 +0000309
310**In-place operations**
311
Georg Brandl116aa622007-08-15 14:28:22 +0000312In-place operations are like binary operations, in that they remove TOS and
313TOS1, and push the result back on the stack, but the operation is done in-place
314when TOS1 supports it, and the resulting TOS may be (but does not have to be)
315the original TOS1.
316
Georg Brandl4833e5b2010-07-03 10:41:33 +0000317.. opcode:: INPLACE_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000318
319 Implements in-place ``TOS = TOS1 ** TOS``.
320
321
Georg Brandl4833e5b2010-07-03 10:41:33 +0000322.. opcode:: INPLACE_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000323
324 Implements in-place ``TOS = TOS1 * TOS``.
325
326
Georg Brandl4833e5b2010-07-03 10:41:33 +0000327.. opcode:: INPLACE_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000328
329 Implements in-place ``TOS = TOS1 // TOS``.
330
331
Georg Brandl4833e5b2010-07-03 10:41:33 +0000332.. opcode:: INPLACE_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000333
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000334 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000335
336
Georg Brandl4833e5b2010-07-03 10:41:33 +0000337.. opcode:: INPLACE_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000338
339 Implements in-place ``TOS = TOS1 % TOS``.
340
341
Georg Brandl4833e5b2010-07-03 10:41:33 +0000342.. opcode:: INPLACE_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000343
344 Implements in-place ``TOS = TOS1 + TOS``.
345
346
Georg Brandl4833e5b2010-07-03 10:41:33 +0000347.. opcode:: INPLACE_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000348
349 Implements in-place ``TOS = TOS1 - TOS``.
350
351
Georg Brandl4833e5b2010-07-03 10:41:33 +0000352.. opcode:: INPLACE_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000353
354 Implements in-place ``TOS = TOS1 << TOS``.
355
356
Georg Brandl4833e5b2010-07-03 10:41:33 +0000357.. opcode:: INPLACE_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000358
359 Implements in-place ``TOS = TOS1 >> TOS``.
360
361
Georg Brandl4833e5b2010-07-03 10:41:33 +0000362.. opcode:: INPLACE_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000363
364 Implements in-place ``TOS = TOS1 & TOS``.
365
366
Georg Brandl4833e5b2010-07-03 10:41:33 +0000367.. opcode:: INPLACE_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000368
369 Implements in-place ``TOS = TOS1 ^ TOS``.
370
371
Georg Brandl4833e5b2010-07-03 10:41:33 +0000372.. opcode:: INPLACE_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000373
374 Implements in-place ``TOS = TOS1 | TOS``.
375
Georg Brandl116aa622007-08-15 14:28:22 +0000376
Georg Brandl4833e5b2010-07-03 10:41:33 +0000377.. opcode:: STORE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000378
379 Implements ``TOS1[TOS] = TOS2``.
380
381
Georg Brandl4833e5b2010-07-03 10:41:33 +0000382.. opcode:: DELETE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000383
384 Implements ``del TOS1[TOS]``.
385
Georg Brandl116aa622007-08-15 14:28:22 +0000386
Georg Brandl4833e5b2010-07-03 10:41:33 +0000387**Miscellaneous opcodes**
Georg Brandl116aa622007-08-15 14:28:22 +0000388
Georg Brandl4833e5b2010-07-03 10:41:33 +0000389.. opcode:: PRINT_EXPR
Georg Brandl116aa622007-08-15 14:28:22 +0000390
391 Implements the expression statement for the interactive mode. TOS is removed
392 from the stack and printed. In non-interactive mode, an expression statement is
393 terminated with ``POP_STACK``.
394
395
Georg Brandl4833e5b2010-07-03 10:41:33 +0000396.. opcode:: BREAK_LOOP
Georg Brandl116aa622007-08-15 14:28:22 +0000397
398 Terminates a loop due to a :keyword:`break` statement.
399
400
401.. opcode:: CONTINUE_LOOP (target)
402
403 Continues a loop due to a :keyword:`continue` statement. *target* is the
404 address to jump to (which should be a ``FOR_ITER`` instruction).
405
406
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000407.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000408
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000409 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000410
411
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000412.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000413
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000414 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
415
416
417.. opcode:: MAP_ADD (i)
418
419 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
420 comprehensions.
421
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000422For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the
423added value or key/value pair is popped off, the container object remains on
424the stack so that it is available for further iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000425
426
Georg Brandl4833e5b2010-07-03 10:41:33 +0000427.. opcode:: RETURN_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000428
429 Returns with TOS to the caller of the function.
430
431
Georg Brandl4833e5b2010-07-03 10:41:33 +0000432.. opcode:: YIELD_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000433
Georg Brandl9afde1c2007-11-01 20:32:30 +0000434 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000435
436
Georg Brandl4833e5b2010-07-03 10:41:33 +0000437.. opcode:: IMPORT_STAR
Georg Brandl116aa622007-08-15 14:28:22 +0000438
439 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
440 local namespace. The module is popped after loading all names. This opcode
441 implements ``from module import *``.
442
443
Georg Brandl4833e5b2010-07-03 10:41:33 +0000444.. opcode:: POP_BLOCK
Georg Brandl116aa622007-08-15 14:28:22 +0000445
446 Removes one block from the block stack. Per frame, there is a stack of blocks,
447 denoting nested loops, try statements, and such.
448
449
Georg Brandl4833e5b2010-07-03 10:41:33 +0000450.. opcode:: POP_EXCEPT
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000451
452 Removes one block from the block stack. The popped block must be an exception
453 handler block, as implicitly created when entering an except handler.
454 In addition to popping extraneous values from the frame stack, the
455 last three popped values are used to restore the exception state.
456
457
Georg Brandl4833e5b2010-07-03 10:41:33 +0000458.. opcode:: END_FINALLY
Georg Brandl116aa622007-08-15 14:28:22 +0000459
460 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
461 exception has to be re-raised, or whether the function returns, and continues
462 with the outer-next block.
463
464
Georg Brandl4833e5b2010-07-03 10:41:33 +0000465.. opcode:: LOAD_BUILD_CLASS
Georg Brandl116aa622007-08-15 14:28:22 +0000466
Georg Brandl5ac22302008-07-20 21:39:03 +0000467 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Benjamin Petersonaac8fd32008-07-20 22:02:26 +0000468 by ``CALL_FUNCTION`` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000469
Guido van Rossum04110fb2007-08-24 16:32:05 +0000470
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000471.. opcode:: SETUP_WITH (delta)
472
473 This opcode performs several operations before a with block starts. First,
474 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
475 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
476 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
477 is pushed. Finally, the result of calling the enter method is pushed onto
478 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
479 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
480 :opcode:`UNPACK_SEQUENCE`).
481
482
Georg Brandl4833e5b2010-07-03 10:41:33 +0000483.. opcode:: WITH_CLEANUP
Guido van Rossum04110fb2007-08-24 16:32:05 +0000484
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000485 Cleans up the stack when a :keyword:`with` statement block exits. TOS is
486 the context manager's :meth:`__exit__` bound method. Below TOS are 1--3
487 values indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000488
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000489 * SECOND = ``None``
490 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
491 * SECOND = ``WHY_*``; no retval below it
492 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000493
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000494 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
495 ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000496
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000497 If the stack represents an exception, *and* the function call returns
498 a 'true' value, this information is "zapped" and replaced with a single
499 ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception.
500 (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000501
Georg Brandl9afde1c2007-11-01 20:32:30 +0000502 .. XXX explain the WHY stuff!
503
Guido van Rossum04110fb2007-08-24 16:32:05 +0000504
Georg Brandl5ac22302008-07-20 21:39:03 +0000505.. opcode:: STORE_LOCALS
506
507 Pops TOS from the stack and stores it as the current frame's ``f_locals``.
508 This is used in class construction.
509
510
Georg Brandl116aa622007-08-15 14:28:22 +0000511All of the following opcodes expect arguments. An argument is two bytes, with
512the more significant byte last.
513
Georg Brandl116aa622007-08-15 14:28:22 +0000514.. opcode:: STORE_NAME (namei)
515
516 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Christian Heimes8640e742008-02-23 16:23:06 +0000517 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl116aa622007-08-15 14:28:22 +0000518 or ``STORE_GLOBAL`` if possible.
519
520
521.. opcode:: DELETE_NAME (namei)
522
523 Implements ``del name``, where *namei* is the index into :attr:`co_names`
524 attribute of the code object.
525
526
527.. opcode:: UNPACK_SEQUENCE (count)
528
529 Unpacks TOS into *count* individual values, which are put onto the stack
530 right-to-left.
531
Georg Brandl116aa622007-08-15 14:28:22 +0000532
Georg Brandl5ac22302008-07-20 21:39:03 +0000533.. opcode:: UNPACK_EX (counts)
534
535 Implements assignment with a starred target: Unpacks an iterable in TOS into
536 individual values, where the total number of values can be smaller than the
537 number of items in the iterable: one the new values will be a list of all
538 leftover items.
539
540 The low byte of *counts* is the number of values before the list value, the
541 high byte of *counts* the number of values after it. The resulting values
542 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000543
Georg Brandl5ac22302008-07-20 21:39:03 +0000544
Georg Brandl116aa622007-08-15 14:28:22 +0000545.. opcode:: STORE_ATTR (namei)
546
547 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
548 :attr:`co_names`.
549
550
551.. opcode:: DELETE_ATTR (namei)
552
553 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
554
555
556.. opcode:: STORE_GLOBAL (namei)
557
558 Works as ``STORE_NAME``, but stores the name as a global.
559
560
561.. opcode:: DELETE_GLOBAL (namei)
562
563 Works as ``DELETE_NAME``, but deletes a global name.
564
Georg Brandl116aa622007-08-15 14:28:22 +0000565
566.. opcode:: LOAD_CONST (consti)
567
568 Pushes ``co_consts[consti]`` onto the stack.
569
570
571.. opcode:: LOAD_NAME (namei)
572
573 Pushes the value associated with ``co_names[namei]`` onto the stack.
574
575
576.. opcode:: BUILD_TUPLE (count)
577
578 Creates a tuple consuming *count* items from the stack, and pushes the resulting
579 tuple onto the stack.
580
581
582.. opcode:: BUILD_LIST (count)
583
584 Works as ``BUILD_TUPLE``, but creates a list.
585
586
587.. opcode:: BUILD_SET (count)
588
589 Works as ``BUILD_TUPLE``, but creates a set.
590
591
Christian Heimesa62da1d2008-01-12 19:39:10 +0000592.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000593
Christian Heimesa62da1d2008-01-12 19:39:10 +0000594 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
595 to hold *count* entries.
Georg Brandl116aa622007-08-15 14:28:22 +0000596
597
598.. opcode:: LOAD_ATTR (namei)
599
600 Replaces TOS with ``getattr(TOS, co_names[namei])``.
601
602
603.. opcode:: COMPARE_OP (opname)
604
605 Performs a Boolean operation. The operation name can be found in
606 ``cmp_op[opname]``.
607
608
609.. opcode:: IMPORT_NAME (namei)
610
Christian Heimesa342c012008-04-20 21:01:16 +0000611 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
612 the *fromlist* and *level* arguments of :func:`__import__`. The module
613 object is pushed onto the stack. The current namespace is not affected:
614 for a proper import statement, a subsequent ``STORE_FAST`` instruction
615 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000616
617
618.. opcode:: IMPORT_FROM (namei)
619
620 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
621 resulting object is pushed onto the stack, to be subsequently stored by a
622 ``STORE_FAST`` instruction.
623
624
625.. opcode:: JUMP_FORWARD (delta)
626
Georg Brandl9afde1c2007-11-01 20:32:30 +0000627 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000628
629
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000630.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000631
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000632 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000633
634
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000635.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000636
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000637 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
638
639
640.. opcode:: JUMP_IF_TRUE_OR_POP (target)
641
642 If TOS is true, sets the bytecode counter to *target* and leaves TOS
643 on the stack. Otherwise (TOS is false), TOS is popped.
644
645
646.. opcode:: JUMP_IF_FALSE_OR_POP (target)
647
648 If TOS is false, sets the bytecode counter to *target* and leaves
649 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000650
651
652.. opcode:: JUMP_ABSOLUTE (target)
653
Georg Brandl9afde1c2007-11-01 20:32:30 +0000654 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000655
656
657.. opcode:: FOR_ITER (delta)
658
Georg Brandl9afde1c2007-11-01 20:32:30 +0000659 ``TOS`` is an :term:`iterator`. Call its :meth:`__next__` method. If this
660 yields a new value, push it on the stack (leaving the iterator below it). If
661 the iterator indicates it is exhausted ``TOS`` is popped, and the byte code
662 counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000663
Georg Brandl116aa622007-08-15 14:28:22 +0000664
665.. opcode:: LOAD_GLOBAL (namei)
666
667 Loads the global named ``co_names[namei]`` onto the stack.
668
Georg Brandl116aa622007-08-15 14:28:22 +0000669
670.. opcode:: SETUP_LOOP (delta)
671
672 Pushes a block for a loop onto the block stack. The block spans from the
673 current instruction with a size of *delta* bytes.
674
675
676.. opcode:: SETUP_EXCEPT (delta)
677
678 Pushes a try block from a try-except clause onto the block stack. *delta* points
679 to the first except block.
680
681
682.. opcode:: SETUP_FINALLY (delta)
683
684 Pushes a try block from a try-except clause onto the block stack. *delta* points
685 to the finally block.
686
Georg Brandl4833e5b2010-07-03 10:41:33 +0000687.. opcode:: STORE_MAP
Christian Heimesa62da1d2008-01-12 19:39:10 +0000688
689 Store a key and value pair in a dictionary. Pops the key and value while leaving
690 the dictionary on the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000691
692.. opcode:: LOAD_FAST (var_num)
693
694 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
695
696
697.. opcode:: STORE_FAST (var_num)
698
699 Stores TOS into the local ``co_varnames[var_num]``.
700
701
702.. opcode:: DELETE_FAST (var_num)
703
704 Deletes local ``co_varnames[var_num]``.
705
706
707.. opcode:: LOAD_CLOSURE (i)
708
709 Pushes a reference to the cell contained in slot *i* of the cell and free
710 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
711 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
712 len(co_cellvars)]``.
713
714
715.. opcode:: LOAD_DEREF (i)
716
717 Loads the cell contained in slot *i* of the cell and free variable storage.
718 Pushes a reference to the object the cell contains on the stack.
719
720
721.. opcode:: STORE_DEREF (i)
722
723 Stores TOS into the cell contained in slot *i* of the cell and free variable
724 storage.
725
726
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000727.. opcode:: DELETE_DEREF (i)
728
729 Empties the cell contained in slot *i* of the cell and free variable storage.
730 Used by the :keyword:`del` statement.
731
732
Georg Brandl116aa622007-08-15 14:28:22 +0000733.. opcode:: RAISE_VARARGS (argc)
734
735 Raises an exception. *argc* indicates the number of parameters to the raise
736 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
737 the parameter as TOS1, and the exception as TOS.
738
739
740.. opcode:: CALL_FUNCTION (argc)
741
742 Calls a function. The low byte of *argc* indicates the number of positional
743 parameters, the high byte the number of keyword parameters. On the stack, the
744 opcode finds the keyword parameters first. For each keyword argument, the value
745 is on top of the key. Below the keyword parameters, the positional parameters
746 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl48310cd2009-01-03 21:18:54 +0000747 the function object to call is on the stack. Pops all function arguments, and
Benjamin Peterson206e3072008-10-19 14:07:49 +0000748 the function itself off the stack, and pushes the return value.
Georg Brandl116aa622007-08-15 14:28:22 +0000749
750
751.. opcode:: MAKE_FUNCTION (argc)
752
753 Pushes a new function object on the stack. TOS is the code associated with the
754 function. The function object is defined to have *argc* default parameters,
755 which are found below TOS.
756
757
758.. opcode:: MAKE_CLOSURE (argc)
759
Guido van Rossum04110fb2007-08-24 16:32:05 +0000760 Creates a new function object, sets its *__closure__* slot, and pushes it on
761 the stack. TOS is the code associated with the function, TOS1 the tuple
762 containing cells for the closure's free variables. The function also has
763 *argc* default parameters, which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000764
765
766.. opcode:: BUILD_SLICE (argc)
767
768 .. index:: builtin: slice
769
770 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
771 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000772 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000773
774
775.. opcode:: EXTENDED_ARG (ext)
776
777 Prefixes any opcode which has an argument too big to fit into the default two
778 bytes. *ext* holds two additional bytes which, taken together with the
779 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
780 most-significant bytes.
781
782
783.. opcode:: CALL_FUNCTION_VAR (argc)
784
785 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
786 on the stack contains the variable argument list, followed by keyword and
787 positional arguments.
788
789
790.. opcode:: CALL_FUNCTION_KW (argc)
791
792 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
793 on the stack contains the keyword arguments dictionary, followed by explicit
794 keyword and positional arguments.
795
796
797.. opcode:: CALL_FUNCTION_VAR_KW (argc)
798
799 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
800 element on the stack contains the keyword arguments dictionary, followed by the
801 variable-arguments tuple, followed by explicit keyword and positional arguments.
802
803
Georg Brandl4833e5b2010-07-03 10:41:33 +0000804.. opcode:: HAVE_ARGUMENT
Georg Brandl116aa622007-08-15 14:28:22 +0000805
806 This is not really an opcode. It identifies the dividing line between opcodes
807 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
808 HAVE_ARGUMENT``.
809