blob: 79cc583b75440c3ebe5aa0b9c171075ce6edc9ba [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
174.. opcode:: STOP_CODE
Georg Brandl116aa622007-08-15 14:28:22 +0000175
176 Indicates end-of-code to the compiler, not used by the interpreter.
177
178
Georg Brandl4833e5b2010-07-03 10:41:33 +0000179.. opcode:: NOP
Georg Brandl116aa622007-08-15 14:28:22 +0000180
181 Do nothing code. Used as a placeholder by the bytecode optimizer.
182
183
Georg Brandl4833e5b2010-07-03 10:41:33 +0000184.. opcode:: POP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000185
186 Removes the top-of-stack (TOS) item.
187
188
Georg Brandl4833e5b2010-07-03 10:41:33 +0000189.. opcode:: ROT_TWO
Georg Brandl116aa622007-08-15 14:28:22 +0000190
191 Swaps the two top-most stack items.
192
193
Georg Brandl4833e5b2010-07-03 10:41:33 +0000194.. opcode:: ROT_THREE
Georg Brandl116aa622007-08-15 14:28:22 +0000195
196 Lifts second and third stack item one position up, moves top down to position
197 three.
198
199
Georg Brandl4833e5b2010-07-03 10:41:33 +0000200.. opcode:: DUP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000201
202 Duplicates the reference on top of the stack.
203
Georg Brandl4833e5b2010-07-03 10:41:33 +0000204
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000205.. opcode:: DUP_TOP_TWO
206
207 Duplicates the two references on top of the stack, leaving them in the
208 same order.
209
210
Georg Brandl4833e5b2010-07-03 10:41:33 +0000211**Unary operations**
212
213Unary operations take the top of the stack, apply the operation, and push the
Georg Brandl116aa622007-08-15 14:28:22 +0000214result back on the stack.
215
Georg Brandl4833e5b2010-07-03 10:41:33 +0000216.. opcode:: UNARY_POSITIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000217
218 Implements ``TOS = +TOS``.
219
220
Georg Brandl4833e5b2010-07-03 10:41:33 +0000221.. opcode:: UNARY_NEGATIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000222
223 Implements ``TOS = -TOS``.
224
225
Georg Brandl4833e5b2010-07-03 10:41:33 +0000226.. opcode:: UNARY_NOT
Georg Brandl116aa622007-08-15 14:28:22 +0000227
228 Implements ``TOS = not TOS``.
229
230
Georg Brandl4833e5b2010-07-03 10:41:33 +0000231.. opcode:: UNARY_INVERT
Georg Brandl116aa622007-08-15 14:28:22 +0000232
233 Implements ``TOS = ~TOS``.
234
235
Georg Brandl4833e5b2010-07-03 10:41:33 +0000236.. opcode:: GET_ITER
Georg Brandl116aa622007-08-15 14:28:22 +0000237
238 Implements ``TOS = iter(TOS)``.
239
Georg Brandl4833e5b2010-07-03 10:41:33 +0000240
241**Binary operations**
242
Georg Brandl116aa622007-08-15 14:28:22 +0000243Binary operations remove the top of the stack (TOS) and the second top-most
244stack item (TOS1) from the stack. They perform the operation, and put the
245result back on the stack.
246
Georg Brandl4833e5b2010-07-03 10:41:33 +0000247.. opcode:: BINARY_POWER
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_MULTIPLY
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_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000258
259 Implements ``TOS = TOS1 // TOS``.
260
261
Georg Brandl4833e5b2010-07-03 10:41:33 +0000262.. opcode:: BINARY_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000263
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000264 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000265
266
Georg Brandl4833e5b2010-07-03 10:41:33 +0000267.. opcode:: BINARY_MODULO
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_ADD
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_SUBTRACT
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_SUBSCR
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_LSHIFT
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_RSHIFT
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_AND
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_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000303
304 Implements ``TOS = TOS1 ^ TOS``.
305
306
Georg Brandl4833e5b2010-07-03 10:41:33 +0000307.. opcode:: BINARY_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000308
309 Implements ``TOS = TOS1 | TOS``.
310
Georg Brandl4833e5b2010-07-03 10:41:33 +0000311
312**In-place operations**
313
Georg Brandl116aa622007-08-15 14:28:22 +0000314In-place operations are like binary operations, in that they remove TOS and
315TOS1, and push the result back on the stack, but the operation is done in-place
316when TOS1 supports it, and the resulting TOS may be (but does not have to be)
317the original TOS1.
318
Georg Brandl4833e5b2010-07-03 10:41:33 +0000319.. opcode:: INPLACE_POWER
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_MULTIPLY
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_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000330
331 Implements in-place ``TOS = TOS1 // TOS``.
332
333
Georg Brandl4833e5b2010-07-03 10:41:33 +0000334.. opcode:: INPLACE_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000335
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000336 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000337
338
Georg Brandl4833e5b2010-07-03 10:41:33 +0000339.. opcode:: INPLACE_MODULO
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_ADD
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_SUBTRACT
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_LSHIFT
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_RSHIFT
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_AND
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_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000370
371 Implements in-place ``TOS = TOS1 ^ TOS``.
372
373
Georg Brandl4833e5b2010-07-03 10:41:33 +0000374.. opcode:: INPLACE_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000375
376 Implements in-place ``TOS = TOS1 | TOS``.
377
Georg Brandl116aa622007-08-15 14:28:22 +0000378
Georg Brandl4833e5b2010-07-03 10:41:33 +0000379.. opcode:: STORE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000380
381 Implements ``TOS1[TOS] = TOS2``.
382
383
Georg Brandl4833e5b2010-07-03 10:41:33 +0000384.. opcode:: DELETE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000385
386 Implements ``del TOS1[TOS]``.
387
Georg Brandl116aa622007-08-15 14:28:22 +0000388
Georg Brandl4833e5b2010-07-03 10:41:33 +0000389**Miscellaneous opcodes**
Georg Brandl116aa622007-08-15 14:28:22 +0000390
Georg Brandl4833e5b2010-07-03 10:41:33 +0000391.. opcode:: PRINT_EXPR
Georg Brandl116aa622007-08-15 14:28:22 +0000392
393 Implements the expression statement for the interactive mode. TOS is removed
394 from the stack and printed. In non-interactive mode, an expression statement is
395 terminated with ``POP_STACK``.
396
397
Georg Brandl4833e5b2010-07-03 10:41:33 +0000398.. opcode:: BREAK_LOOP
Georg Brandl116aa622007-08-15 14:28:22 +0000399
400 Terminates a loop due to a :keyword:`break` statement.
401
402
403.. opcode:: CONTINUE_LOOP (target)
404
405 Continues a loop due to a :keyword:`continue` statement. *target* is the
406 address to jump to (which should be a ``FOR_ITER`` instruction).
407
408
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000409.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000410
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000411 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000412
413
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000414.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000415
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000416 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
417
418
419.. opcode:: MAP_ADD (i)
420
421 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
422 comprehensions.
423
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000424For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the
425added value or key/value pair is popped off, the container object remains on
426the stack so that it is available for further iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000427
428
Georg Brandl4833e5b2010-07-03 10:41:33 +0000429.. opcode:: RETURN_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000430
431 Returns with TOS to the caller of the function.
432
433
Georg Brandl4833e5b2010-07-03 10:41:33 +0000434.. opcode:: YIELD_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000435
Georg Brandl9afde1c2007-11-01 20:32:30 +0000436 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000437
438
Georg Brandl4833e5b2010-07-03 10:41:33 +0000439.. opcode:: IMPORT_STAR
Georg Brandl116aa622007-08-15 14:28:22 +0000440
441 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
442 local namespace. The module is popped after loading all names. This opcode
443 implements ``from module import *``.
444
445
Georg Brandl4833e5b2010-07-03 10:41:33 +0000446.. opcode:: POP_BLOCK
Georg Brandl116aa622007-08-15 14:28:22 +0000447
448 Removes one block from the block stack. Per frame, there is a stack of blocks,
449 denoting nested loops, try statements, and such.
450
451
Georg Brandl4833e5b2010-07-03 10:41:33 +0000452.. opcode:: POP_EXCEPT
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000453
454 Removes one block from the block stack. The popped block must be an exception
455 handler block, as implicitly created when entering an except handler.
456 In addition to popping extraneous values from the frame stack, the
457 last three popped values are used to restore the exception state.
458
459
Georg Brandl4833e5b2010-07-03 10:41:33 +0000460.. opcode:: END_FINALLY
Georg Brandl116aa622007-08-15 14:28:22 +0000461
462 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
463 exception has to be re-raised, or whether the function returns, and continues
464 with the outer-next block.
465
466
Georg Brandl4833e5b2010-07-03 10:41:33 +0000467.. opcode:: LOAD_BUILD_CLASS
Georg Brandl116aa622007-08-15 14:28:22 +0000468
Georg Brandl5ac22302008-07-20 21:39:03 +0000469 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Benjamin Petersonaac8fd32008-07-20 22:02:26 +0000470 by ``CALL_FUNCTION`` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000471
Guido van Rossum04110fb2007-08-24 16:32:05 +0000472
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000473.. opcode:: SETUP_WITH (delta)
474
475 This opcode performs several operations before a with block starts. First,
476 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
477 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
478 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
479 is pushed. Finally, the result of calling the enter method is pushed onto
480 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
481 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
482 :opcode:`UNPACK_SEQUENCE`).
483
484
Georg Brandl4833e5b2010-07-03 10:41:33 +0000485.. opcode:: WITH_CLEANUP
Guido van Rossum04110fb2007-08-24 16:32:05 +0000486
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000487 Cleans up the stack when a :keyword:`with` statement block exits. TOS is
488 the context manager's :meth:`__exit__` bound method. Below TOS are 1--3
489 values indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000490
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000491 * SECOND = ``None``
492 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
493 * SECOND = ``WHY_*``; no retval below it
494 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000495
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000496 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
497 ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000498
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000499 If the stack represents an exception, *and* the function call returns
500 a 'true' value, this information is "zapped" and replaced with a single
501 ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception.
502 (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000503
Georg Brandl9afde1c2007-11-01 20:32:30 +0000504 .. XXX explain the WHY stuff!
505
Guido van Rossum04110fb2007-08-24 16:32:05 +0000506
Georg Brandl5ac22302008-07-20 21:39:03 +0000507.. opcode:: STORE_LOCALS
508
509 Pops TOS from the stack and stores it as the current frame's ``f_locals``.
510 This is used in class construction.
511
512
Georg Brandl116aa622007-08-15 14:28:22 +0000513All of the following opcodes expect arguments. An argument is two bytes, with
514the more significant byte last.
515
Georg Brandl116aa622007-08-15 14:28:22 +0000516.. opcode:: STORE_NAME (namei)
517
518 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Christian Heimes8640e742008-02-23 16:23:06 +0000519 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl116aa622007-08-15 14:28:22 +0000520 or ``STORE_GLOBAL`` if possible.
521
522
523.. opcode:: DELETE_NAME (namei)
524
525 Implements ``del name``, where *namei* is the index into :attr:`co_names`
526 attribute of the code object.
527
528
529.. opcode:: UNPACK_SEQUENCE (count)
530
531 Unpacks TOS into *count* individual values, which are put onto the stack
532 right-to-left.
533
Georg Brandl116aa622007-08-15 14:28:22 +0000534
Georg Brandl5ac22302008-07-20 21:39:03 +0000535.. opcode:: UNPACK_EX (counts)
536
537 Implements assignment with a starred target: Unpacks an iterable in TOS into
538 individual values, where the total number of values can be smaller than the
539 number of items in the iterable: one the new values will be a list of all
540 leftover items.
541
542 The low byte of *counts* is the number of values before the list value, the
543 high byte of *counts* the number of values after it. The resulting values
544 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000545
Georg Brandl5ac22302008-07-20 21:39:03 +0000546
Georg Brandl116aa622007-08-15 14:28:22 +0000547.. opcode:: STORE_ATTR (namei)
548
549 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
550 :attr:`co_names`.
551
552
553.. opcode:: DELETE_ATTR (namei)
554
555 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
556
557
558.. opcode:: STORE_GLOBAL (namei)
559
560 Works as ``STORE_NAME``, but stores the name as a global.
561
562
563.. opcode:: DELETE_GLOBAL (namei)
564
565 Works as ``DELETE_NAME``, but deletes a global name.
566
Georg Brandl116aa622007-08-15 14:28:22 +0000567
568.. opcode:: LOAD_CONST (consti)
569
570 Pushes ``co_consts[consti]`` onto the stack.
571
572
573.. opcode:: LOAD_NAME (namei)
574
575 Pushes the value associated with ``co_names[namei]`` onto the stack.
576
577
578.. opcode:: BUILD_TUPLE (count)
579
580 Creates a tuple consuming *count* items from the stack, and pushes the resulting
581 tuple onto the stack.
582
583
584.. opcode:: BUILD_LIST (count)
585
586 Works as ``BUILD_TUPLE``, but creates a list.
587
588
589.. opcode:: BUILD_SET (count)
590
591 Works as ``BUILD_TUPLE``, but creates a set.
592
593
Christian Heimesa62da1d2008-01-12 19:39:10 +0000594.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000595
Christian Heimesa62da1d2008-01-12 19:39:10 +0000596 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
597 to hold *count* entries.
Georg Brandl116aa622007-08-15 14:28:22 +0000598
599
600.. opcode:: LOAD_ATTR (namei)
601
602 Replaces TOS with ``getattr(TOS, co_names[namei])``.
603
604
605.. opcode:: COMPARE_OP (opname)
606
607 Performs a Boolean operation. The operation name can be found in
608 ``cmp_op[opname]``.
609
610
611.. opcode:: IMPORT_NAME (namei)
612
Christian Heimesa342c012008-04-20 21:01:16 +0000613 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
614 the *fromlist* and *level* arguments of :func:`__import__`. The module
615 object is pushed onto the stack. The current namespace is not affected:
616 for a proper import statement, a subsequent ``STORE_FAST`` instruction
617 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000618
619
620.. opcode:: IMPORT_FROM (namei)
621
622 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
623 resulting object is pushed onto the stack, to be subsequently stored by a
624 ``STORE_FAST`` instruction.
625
626
627.. opcode:: JUMP_FORWARD (delta)
628
Georg Brandl9afde1c2007-11-01 20:32:30 +0000629 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000630
631
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000632.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000633
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000634 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000635
636
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000637.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000638
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000639 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
640
641
642.. opcode:: JUMP_IF_TRUE_OR_POP (target)
643
644 If TOS is true, sets the bytecode counter to *target* and leaves TOS
645 on the stack. Otherwise (TOS is false), TOS is popped.
646
647
648.. opcode:: JUMP_IF_FALSE_OR_POP (target)
649
650 If TOS is false, sets the bytecode counter to *target* and leaves
651 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000652
653
654.. opcode:: JUMP_ABSOLUTE (target)
655
Georg Brandl9afde1c2007-11-01 20:32:30 +0000656 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000657
658
659.. opcode:: FOR_ITER (delta)
660
Georg Brandl9afde1c2007-11-01 20:32:30 +0000661 ``TOS`` is an :term:`iterator`. Call its :meth:`__next__` method. If this
662 yields a new value, push it on the stack (leaving the iterator below it). If
663 the iterator indicates it is exhausted ``TOS`` is popped, and the byte code
664 counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000665
Georg Brandl116aa622007-08-15 14:28:22 +0000666
667.. opcode:: LOAD_GLOBAL (namei)
668
669 Loads the global named ``co_names[namei]`` onto the stack.
670
Georg Brandl116aa622007-08-15 14:28:22 +0000671
672.. opcode:: SETUP_LOOP (delta)
673
674 Pushes a block for a loop onto the block stack. The block spans from the
675 current instruction with a size of *delta* bytes.
676
677
678.. opcode:: SETUP_EXCEPT (delta)
679
680 Pushes a try block from a try-except clause onto the block stack. *delta* points
681 to the first except block.
682
683
684.. opcode:: SETUP_FINALLY (delta)
685
686 Pushes a try block from a try-except clause onto the block stack. *delta* points
687 to the finally block.
688
Georg Brandl4833e5b2010-07-03 10:41:33 +0000689.. opcode:: STORE_MAP
Christian Heimesa62da1d2008-01-12 19:39:10 +0000690
691 Store a key and value pair in a dictionary. Pops the key and value while leaving
692 the dictionary on the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000693
694.. opcode:: LOAD_FAST (var_num)
695
696 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
697
698
699.. opcode:: STORE_FAST (var_num)
700
701 Stores TOS into the local ``co_varnames[var_num]``.
702
703
704.. opcode:: DELETE_FAST (var_num)
705
706 Deletes local ``co_varnames[var_num]``.
707
708
709.. opcode:: LOAD_CLOSURE (i)
710
711 Pushes a reference to the cell contained in slot *i* of the cell and free
712 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
713 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
714 len(co_cellvars)]``.
715
716
717.. opcode:: LOAD_DEREF (i)
718
719 Loads the cell contained in slot *i* of the cell and free variable storage.
720 Pushes a reference to the object the cell contains on the stack.
721
722
723.. opcode:: STORE_DEREF (i)
724
725 Stores TOS into the cell contained in slot *i* of the cell and free variable
726 storage.
727
728
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000729.. opcode:: DELETE_DEREF (i)
730
731 Empties the cell contained in slot *i* of the cell and free variable storage.
732 Used by the :keyword:`del` statement.
733
734
Georg Brandl116aa622007-08-15 14:28:22 +0000735.. opcode:: RAISE_VARARGS (argc)
736
737 Raises an exception. *argc* indicates the number of parameters to the raise
738 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
739 the parameter as TOS1, and the exception as TOS.
740
741
742.. opcode:: CALL_FUNCTION (argc)
743
744 Calls a function. The low byte of *argc* indicates the number of positional
745 parameters, the high byte the number of keyword parameters. On the stack, the
746 opcode finds the keyword parameters first. For each keyword argument, the value
747 is on top of the key. Below the keyword parameters, the positional parameters
748 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl48310cd2009-01-03 21:18:54 +0000749 the function object to call is on the stack. Pops all function arguments, and
Benjamin Peterson206e3072008-10-19 14:07:49 +0000750 the function itself off the stack, and pushes the return value.
Georg Brandl116aa622007-08-15 14:28:22 +0000751
752
753.. opcode:: MAKE_FUNCTION (argc)
754
755 Pushes a new function object on the stack. TOS is the code associated with the
756 function. The function object is defined to have *argc* default parameters,
757 which are found below TOS.
758
759
760.. opcode:: MAKE_CLOSURE (argc)
761
Guido van Rossum04110fb2007-08-24 16:32:05 +0000762 Creates a new function object, sets its *__closure__* slot, and pushes it on
763 the stack. TOS is the code associated with the function, TOS1 the tuple
764 containing cells for the closure's free variables. The function also has
765 *argc* default parameters, which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000766
767
768.. opcode:: BUILD_SLICE (argc)
769
770 .. index:: builtin: slice
771
772 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
773 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000774 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000775
776
777.. opcode:: EXTENDED_ARG (ext)
778
779 Prefixes any opcode which has an argument too big to fit into the default two
780 bytes. *ext* holds two additional bytes which, taken together with the
781 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
782 most-significant bytes.
783
784
785.. opcode:: CALL_FUNCTION_VAR (argc)
786
787 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
788 on the stack contains the variable argument list, followed by keyword and
789 positional arguments.
790
791
792.. opcode:: CALL_FUNCTION_KW (argc)
793
794 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
795 on the stack contains the keyword arguments dictionary, followed by explicit
796 keyword and positional arguments.
797
798
799.. opcode:: CALL_FUNCTION_VAR_KW (argc)
800
801 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
802 element on the stack contains the keyword arguments dictionary, followed by the
803 variable-arguments tuple, followed by explicit keyword and positional arguments.
804
805
Georg Brandl4833e5b2010-07-03 10:41:33 +0000806.. opcode:: HAVE_ARGUMENT
Georg Brandl116aa622007-08-15 14:28:22 +0000807
808 This is not really an opcode. It identifies the dividing line between opcodes
809 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
810 HAVE_ARGUMENT``.
811