blob: 10cf55599a8a5cfd2a2b1d86e313479d2241e43b [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
Éric Araujo6e6cb8e2010-11-16 19:13:50 +000013.. seealso::
14
15 Latest version of the :source:`dis module Python source code <Lib/dis.py>`
16
Georg Brandl19b7a872010-07-03 10:21:50 +000017.. impl-detail::
18
19 Bytecode is an implementation detail of the CPython interpreter! No
20 guarantees are made that bytecode will not be added, removed, or changed
21 between versions of Python. Use of this module should not be considered to
22 work across Python VMs or Python releases.
23
Brett Cannon8315fd12010-07-02 22:03:00 +000024
Georg Brandl116aa622007-08-15 14:28:22 +000025Example: Given the function :func:`myfunc`::
26
27 def myfunc(alist):
28 return len(alist)
29
30the following command can be used to get the disassembly of :func:`myfunc`::
31
32 >>> dis.dis(myfunc)
33 2 0 LOAD_GLOBAL 0 (len)
34 3 LOAD_FAST 0 (alist)
35 6 CALL_FUNCTION 1
36 9 RETURN_VALUE
37
38(The "2" is a line number).
39
40The :mod:`dis` module defines the following functions and constants:
41
42
Nick Coghlane8814fb2010-09-10 14:08:04 +000043.. function:: code_info(x)
Nick Coghlaneae2da12010-08-17 08:03:36 +000044
Georg Brandl67b21b72010-08-17 15:07:14 +000045 Return a formatted multi-line string with detailed code object information
46 for the supplied function, method, source code string or code object.
Nick Coghlaneae2da12010-08-17 08:03:36 +000047
Georg Brandl67b21b72010-08-17 15:07:14 +000048 Note that the exact contents of code info strings are highly implementation
49 dependent and they may change arbitrarily across Python VMs or Python
50 releases.
Nick Coghlaneae2da12010-08-17 08:03:36 +000051
52 .. versionadded:: 3.2
53
Georg Brandl67b21b72010-08-17 15:07:14 +000054
Nick Coghlane8814fb2010-09-10 14:08:04 +000055.. function:: show_code(x)
56
57 Print detailed code object information for the supplied function, method,
58 source code string or code object to stdout.
59
60 This is a convenient shorthand for ``print(code_info(x))``, intended for
61 interactive exploration at the interpreter prompt.
62
63 .. versionadded:: 3.2
64
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000065.. function:: dis(x=None)
Georg Brandl116aa622007-08-15 14:28:22 +000066
Georg Brandl67b21b72010-08-17 15:07:14 +000067 Disassemble the *x* object. *x* can denote either a module, a class, a
68 method, a function, a code object, a string of source code or a byte sequence
69 of raw bytecode. For a module, it disassembles all functions. For a class,
70 it disassembles all methods. For a code object or sequence of raw bytecode,
71 it prints one line per bytecode instruction. Strings are first compiled to
72 code objects with the :func:`compile` built-in function before being
73 disassembled. If no object is provided, this function disassembles the last
74 traceback.
Georg Brandl116aa622007-08-15 14:28:22 +000075
76
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000077.. function:: distb(tb=None)
Georg Brandl116aa622007-08-15 14:28:22 +000078
Georg Brandl4833e5b2010-07-03 10:41:33 +000079 Disassemble the top-of-stack function of a traceback, using the last
80 traceback if none was passed. The instruction causing the exception is
81 indicated.
Georg Brandl116aa622007-08-15 14:28:22 +000082
83
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000084.. function:: disassemble(code, lasti=-1)
85 disco(code, lasti=-1)
Georg Brandl116aa622007-08-15 14:28:22 +000086
Georg Brandl4833e5b2010-07-03 10:41:33 +000087 Disassemble a code object, indicating the last instruction if *lasti* was
Georg Brandl116aa622007-08-15 14:28:22 +000088 provided. The output is divided in the following columns:
89
90 #. the line number, for the first instruction of each line
91 #. the current instruction, indicated as ``-->``,
92 #. a labelled instruction, indicated with ``>>``,
93 #. the address of the instruction,
94 #. the operation code name,
95 #. operation parameters, and
96 #. interpretation of the parameters in parentheses.
97
98 The parameter interpretation recognizes local and global variable names,
99 constant values, branch targets, and compare operators.
100
101
Benjamin Peterson75edad02009-01-01 15:05:06 +0000102.. function:: findlinestarts(code)
103
104 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
105 attributes of the code object *code* to find the offsets which are starts of
106 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
107
108
109.. function:: findlabels(code)
110
111 Detect all offsets in the code object *code* which are jump targets, and
112 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +0000113
114
Georg Brandl116aa622007-08-15 14:28:22 +0000115.. data:: opname
116
Georg Brandl9afde1c2007-11-01 20:32:30 +0000117 Sequence of operation names, indexable using the bytecode.
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119
120.. data:: opmap
121
Georg Brandl23798772010-10-17 11:29:07 +0000122 Dictionary mapping operation names to bytecodes.
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124
125.. data:: cmp_op
126
127 Sequence of all compare operation names.
128
129
130.. data:: hasconst
131
Georg Brandl9afde1c2007-11-01 20:32:30 +0000132 Sequence of bytecodes that have a constant parameter.
Georg Brandl116aa622007-08-15 14:28:22 +0000133
134
135.. data:: hasfree
136
Georg Brandl9afde1c2007-11-01 20:32:30 +0000137 Sequence of bytecodes that access a free variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000138
139
140.. data:: hasname
141
Georg Brandl9afde1c2007-11-01 20:32:30 +0000142 Sequence of bytecodes that access an attribute by name.
Georg Brandl116aa622007-08-15 14:28:22 +0000143
144
145.. data:: hasjrel
146
Georg Brandl9afde1c2007-11-01 20:32:30 +0000147 Sequence of bytecodes that have a relative jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000148
149
150.. data:: hasjabs
151
Georg Brandl9afde1c2007-11-01 20:32:30 +0000152 Sequence of bytecodes that have an absolute jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154
155.. data:: haslocal
156
Georg Brandl9afde1c2007-11-01 20:32:30 +0000157 Sequence of bytecodes that access a local variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000158
159
160.. data:: hascompare
161
Georg Brandl9afde1c2007-11-01 20:32:30 +0000162 Sequence of bytecodes of Boolean operations.
Georg Brandl116aa622007-08-15 14:28:22 +0000163
164
165.. _bytecodes:
166
Georg Brandl9afde1c2007-11-01 20:32:30 +0000167Python Bytecode Instructions
168----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000169
Georg Brandl9afde1c2007-11-01 20:32:30 +0000170The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000171
172
Georg Brandl4833e5b2010-07-03 10:41:33 +0000173**General instructions**
174
175.. opcode:: STOP_CODE
Georg Brandl116aa622007-08-15 14:28:22 +0000176
177 Indicates end-of-code to the compiler, not used by the interpreter.
178
179
Georg Brandl4833e5b2010-07-03 10:41:33 +0000180.. opcode:: NOP
Georg Brandl116aa622007-08-15 14:28:22 +0000181
182 Do nothing code. Used as a placeholder by the bytecode optimizer.
183
184
Georg Brandl4833e5b2010-07-03 10:41:33 +0000185.. opcode:: POP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000186
187 Removes the top-of-stack (TOS) item.
188
189
Georg Brandl4833e5b2010-07-03 10:41:33 +0000190.. opcode:: ROT_TWO
Georg Brandl116aa622007-08-15 14:28:22 +0000191
192 Swaps the two top-most stack items.
193
194
Georg Brandl4833e5b2010-07-03 10:41:33 +0000195.. opcode:: ROT_THREE
Georg Brandl116aa622007-08-15 14:28:22 +0000196
197 Lifts second and third stack item one position up, moves top down to position
198 three.
199
200
Georg Brandl4833e5b2010-07-03 10:41:33 +0000201.. opcode:: DUP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000202
203 Duplicates the reference on top of the stack.
204
Georg Brandl4833e5b2010-07-03 10:41:33 +0000205
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000206.. opcode:: DUP_TOP_TWO
207
208 Duplicates the two references on top of the stack, leaving them in the
209 same order.
210
211
Georg Brandl4833e5b2010-07-03 10:41:33 +0000212**Unary operations**
213
214Unary operations take the top of the stack, apply the operation, and push the
Georg Brandl116aa622007-08-15 14:28:22 +0000215result back on the stack.
216
Georg Brandl4833e5b2010-07-03 10:41:33 +0000217.. opcode:: UNARY_POSITIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000218
219 Implements ``TOS = +TOS``.
220
221
Georg Brandl4833e5b2010-07-03 10:41:33 +0000222.. opcode:: UNARY_NEGATIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000223
224 Implements ``TOS = -TOS``.
225
226
Georg Brandl4833e5b2010-07-03 10:41:33 +0000227.. opcode:: UNARY_NOT
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229 Implements ``TOS = not TOS``.
230
231
Georg Brandl4833e5b2010-07-03 10:41:33 +0000232.. opcode:: UNARY_INVERT
Georg Brandl116aa622007-08-15 14:28:22 +0000233
234 Implements ``TOS = ~TOS``.
235
236
Georg Brandl4833e5b2010-07-03 10:41:33 +0000237.. opcode:: GET_ITER
Georg Brandl116aa622007-08-15 14:28:22 +0000238
239 Implements ``TOS = iter(TOS)``.
240
Georg Brandl4833e5b2010-07-03 10:41:33 +0000241
242**Binary operations**
243
Georg Brandl116aa622007-08-15 14:28:22 +0000244Binary operations remove the top of the stack (TOS) and the second top-most
245stack item (TOS1) from the stack. They perform the operation, and put the
246result back on the stack.
247
Georg Brandl4833e5b2010-07-03 10:41:33 +0000248.. opcode:: BINARY_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000249
250 Implements ``TOS = TOS1 ** TOS``.
251
252
Georg Brandl4833e5b2010-07-03 10:41:33 +0000253.. opcode:: BINARY_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000254
255 Implements ``TOS = TOS1 * TOS``.
256
257
Georg Brandl4833e5b2010-07-03 10:41:33 +0000258.. opcode:: BINARY_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000259
260 Implements ``TOS = TOS1 // TOS``.
261
262
Georg Brandl4833e5b2010-07-03 10:41:33 +0000263.. opcode:: BINARY_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000264
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000265 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000266
267
Georg Brandl4833e5b2010-07-03 10:41:33 +0000268.. opcode:: BINARY_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000269
270 Implements ``TOS = TOS1 % TOS``.
271
272
Georg Brandl4833e5b2010-07-03 10:41:33 +0000273.. opcode:: BINARY_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000274
275 Implements ``TOS = TOS1 + TOS``.
276
277
Georg Brandl4833e5b2010-07-03 10:41:33 +0000278.. opcode:: BINARY_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000279
280 Implements ``TOS = TOS1 - TOS``.
281
282
Georg Brandl4833e5b2010-07-03 10:41:33 +0000283.. opcode:: BINARY_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000284
285 Implements ``TOS = TOS1[TOS]``.
286
287
Georg Brandl4833e5b2010-07-03 10:41:33 +0000288.. opcode:: BINARY_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000289
290 Implements ``TOS = TOS1 << TOS``.
291
292
Georg Brandl4833e5b2010-07-03 10:41:33 +0000293.. opcode:: BINARY_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000294
295 Implements ``TOS = TOS1 >> TOS``.
296
297
Georg Brandl4833e5b2010-07-03 10:41:33 +0000298.. opcode:: BINARY_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000299
300 Implements ``TOS = TOS1 & TOS``.
301
302
Georg Brandl4833e5b2010-07-03 10:41:33 +0000303.. opcode:: BINARY_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000304
305 Implements ``TOS = TOS1 ^ TOS``.
306
307
Georg Brandl4833e5b2010-07-03 10:41:33 +0000308.. opcode:: BINARY_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000309
310 Implements ``TOS = TOS1 | TOS``.
311
Georg Brandl4833e5b2010-07-03 10:41:33 +0000312
313**In-place operations**
314
Georg Brandl116aa622007-08-15 14:28:22 +0000315In-place operations are like binary operations, in that they remove TOS and
316TOS1, and push the result back on the stack, but the operation is done in-place
317when TOS1 supports it, and the resulting TOS may be (but does not have to be)
318the original TOS1.
319
Georg Brandl4833e5b2010-07-03 10:41:33 +0000320.. opcode:: INPLACE_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000321
322 Implements in-place ``TOS = TOS1 ** TOS``.
323
324
Georg Brandl4833e5b2010-07-03 10:41:33 +0000325.. opcode:: INPLACE_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000326
327 Implements in-place ``TOS = TOS1 * TOS``.
328
329
Georg Brandl4833e5b2010-07-03 10:41:33 +0000330.. opcode:: INPLACE_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000331
332 Implements in-place ``TOS = TOS1 // TOS``.
333
334
Georg Brandl4833e5b2010-07-03 10:41:33 +0000335.. opcode:: INPLACE_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000336
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000337 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000338
339
Georg Brandl4833e5b2010-07-03 10:41:33 +0000340.. opcode:: INPLACE_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000341
342 Implements in-place ``TOS = TOS1 % TOS``.
343
344
Georg Brandl4833e5b2010-07-03 10:41:33 +0000345.. opcode:: INPLACE_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000346
347 Implements in-place ``TOS = TOS1 + TOS``.
348
349
Georg Brandl4833e5b2010-07-03 10:41:33 +0000350.. opcode:: INPLACE_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000351
352 Implements in-place ``TOS = TOS1 - TOS``.
353
354
Georg Brandl4833e5b2010-07-03 10:41:33 +0000355.. opcode:: INPLACE_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000356
357 Implements in-place ``TOS = TOS1 << TOS``.
358
359
Georg Brandl4833e5b2010-07-03 10:41:33 +0000360.. opcode:: INPLACE_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000361
362 Implements in-place ``TOS = TOS1 >> TOS``.
363
364
Georg Brandl4833e5b2010-07-03 10:41:33 +0000365.. opcode:: INPLACE_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000366
367 Implements in-place ``TOS = TOS1 & TOS``.
368
369
Georg Brandl4833e5b2010-07-03 10:41:33 +0000370.. opcode:: INPLACE_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000371
372 Implements in-place ``TOS = TOS1 ^ TOS``.
373
374
Georg Brandl4833e5b2010-07-03 10:41:33 +0000375.. opcode:: INPLACE_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000376
377 Implements in-place ``TOS = TOS1 | TOS``.
378
Georg Brandl116aa622007-08-15 14:28:22 +0000379
Georg Brandl4833e5b2010-07-03 10:41:33 +0000380.. opcode:: STORE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000381
382 Implements ``TOS1[TOS] = TOS2``.
383
384
Georg Brandl4833e5b2010-07-03 10:41:33 +0000385.. opcode:: DELETE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000386
387 Implements ``del TOS1[TOS]``.
388
Georg Brandl116aa622007-08-15 14:28:22 +0000389
Georg Brandl4833e5b2010-07-03 10:41:33 +0000390**Miscellaneous opcodes**
Georg Brandl116aa622007-08-15 14:28:22 +0000391
Georg Brandl4833e5b2010-07-03 10:41:33 +0000392.. opcode:: PRINT_EXPR
Georg Brandl116aa622007-08-15 14:28:22 +0000393
394 Implements the expression statement for the interactive mode. TOS is removed
395 from the stack and printed. In non-interactive mode, an expression statement is
396 terminated with ``POP_STACK``.
397
398
Georg Brandl4833e5b2010-07-03 10:41:33 +0000399.. opcode:: BREAK_LOOP
Georg Brandl116aa622007-08-15 14:28:22 +0000400
401 Terminates a loop due to a :keyword:`break` statement.
402
403
404.. opcode:: CONTINUE_LOOP (target)
405
406 Continues a loop due to a :keyword:`continue` statement. *target* is the
407 address to jump to (which should be a ``FOR_ITER`` instruction).
408
409
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000410.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000411
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000412 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000413
414
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000415.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000416
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000417 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
418
419
420.. opcode:: MAP_ADD (i)
421
422 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
423 comprehensions.
424
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000425For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the
426added value or key/value pair is popped off, the container object remains on
427the stack so that it is available for further iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000428
429
Georg Brandl4833e5b2010-07-03 10:41:33 +0000430.. opcode:: RETURN_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000431
432 Returns with TOS to the caller of the function.
433
434
Georg Brandl4833e5b2010-07-03 10:41:33 +0000435.. opcode:: YIELD_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000436
Georg Brandl9afde1c2007-11-01 20:32:30 +0000437 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000438
439
Georg Brandl4833e5b2010-07-03 10:41:33 +0000440.. opcode:: IMPORT_STAR
Georg Brandl116aa622007-08-15 14:28:22 +0000441
442 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
443 local namespace. The module is popped after loading all names. This opcode
444 implements ``from module import *``.
445
446
Georg Brandl4833e5b2010-07-03 10:41:33 +0000447.. opcode:: POP_BLOCK
Georg Brandl116aa622007-08-15 14:28:22 +0000448
449 Removes one block from the block stack. Per frame, there is a stack of blocks,
450 denoting nested loops, try statements, and such.
451
452
Georg Brandl4833e5b2010-07-03 10:41:33 +0000453.. opcode:: POP_EXCEPT
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000454
455 Removes one block from the block stack. The popped block must be an exception
456 handler block, as implicitly created when entering an except handler.
457 In addition to popping extraneous values from the frame stack, the
458 last three popped values are used to restore the exception state.
459
460
Georg Brandl4833e5b2010-07-03 10:41:33 +0000461.. opcode:: END_FINALLY
Georg Brandl116aa622007-08-15 14:28:22 +0000462
463 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
464 exception has to be re-raised, or whether the function returns, and continues
465 with the outer-next block.
466
467
Georg Brandl4833e5b2010-07-03 10:41:33 +0000468.. opcode:: LOAD_BUILD_CLASS
Georg Brandl116aa622007-08-15 14:28:22 +0000469
Georg Brandl5ac22302008-07-20 21:39:03 +0000470 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Benjamin Petersonaac8fd32008-07-20 22:02:26 +0000471 by ``CALL_FUNCTION`` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000472
Guido van Rossum04110fb2007-08-24 16:32:05 +0000473
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000474.. opcode:: SETUP_WITH (delta)
475
476 This opcode performs several operations before a with block starts. First,
477 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
478 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
479 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
480 is pushed. Finally, the result of calling the enter method is pushed onto
481 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
482 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
483 :opcode:`UNPACK_SEQUENCE`).
484
485
Georg Brandl4833e5b2010-07-03 10:41:33 +0000486.. opcode:: WITH_CLEANUP
Guido van Rossum04110fb2007-08-24 16:32:05 +0000487
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000488 Cleans up the stack when a :keyword:`with` statement block exits. TOS is
489 the context manager's :meth:`__exit__` bound method. Below TOS are 1--3
490 values indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000491
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000492 * SECOND = ``None``
493 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
494 * SECOND = ``WHY_*``; no retval below it
495 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000496
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000497 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
498 ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000499
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000500 If the stack represents an exception, *and* the function call returns
501 a 'true' value, this information is "zapped" and replaced with a single
502 ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception.
503 (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000504
Georg Brandl9afde1c2007-11-01 20:32:30 +0000505 .. XXX explain the WHY stuff!
506
Guido van Rossum04110fb2007-08-24 16:32:05 +0000507
Georg Brandl5ac22302008-07-20 21:39:03 +0000508.. opcode:: STORE_LOCALS
509
510 Pops TOS from the stack and stores it as the current frame's ``f_locals``.
511 This is used in class construction.
512
513
Georg Brandl116aa622007-08-15 14:28:22 +0000514All of the following opcodes expect arguments. An argument is two bytes, with
515the more significant byte last.
516
Georg Brandl116aa622007-08-15 14:28:22 +0000517.. opcode:: STORE_NAME (namei)
518
519 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Christian Heimes8640e742008-02-23 16:23:06 +0000520 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl116aa622007-08-15 14:28:22 +0000521 or ``STORE_GLOBAL`` if possible.
522
523
524.. opcode:: DELETE_NAME (namei)
525
526 Implements ``del name``, where *namei* is the index into :attr:`co_names`
527 attribute of the code object.
528
529
530.. opcode:: UNPACK_SEQUENCE (count)
531
532 Unpacks TOS into *count* individual values, which are put onto the stack
533 right-to-left.
534
Georg Brandl116aa622007-08-15 14:28:22 +0000535
Georg Brandl5ac22302008-07-20 21:39:03 +0000536.. opcode:: UNPACK_EX (counts)
537
538 Implements assignment with a starred target: Unpacks an iterable in TOS into
539 individual values, where the total number of values can be smaller than the
540 number of items in the iterable: one the new values will be a list of all
541 leftover items.
542
543 The low byte of *counts* is the number of values before the list value, the
544 high byte of *counts* the number of values after it. The resulting values
545 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000546
Georg Brandl5ac22302008-07-20 21:39:03 +0000547
Georg Brandl116aa622007-08-15 14:28:22 +0000548.. opcode:: STORE_ATTR (namei)
549
550 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
551 :attr:`co_names`.
552
553
554.. opcode:: DELETE_ATTR (namei)
555
556 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
557
558
559.. opcode:: STORE_GLOBAL (namei)
560
561 Works as ``STORE_NAME``, but stores the name as a global.
562
563
564.. opcode:: DELETE_GLOBAL (namei)
565
566 Works as ``DELETE_NAME``, but deletes a global name.
567
Georg Brandl116aa622007-08-15 14:28:22 +0000568
569.. opcode:: LOAD_CONST (consti)
570
571 Pushes ``co_consts[consti]`` onto the stack.
572
573
574.. opcode:: LOAD_NAME (namei)
575
576 Pushes the value associated with ``co_names[namei]`` onto the stack.
577
578
579.. opcode:: BUILD_TUPLE (count)
580
581 Creates a tuple consuming *count* items from the stack, and pushes the resulting
582 tuple onto the stack.
583
584
585.. opcode:: BUILD_LIST (count)
586
587 Works as ``BUILD_TUPLE``, but creates a list.
588
589
590.. opcode:: BUILD_SET (count)
591
592 Works as ``BUILD_TUPLE``, but creates a set.
593
594
Christian Heimesa62da1d2008-01-12 19:39:10 +0000595.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000596
Christian Heimesa62da1d2008-01-12 19:39:10 +0000597 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
598 to hold *count* entries.
Georg Brandl116aa622007-08-15 14:28:22 +0000599
600
601.. opcode:: LOAD_ATTR (namei)
602
603 Replaces TOS with ``getattr(TOS, co_names[namei])``.
604
605
606.. opcode:: COMPARE_OP (opname)
607
608 Performs a Boolean operation. The operation name can be found in
609 ``cmp_op[opname]``.
610
611
612.. opcode:: IMPORT_NAME (namei)
613
Christian Heimesa342c012008-04-20 21:01:16 +0000614 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
615 the *fromlist* and *level* arguments of :func:`__import__`. The module
616 object is pushed onto the stack. The current namespace is not affected:
617 for a proper import statement, a subsequent ``STORE_FAST`` instruction
618 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000619
620
621.. opcode:: IMPORT_FROM (namei)
622
623 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
624 resulting object is pushed onto the stack, to be subsequently stored by a
625 ``STORE_FAST`` instruction.
626
627
628.. opcode:: JUMP_FORWARD (delta)
629
Georg Brandl9afde1c2007-11-01 20:32:30 +0000630 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000631
632
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000633.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000634
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000635 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000636
637
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000638.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000639
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000640 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
641
642
643.. opcode:: JUMP_IF_TRUE_OR_POP (target)
644
645 If TOS is true, sets the bytecode counter to *target* and leaves TOS
646 on the stack. Otherwise (TOS is false), TOS is popped.
647
648
649.. opcode:: JUMP_IF_FALSE_OR_POP (target)
650
651 If TOS is false, sets the bytecode counter to *target* and leaves
652 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000653
654
655.. opcode:: JUMP_ABSOLUTE (target)
656
Georg Brandl9afde1c2007-11-01 20:32:30 +0000657 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000658
659
660.. opcode:: FOR_ITER (delta)
661
Georg Brandl9afde1c2007-11-01 20:32:30 +0000662 ``TOS`` is an :term:`iterator`. Call its :meth:`__next__` method. If this
663 yields a new value, push it on the stack (leaving the iterator below it). If
664 the iterator indicates it is exhausted ``TOS`` is popped, and the byte code
665 counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000666
Georg Brandl116aa622007-08-15 14:28:22 +0000667
668.. opcode:: LOAD_GLOBAL (namei)
669
670 Loads the global named ``co_names[namei]`` onto the stack.
671
Georg Brandl116aa622007-08-15 14:28:22 +0000672
673.. opcode:: SETUP_LOOP (delta)
674
675 Pushes a block for a loop onto the block stack. The block spans from the
676 current instruction with a size of *delta* bytes.
677
678
679.. opcode:: SETUP_EXCEPT (delta)
680
681 Pushes a try block from a try-except clause onto the block stack. *delta* points
682 to the first except block.
683
684
685.. opcode:: SETUP_FINALLY (delta)
686
687 Pushes a try block from a try-except clause onto the block stack. *delta* points
688 to the finally block.
689
Georg Brandl4833e5b2010-07-03 10:41:33 +0000690.. opcode:: STORE_MAP
Christian Heimesa62da1d2008-01-12 19:39:10 +0000691
692 Store a key and value pair in a dictionary. Pops the key and value while leaving
693 the dictionary on the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000694
695.. opcode:: LOAD_FAST (var_num)
696
697 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
698
699
700.. opcode:: STORE_FAST (var_num)
701
702 Stores TOS into the local ``co_varnames[var_num]``.
703
704
705.. opcode:: DELETE_FAST (var_num)
706
707 Deletes local ``co_varnames[var_num]``.
708
709
710.. opcode:: LOAD_CLOSURE (i)
711
712 Pushes a reference to the cell contained in slot *i* of the cell and free
713 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
714 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
715 len(co_cellvars)]``.
716
717
718.. opcode:: LOAD_DEREF (i)
719
720 Loads the cell contained in slot *i* of the cell and free variable storage.
721 Pushes a reference to the object the cell contains on the stack.
722
723
724.. opcode:: STORE_DEREF (i)
725
726 Stores TOS into the cell contained in slot *i* of the cell and free variable
727 storage.
728
729
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000730.. opcode:: DELETE_DEREF (i)
731
732 Empties the cell contained in slot *i* of the cell and free variable storage.
733 Used by the :keyword:`del` statement.
734
735
Georg Brandl116aa622007-08-15 14:28:22 +0000736.. opcode:: RAISE_VARARGS (argc)
737
738 Raises an exception. *argc* indicates the number of parameters to the raise
739 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
740 the parameter as TOS1, and the exception as TOS.
741
742
743.. opcode:: CALL_FUNCTION (argc)
744
745 Calls a function. The low byte of *argc* indicates the number of positional
746 parameters, the high byte the number of keyword parameters. On the stack, the
747 opcode finds the keyword parameters first. For each keyword argument, the value
748 is on top of the key. Below the keyword parameters, the positional parameters
749 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl48310cd2009-01-03 21:18:54 +0000750 the function object to call is on the stack. Pops all function arguments, and
Benjamin Peterson206e3072008-10-19 14:07:49 +0000751 the function itself off the stack, and pushes the return value.
Georg Brandl116aa622007-08-15 14:28:22 +0000752
753
754.. opcode:: MAKE_FUNCTION (argc)
755
756 Pushes a new function object on the stack. TOS is the code associated with the
757 function. The function object is defined to have *argc* default parameters,
758 which are found below TOS.
759
760
761.. opcode:: MAKE_CLOSURE (argc)
762
Guido van Rossum04110fb2007-08-24 16:32:05 +0000763 Creates a new function object, sets its *__closure__* slot, and pushes it on
764 the stack. TOS is the code associated with the function, TOS1 the tuple
765 containing cells for the closure's free variables. The function also has
766 *argc* default parameters, which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000767
768
769.. opcode:: BUILD_SLICE (argc)
770
771 .. index:: builtin: slice
772
773 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
774 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000775 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000776
777
778.. opcode:: EXTENDED_ARG (ext)
779
780 Prefixes any opcode which has an argument too big to fit into the default two
781 bytes. *ext* holds two additional bytes which, taken together with the
782 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
783 most-significant bytes.
784
785
786.. opcode:: CALL_FUNCTION_VAR (argc)
787
788 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
789 on the stack contains the variable argument list, followed by keyword and
790 positional arguments.
791
792
793.. opcode:: CALL_FUNCTION_KW (argc)
794
795 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
796 on the stack contains the keyword arguments dictionary, followed by explicit
797 keyword and positional arguments.
798
799
800.. opcode:: CALL_FUNCTION_VAR_KW (argc)
801
802 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
803 element on the stack contains the keyword arguments dictionary, followed by the
804 variable-arguments tuple, followed by explicit keyword and positional arguments.
805
806
Georg Brandl4833e5b2010-07-03 10:41:33 +0000807.. opcode:: HAVE_ARGUMENT
Georg Brandl116aa622007-08-15 14:28:22 +0000808
809 This is not really an opcode. It identifies the dividing line between opcodes
810 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
811 HAVE_ARGUMENT``.
812