blob: 341beb57d801e11f83dd2b751f98c6cce10fceed [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
Georg Brandl71515ca2009-05-17 12:29:12 +00008The :mod:`dis` module supports the analysis of Python :term:`bytecode` by
9disassembling it. Since there is no Python assembler, this module defines the
10Python assembly language. The Python bytecode which this module takes as an
11input is defined in the file :file:`Include/opcode.h` and used by the compiler
12and the interpreter.
Georg Brandl116aa622007-08-15 14:28:22 +000013
14Example: Given the function :func:`myfunc`::
15
16 def myfunc(alist):
17 return len(alist)
18
19the following command can be used to get the disassembly of :func:`myfunc`::
20
21 >>> dis.dis(myfunc)
22 2 0 LOAD_GLOBAL 0 (len)
23 3 LOAD_FAST 0 (alist)
24 6 CALL_FUNCTION 1
25 9 RETURN_VALUE
26
27(The "2" is a line number).
28
29The :mod:`dis` module defines the following functions and constants:
30
31
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000032.. function:: dis(x=None)
Georg Brandl116aa622007-08-15 14:28:22 +000033
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000034 Disassemble the *x* object. *x* can denote either a module, a
Georg Brandl116aa622007-08-15 14:28:22 +000035 class, a method, a function, or a code object. For a module, it disassembles
36 all functions. For a class, it disassembles all methods. For a single code
Georg Brandl9afde1c2007-11-01 20:32:30 +000037 sequence, it prints one line per bytecode instruction. If no object is
Georg Brandl116aa622007-08-15 14:28:22 +000038 provided, it disassembles the last traceback.
39
40
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000041.. function:: distb(tb=None)
Georg Brandl116aa622007-08-15 14:28:22 +000042
43 Disassembles the top-of-stack function of a traceback, using the last traceback
44 if none was passed. The instruction causing the exception is indicated.
45
46
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000047.. function:: disassemble(code, lasti=-1)
48 disco(code, lasti=-1)
Georg Brandl116aa622007-08-15 14:28:22 +000049
50 Disassembles a code object, indicating the last instruction if *lasti* was
51 provided. The output is divided in the following columns:
52
53 #. the line number, for the first instruction of each line
54 #. the current instruction, indicated as ``-->``,
55 #. a labelled instruction, indicated with ``>>``,
56 #. the address of the instruction,
57 #. the operation code name,
58 #. operation parameters, and
59 #. interpretation of the parameters in parentheses.
60
61 The parameter interpretation recognizes local and global variable names,
62 constant values, branch targets, and compare operators.
63
64
Benjamin Peterson75edad02009-01-01 15:05:06 +000065.. function:: findlinestarts(code)
66
67 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
68 attributes of the code object *code* to find the offsets which are starts of
69 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
70
71
72.. function:: findlabels(code)
73
74 Detect all offsets in the code object *code* which are jump targets, and
75 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +000076
77
Georg Brandl116aa622007-08-15 14:28:22 +000078.. data:: opname
79
Georg Brandl9afde1c2007-11-01 20:32:30 +000080 Sequence of operation names, indexable using the bytecode.
Georg Brandl116aa622007-08-15 14:28:22 +000081
82
83.. data:: opmap
84
Georg Brandl9afde1c2007-11-01 20:32:30 +000085 Dictionary mapping bytecodes to operation names.
Georg Brandl116aa622007-08-15 14:28:22 +000086
87
88.. data:: cmp_op
89
90 Sequence of all compare operation names.
91
92
93.. data:: hasconst
94
Georg Brandl9afde1c2007-11-01 20:32:30 +000095 Sequence of bytecodes that have a constant parameter.
Georg Brandl116aa622007-08-15 14:28:22 +000096
97
98.. data:: hasfree
99
Georg Brandl9afde1c2007-11-01 20:32:30 +0000100 Sequence of bytecodes that access a free variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000101
102
103.. data:: hasname
104
Georg Brandl9afde1c2007-11-01 20:32:30 +0000105 Sequence of bytecodes that access an attribute by name.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107
108.. data:: hasjrel
109
Georg Brandl9afde1c2007-11-01 20:32:30 +0000110 Sequence of bytecodes that have a relative jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112
113.. data:: hasjabs
114
Georg Brandl9afde1c2007-11-01 20:32:30 +0000115 Sequence of bytecodes that have an absolute jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000116
117
118.. data:: haslocal
119
Georg Brandl9afde1c2007-11-01 20:32:30 +0000120 Sequence of bytecodes that access a local variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000121
122
123.. data:: hascompare
124
Georg Brandl9afde1c2007-11-01 20:32:30 +0000125 Sequence of bytecodes of Boolean operations.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127
128.. _bytecodes:
129
Georg Brandl9afde1c2007-11-01 20:32:30 +0000130Python Bytecode Instructions
131----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000132
Georg Brandl9afde1c2007-11-01 20:32:30 +0000133The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135
136.. opcode:: STOP_CODE ()
137
138 Indicates end-of-code to the compiler, not used by the interpreter.
139
140
141.. opcode:: NOP ()
142
143 Do nothing code. Used as a placeholder by the bytecode optimizer.
144
145
146.. opcode:: POP_TOP ()
147
148 Removes the top-of-stack (TOS) item.
149
150
151.. opcode:: ROT_TWO ()
152
153 Swaps the two top-most stack items.
154
155
156.. opcode:: ROT_THREE ()
157
158 Lifts second and third stack item one position up, moves top down to position
159 three.
160
161
162.. opcode:: ROT_FOUR ()
163
164 Lifts second, third and forth stack item one position up, moves top down to
165 position four.
166
167
168.. opcode:: DUP_TOP ()
169
170 Duplicates the reference on top of the stack.
171
172Unary Operations take the top of the stack, apply the operation, and push the
173result back on the stack.
174
175
176.. opcode:: UNARY_POSITIVE ()
177
178 Implements ``TOS = +TOS``.
179
180
181.. opcode:: UNARY_NEGATIVE ()
182
183 Implements ``TOS = -TOS``.
184
185
186.. opcode:: UNARY_NOT ()
187
188 Implements ``TOS = not TOS``.
189
190
191.. opcode:: UNARY_INVERT ()
192
193 Implements ``TOS = ~TOS``.
194
195
196.. opcode:: GET_ITER ()
197
198 Implements ``TOS = iter(TOS)``.
199
200Binary operations remove the top of the stack (TOS) and the second top-most
201stack item (TOS1) from the stack. They perform the operation, and put the
202result back on the stack.
203
204
205.. opcode:: BINARY_POWER ()
206
207 Implements ``TOS = TOS1 ** TOS``.
208
209
210.. opcode:: BINARY_MULTIPLY ()
211
212 Implements ``TOS = TOS1 * TOS``.
213
214
215.. opcode:: BINARY_FLOOR_DIVIDE ()
216
217 Implements ``TOS = TOS1 // TOS``.
218
219
220.. opcode:: BINARY_TRUE_DIVIDE ()
221
222 Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is in
223 effect.
224
225
226.. opcode:: BINARY_MODULO ()
227
228 Implements ``TOS = TOS1 % TOS``.
229
230
231.. opcode:: BINARY_ADD ()
232
233 Implements ``TOS = TOS1 + TOS``.
234
235
236.. opcode:: BINARY_SUBTRACT ()
237
238 Implements ``TOS = TOS1 - TOS``.
239
240
241.. opcode:: BINARY_SUBSCR ()
242
243 Implements ``TOS = TOS1[TOS]``.
244
245
246.. opcode:: BINARY_LSHIFT ()
247
248 Implements ``TOS = TOS1 << TOS``.
249
250
251.. opcode:: BINARY_RSHIFT ()
252
253 Implements ``TOS = TOS1 >> TOS``.
254
255
256.. opcode:: BINARY_AND ()
257
258 Implements ``TOS = TOS1 & TOS``.
259
260
261.. opcode:: BINARY_XOR ()
262
263 Implements ``TOS = TOS1 ^ TOS``.
264
265
266.. opcode:: BINARY_OR ()
267
268 Implements ``TOS = TOS1 | TOS``.
269
270In-place operations are like binary operations, in that they remove TOS and
271TOS1, and push the result back on the stack, but the operation is done in-place
272when TOS1 supports it, and the resulting TOS may be (but does not have to be)
273the original TOS1.
274
275
276.. opcode:: INPLACE_POWER ()
277
278 Implements in-place ``TOS = TOS1 ** TOS``.
279
280
281.. opcode:: INPLACE_MULTIPLY ()
282
283 Implements in-place ``TOS = TOS1 * TOS``.
284
285
286.. opcode:: INPLACE_FLOOR_DIVIDE ()
287
288 Implements in-place ``TOS = TOS1 // TOS``.
289
290
291.. opcode:: INPLACE_TRUE_DIVIDE ()
292
293 Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
294 division`` is in effect.
295
296
297.. opcode:: INPLACE_MODULO ()
298
299 Implements in-place ``TOS = TOS1 % TOS``.
300
301
302.. opcode:: INPLACE_ADD ()
303
304 Implements in-place ``TOS = TOS1 + TOS``.
305
306
307.. opcode:: INPLACE_SUBTRACT ()
308
309 Implements in-place ``TOS = TOS1 - TOS``.
310
311
312.. opcode:: INPLACE_LSHIFT ()
313
314 Implements in-place ``TOS = TOS1 << TOS``.
315
316
317.. opcode:: INPLACE_RSHIFT ()
318
319 Implements in-place ``TOS = TOS1 >> TOS``.
320
321
322.. opcode:: INPLACE_AND ()
323
324 Implements in-place ``TOS = TOS1 & TOS``.
325
326
327.. opcode:: INPLACE_XOR ()
328
329 Implements in-place ``TOS = TOS1 ^ TOS``.
330
331
332.. opcode:: INPLACE_OR ()
333
334 Implements in-place ``TOS = TOS1 | TOS``.
335
Georg Brandl116aa622007-08-15 14:28:22 +0000336
337.. opcode:: STORE_SUBSCR ()
338
339 Implements ``TOS1[TOS] = TOS2``.
340
341
342.. opcode:: DELETE_SUBSCR ()
343
344 Implements ``del TOS1[TOS]``.
345
346Miscellaneous opcodes.
347
348
349.. opcode:: PRINT_EXPR ()
350
351 Implements the expression statement for the interactive mode. TOS is removed
352 from the stack and printed. In non-interactive mode, an expression statement is
353 terminated with ``POP_STACK``.
354
355
356.. opcode:: BREAK_LOOP ()
357
358 Terminates a loop due to a :keyword:`break` statement.
359
360
361.. opcode:: CONTINUE_LOOP (target)
362
363 Continues a loop due to a :keyword:`continue` statement. *target* is the
364 address to jump to (which should be a ``FOR_ITER`` instruction).
365
366
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000367.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000368
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000369 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000370
371
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000372.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000373
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000374 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
375
376
377.. opcode:: MAP_ADD (i)
378
379 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
380 comprehensions.
381
382
383For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the
384added value or key/value pair is popped off, the container object remains on
385the stack so that it is available for further iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000386
387
388.. opcode:: LOAD_LOCALS ()
389
390 Pushes a reference to the locals of the current scope on the stack. This is used
391 in the code for a class definition: After the class body is evaluated, the
392 locals are passed to the class definition.
393
394
395.. opcode:: RETURN_VALUE ()
396
397 Returns with TOS to the caller of the function.
398
399
400.. opcode:: YIELD_VALUE ()
401
Georg Brandl9afde1c2007-11-01 20:32:30 +0000402 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000403
404
405.. opcode:: IMPORT_STAR ()
406
407 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
408 local namespace. The module is popped after loading all names. This opcode
409 implements ``from module import *``.
410
411
412.. opcode:: POP_BLOCK ()
413
414 Removes one block from the block stack. Per frame, there is a stack of blocks,
415 denoting nested loops, try statements, and such.
416
417
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000418.. opcode:: POP_EXCEPT ()
419
420 Removes one block from the block stack. The popped block must be an exception
421 handler block, as implicitly created when entering an except handler.
422 In addition to popping extraneous values from the frame stack, the
423 last three popped values are used to restore the exception state.
424
425
Georg Brandl116aa622007-08-15 14:28:22 +0000426.. opcode:: END_FINALLY ()
427
428 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
429 exception has to be re-raised, or whether the function returns, and continues
430 with the outer-next block.
431
432
Benjamin Peterson69164c72008-07-03 14:45:20 +0000433.. opcode:: LOAD_BUILD_CLASS ()
Georg Brandl116aa622007-08-15 14:28:22 +0000434
Georg Brandl5ac22302008-07-20 21:39:03 +0000435 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Benjamin Petersonaac8fd32008-07-20 22:02:26 +0000436 by ``CALL_FUNCTION`` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000437
Guido van Rossum04110fb2007-08-24 16:32:05 +0000438
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000439.. opcode:: SETUP_WITH (delta)
440
441 This opcode performs several operations before a with block starts. First,
442 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
443 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
444 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
445 is pushed. Finally, the result of calling the enter method is pushed onto
446 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
447 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
448 :opcode:`UNPACK_SEQUENCE`).
449
450
Guido van Rossum04110fb2007-08-24 16:32:05 +0000451.. opcode:: WITH_CLEANUP ()
452
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000453 Cleans up the stack when a :keyword:`with` statement block exits. TOS is
454 the context manager's :meth:`__exit__` bound method. Below TOS are 1--3
455 values indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000456
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000457 * SECOND = ``None``
458 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
459 * SECOND = ``WHY_*``; no retval below it
460 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000461
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000462 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
463 ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000464
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000465 If the stack represents an exception, *and* the function call returns
466 a 'true' value, this information is "zapped" and replaced with a single
467 ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception.
468 (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000469
Georg Brandl9afde1c2007-11-01 20:32:30 +0000470 .. XXX explain the WHY stuff!
471
Guido van Rossum04110fb2007-08-24 16:32:05 +0000472
Georg Brandl5ac22302008-07-20 21:39:03 +0000473.. opcode:: STORE_LOCALS
474
475 Pops TOS from the stack and stores it as the current frame's ``f_locals``.
476 This is used in class construction.
477
478
Georg Brandl116aa622007-08-15 14:28:22 +0000479All of the following opcodes expect arguments. An argument is two bytes, with
480the more significant byte last.
481
Georg Brandl116aa622007-08-15 14:28:22 +0000482.. opcode:: STORE_NAME (namei)
483
484 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Christian Heimes8640e742008-02-23 16:23:06 +0000485 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl116aa622007-08-15 14:28:22 +0000486 or ``STORE_GLOBAL`` if possible.
487
488
489.. opcode:: DELETE_NAME (namei)
490
491 Implements ``del name``, where *namei* is the index into :attr:`co_names`
492 attribute of the code object.
493
494
495.. opcode:: UNPACK_SEQUENCE (count)
496
497 Unpacks TOS into *count* individual values, which are put onto the stack
498 right-to-left.
499
Georg Brandl116aa622007-08-15 14:28:22 +0000500
Georg Brandl5ac22302008-07-20 21:39:03 +0000501.. opcode:: UNPACK_EX (counts)
502
503 Implements assignment with a starred target: Unpacks an iterable in TOS into
504 individual values, where the total number of values can be smaller than the
505 number of items in the iterable: one the new values will be a list of all
506 leftover items.
507
508 The low byte of *counts* is the number of values before the list value, the
509 high byte of *counts* the number of values after it. The resulting values
510 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000511
Georg Brandl5ac22302008-07-20 21:39:03 +0000512
Georg Brandl116aa622007-08-15 14:28:22 +0000513.. opcode:: DUP_TOPX (count)
514
515 Duplicate *count* items, keeping them in the same order. Due to implementation
516 limits, *count* should be between 1 and 5 inclusive.
517
518
519.. opcode:: STORE_ATTR (namei)
520
521 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
522 :attr:`co_names`.
523
524
525.. opcode:: DELETE_ATTR (namei)
526
527 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
528
529
530.. opcode:: STORE_GLOBAL (namei)
531
532 Works as ``STORE_NAME``, but stores the name as a global.
533
534
535.. opcode:: DELETE_GLOBAL (namei)
536
537 Works as ``DELETE_NAME``, but deletes a global name.
538
Georg Brandl116aa622007-08-15 14:28:22 +0000539
540.. opcode:: LOAD_CONST (consti)
541
542 Pushes ``co_consts[consti]`` onto the stack.
543
544
545.. opcode:: LOAD_NAME (namei)
546
547 Pushes the value associated with ``co_names[namei]`` onto the stack.
548
549
550.. opcode:: BUILD_TUPLE (count)
551
552 Creates a tuple consuming *count* items from the stack, and pushes the resulting
553 tuple onto the stack.
554
555
556.. opcode:: BUILD_LIST (count)
557
558 Works as ``BUILD_TUPLE``, but creates a list.
559
560
561.. opcode:: BUILD_SET (count)
562
563 Works as ``BUILD_TUPLE``, but creates a set.
564
565
Christian Heimesa62da1d2008-01-12 19:39:10 +0000566.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000567
Christian Heimesa62da1d2008-01-12 19:39:10 +0000568 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
569 to hold *count* entries.
Georg Brandl116aa622007-08-15 14:28:22 +0000570
571
572.. opcode:: LOAD_ATTR (namei)
573
574 Replaces TOS with ``getattr(TOS, co_names[namei])``.
575
576
577.. opcode:: COMPARE_OP (opname)
578
579 Performs a Boolean operation. The operation name can be found in
580 ``cmp_op[opname]``.
581
582
583.. opcode:: IMPORT_NAME (namei)
584
Christian Heimesa342c012008-04-20 21:01:16 +0000585 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
586 the *fromlist* and *level* arguments of :func:`__import__`. The module
587 object is pushed onto the stack. The current namespace is not affected:
588 for a proper import statement, a subsequent ``STORE_FAST`` instruction
589 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000590
591
592.. opcode:: IMPORT_FROM (namei)
593
594 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
595 resulting object is pushed onto the stack, to be subsequently stored by a
596 ``STORE_FAST`` instruction.
597
598
599.. opcode:: JUMP_FORWARD (delta)
600
Georg Brandl9afde1c2007-11-01 20:32:30 +0000601 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000602
603
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000604.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000605
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000606 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000607
608
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000609.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000610
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000611 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
612
613
614.. opcode:: JUMP_IF_TRUE_OR_POP (target)
615
616 If TOS is true, sets the bytecode counter to *target* and leaves TOS
617 on the stack. Otherwise (TOS is false), TOS is popped.
618
619
620.. opcode:: JUMP_IF_FALSE_OR_POP (target)
621
622 If TOS is false, sets the bytecode counter to *target* and leaves
623 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000624
625
626.. opcode:: JUMP_ABSOLUTE (target)
627
Georg Brandl9afde1c2007-11-01 20:32:30 +0000628 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000629
630
631.. opcode:: FOR_ITER (delta)
632
Georg Brandl9afde1c2007-11-01 20:32:30 +0000633 ``TOS`` is an :term:`iterator`. Call its :meth:`__next__` method. If this
634 yields a new value, push it on the stack (leaving the iterator below it). If
635 the iterator indicates it is exhausted ``TOS`` is popped, and the byte code
636 counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000637
Georg Brandl116aa622007-08-15 14:28:22 +0000638
639.. opcode:: LOAD_GLOBAL (namei)
640
641 Loads the global named ``co_names[namei]`` onto the stack.
642
Georg Brandl116aa622007-08-15 14:28:22 +0000643
644.. opcode:: SETUP_LOOP (delta)
645
646 Pushes a block for a loop onto the block stack. The block spans from the
647 current instruction with a size of *delta* bytes.
648
649
650.. opcode:: SETUP_EXCEPT (delta)
651
652 Pushes a try block from a try-except clause onto the block stack. *delta* points
653 to the first except block.
654
655
656.. opcode:: SETUP_FINALLY (delta)
657
658 Pushes a try block from a try-except clause onto the block stack. *delta* points
659 to the finally block.
660
Christian Heimesa62da1d2008-01-12 19:39:10 +0000661.. opcode:: STORE_MAP ()
662
663 Store a key and value pair in a dictionary. Pops the key and value while leaving
664 the dictionary on the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000665
666.. opcode:: LOAD_FAST (var_num)
667
668 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
669
670
671.. opcode:: STORE_FAST (var_num)
672
673 Stores TOS into the local ``co_varnames[var_num]``.
674
675
676.. opcode:: DELETE_FAST (var_num)
677
678 Deletes local ``co_varnames[var_num]``.
679
680
681.. opcode:: LOAD_CLOSURE (i)
682
683 Pushes a reference to the cell contained in slot *i* of the cell and free
684 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
685 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
686 len(co_cellvars)]``.
687
688
689.. opcode:: LOAD_DEREF (i)
690
691 Loads the cell contained in slot *i* of the cell and free variable storage.
692 Pushes a reference to the object the cell contains on the stack.
693
694
695.. opcode:: STORE_DEREF (i)
696
697 Stores TOS into the cell contained in slot *i* of the cell and free variable
698 storage.
699
700
701.. opcode:: SET_LINENO (lineno)
702
703 This opcode is obsolete.
704
705
706.. opcode:: RAISE_VARARGS (argc)
707
708 Raises an exception. *argc* indicates the number of parameters to the raise
709 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
710 the parameter as TOS1, and the exception as TOS.
711
712
713.. opcode:: CALL_FUNCTION (argc)
714
715 Calls a function. The low byte of *argc* indicates the number of positional
716 parameters, the high byte the number of keyword parameters. On the stack, the
717 opcode finds the keyword parameters first. For each keyword argument, the value
718 is on top of the key. Below the keyword parameters, the positional parameters
719 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl48310cd2009-01-03 21:18:54 +0000720 the function object to call is on the stack. Pops all function arguments, and
Benjamin Peterson206e3072008-10-19 14:07:49 +0000721 the function itself off the stack, and pushes the return value.
Georg Brandl116aa622007-08-15 14:28:22 +0000722
723
724.. opcode:: MAKE_FUNCTION (argc)
725
726 Pushes a new function object on the stack. TOS is the code associated with the
727 function. The function object is defined to have *argc* default parameters,
728 which are found below TOS.
729
730
731.. opcode:: MAKE_CLOSURE (argc)
732
Guido van Rossum04110fb2007-08-24 16:32:05 +0000733 Creates a new function object, sets its *__closure__* slot, and pushes it on
734 the stack. TOS is the code associated with the function, TOS1 the tuple
735 containing cells for the closure's free variables. The function also has
736 *argc* default parameters, which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000737
738
739.. opcode:: BUILD_SLICE (argc)
740
741 .. index:: builtin: slice
742
743 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
744 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000745 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000746
747
748.. opcode:: EXTENDED_ARG (ext)
749
750 Prefixes any opcode which has an argument too big to fit into the default two
751 bytes. *ext* holds two additional bytes which, taken together with the
752 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
753 most-significant bytes.
754
755
756.. opcode:: CALL_FUNCTION_VAR (argc)
757
758 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
759 on the stack contains the variable argument list, followed by keyword and
760 positional arguments.
761
762
763.. opcode:: CALL_FUNCTION_KW (argc)
764
765 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
766 on the stack contains the keyword arguments dictionary, followed by explicit
767 keyword and positional arguments.
768
769
770.. opcode:: CALL_FUNCTION_VAR_KW (argc)
771
772 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
773 element on the stack contains the keyword arguments dictionary, followed by the
774 variable-arguments tuple, followed by explicit keyword and positional arguments.
775
776
777.. opcode:: HAVE_ARGUMENT ()
778
779 This is not really an opcode. It identifies the dividing line between opcodes
780 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
781 HAVE_ARGUMENT``.
782