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