blob: 856a9e4f3974cfe22356e87e965ef90498aa1a9e [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
Georg Brandl9afde1c2007-11-01 20:32:30 +00002:mod:`dis` --- Disassembler for Python bytecode
3===============================================
Georg Brandl116aa622007-08-15 14:28:22 +00004
5.. module:: dis
Georg Brandl9afde1c2007-11-01 20:32:30 +00006 :synopsis: Disassembler for Python bytecode.
Georg Brandl116aa622007-08-15 14:28:22 +00007
8
Georg Brandl9afde1c2007-11-01 20:32:30 +00009The :mod:`dis` module supports the analysis of Python :term:`bytecode` by disassembling
Georg Brandl116aa622007-08-15 14:28:22 +000010it. Since there is no Python assembler, this module defines the Python assembly
Georg Brandl9afde1c2007-11-01 20:32:30 +000011language. The Python bytecode which this module takes as an input is defined
Georg Brandl116aa622007-08-15 14:28:22 +000012in the file :file:`Include/opcode.h` and used by the compiler and the
13interpreter.
14
15Example: Given the function :func:`myfunc`::
16
17 def myfunc(alist):
18 return len(alist)
19
20the following command can be used to get the disassembly of :func:`myfunc`::
21
22 >>> dis.dis(myfunc)
23 2 0 LOAD_GLOBAL 0 (len)
24 3 LOAD_FAST 0 (alist)
25 6 CALL_FUNCTION 1
26 9 RETURN_VALUE
27
28(The "2" is a line number).
29
30The :mod:`dis` module defines the following functions and constants:
31
32
33.. function:: dis([bytesource])
34
35 Disassemble the *bytesource* object. *bytesource* can denote either a module, a
36 class, a method, a function, or a code object. For a module, it disassembles
37 all functions. For a class, it disassembles all methods. For a single code
Georg Brandl9afde1c2007-11-01 20:32:30 +000038 sequence, it prints one line per bytecode instruction. If no object is
Georg Brandl116aa622007-08-15 14:28:22 +000039 provided, it disassembles the last traceback.
40
41
42.. function:: distb([tb])
43
44 Disassembles the top-of-stack function of a traceback, using the last traceback
45 if none was passed. The instruction causing the exception is indicated.
46
47
48.. function:: disassemble(code[, lasti])
49
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
65.. function:: disco(code[, lasti])
66
Benjamin Peterson75edad02009-01-01 15:05:06 +000067 A synonym for :func:`disassemble`. It is more convenient to type, and kept
68 for compatibility with earlier Python releases.
Georg Brandl116aa622007-08-15 14:28:22 +000069
70
Benjamin Peterson75edad02009-01-01 15:05:06 +000071.. function:: findlinestarts(code)
72
73 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
74 attributes of the code object *code* to find the offsets which are starts of
75 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
76
77
78.. function:: findlabels(code)
79
80 Detect all offsets in the code object *code* which are jump targets, and
81 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +000082
83
Georg Brandl116aa622007-08-15 14:28:22 +000084.. data:: opname
85
Georg Brandl9afde1c2007-11-01 20:32:30 +000086 Sequence of operation names, indexable using the bytecode.
Georg Brandl116aa622007-08-15 14:28:22 +000087
88
89.. data:: opmap
90
Georg Brandl9afde1c2007-11-01 20:32:30 +000091 Dictionary mapping bytecodes to operation names.
Georg Brandl116aa622007-08-15 14:28:22 +000092
93
94.. data:: cmp_op
95
96 Sequence of all compare operation names.
97
98
99.. data:: hasconst
100
Georg Brandl9afde1c2007-11-01 20:32:30 +0000101 Sequence of bytecodes that have a constant parameter.
Georg Brandl116aa622007-08-15 14:28:22 +0000102
103
104.. data:: hasfree
105
Georg Brandl9afde1c2007-11-01 20:32:30 +0000106 Sequence of bytecodes that access a free variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108
109.. data:: hasname
110
Georg Brandl9afde1c2007-11-01 20:32:30 +0000111 Sequence of bytecodes that access an attribute by name.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113
114.. data:: hasjrel
115
Georg Brandl9afde1c2007-11-01 20:32:30 +0000116 Sequence of bytecodes that have a relative jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118
119.. data:: hasjabs
120
Georg Brandl9afde1c2007-11-01 20:32:30 +0000121 Sequence of bytecodes that have an absolute jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123
124.. data:: haslocal
125
Georg Brandl9afde1c2007-11-01 20:32:30 +0000126 Sequence of bytecodes that access a local variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000127
128
129.. data:: hascompare
130
Georg Brandl9afde1c2007-11-01 20:32:30 +0000131 Sequence of bytecodes of Boolean operations.
Georg Brandl116aa622007-08-15 14:28:22 +0000132
133
134.. _bytecodes:
135
Georg Brandl9afde1c2007-11-01 20:32:30 +0000136Python Bytecode Instructions
137----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000138
Georg Brandl9afde1c2007-11-01 20:32:30 +0000139The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000140
141
142.. opcode:: STOP_CODE ()
143
144 Indicates end-of-code to the compiler, not used by the interpreter.
145
146
147.. opcode:: NOP ()
148
149 Do nothing code. Used as a placeholder by the bytecode optimizer.
150
151
152.. opcode:: POP_TOP ()
153
154 Removes the top-of-stack (TOS) item.
155
156
157.. opcode:: ROT_TWO ()
158
159 Swaps the two top-most stack items.
160
161
162.. opcode:: ROT_THREE ()
163
164 Lifts second and third stack item one position up, moves top down to position
165 three.
166
167
168.. opcode:: ROT_FOUR ()
169
170 Lifts second, third and forth stack item one position up, moves top down to
171 position four.
172
173
174.. opcode:: DUP_TOP ()
175
176 Duplicates the reference on top of the stack.
177
178Unary Operations take the top of the stack, apply the operation, and push the
179result back on the stack.
180
181
182.. opcode:: UNARY_POSITIVE ()
183
184 Implements ``TOS = +TOS``.
185
186
187.. opcode:: UNARY_NEGATIVE ()
188
189 Implements ``TOS = -TOS``.
190
191
192.. opcode:: UNARY_NOT ()
193
194 Implements ``TOS = not TOS``.
195
196
197.. opcode:: UNARY_INVERT ()
198
199 Implements ``TOS = ~TOS``.
200
201
202.. opcode:: GET_ITER ()
203
204 Implements ``TOS = iter(TOS)``.
205
206Binary operations remove the top of the stack (TOS) and the second top-most
207stack item (TOS1) from the stack. They perform the operation, and put the
208result back on the stack.
209
210
211.. opcode:: BINARY_POWER ()
212
213 Implements ``TOS = TOS1 ** TOS``.
214
215
216.. opcode:: BINARY_MULTIPLY ()
217
218 Implements ``TOS = TOS1 * TOS``.
219
220
221.. opcode:: BINARY_FLOOR_DIVIDE ()
222
223 Implements ``TOS = TOS1 // TOS``.
224
225
226.. opcode:: BINARY_TRUE_DIVIDE ()
227
228 Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is in
229 effect.
230
231
232.. opcode:: BINARY_MODULO ()
233
234 Implements ``TOS = TOS1 % TOS``.
235
236
237.. opcode:: BINARY_ADD ()
238
239 Implements ``TOS = TOS1 + TOS``.
240
241
242.. opcode:: BINARY_SUBTRACT ()
243
244 Implements ``TOS = TOS1 - TOS``.
245
246
247.. opcode:: BINARY_SUBSCR ()
248
249 Implements ``TOS = TOS1[TOS]``.
250
251
252.. opcode:: BINARY_LSHIFT ()
253
254 Implements ``TOS = TOS1 << TOS``.
255
256
257.. opcode:: BINARY_RSHIFT ()
258
259 Implements ``TOS = TOS1 >> TOS``.
260
261
262.. opcode:: BINARY_AND ()
263
264 Implements ``TOS = TOS1 & TOS``.
265
266
267.. opcode:: BINARY_XOR ()
268
269 Implements ``TOS = TOS1 ^ TOS``.
270
271
272.. opcode:: BINARY_OR ()
273
274 Implements ``TOS = TOS1 | TOS``.
275
276In-place operations are like binary operations, in that they remove TOS and
277TOS1, and push the result back on the stack, but the operation is done in-place
278when TOS1 supports it, and the resulting TOS may be (but does not have to be)
279the original TOS1.
280
281
282.. opcode:: INPLACE_POWER ()
283
284 Implements in-place ``TOS = TOS1 ** TOS``.
285
286
287.. opcode:: INPLACE_MULTIPLY ()
288
289 Implements in-place ``TOS = TOS1 * TOS``.
290
291
292.. opcode:: INPLACE_FLOOR_DIVIDE ()
293
294 Implements in-place ``TOS = TOS1 // TOS``.
295
296
297.. opcode:: INPLACE_TRUE_DIVIDE ()
298
299 Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
300 division`` is in effect.
301
302
303.. opcode:: INPLACE_MODULO ()
304
305 Implements in-place ``TOS = TOS1 % TOS``.
306
307
308.. opcode:: INPLACE_ADD ()
309
310 Implements in-place ``TOS = TOS1 + TOS``.
311
312
313.. opcode:: INPLACE_SUBTRACT ()
314
315 Implements in-place ``TOS = TOS1 - TOS``.
316
317
318.. opcode:: INPLACE_LSHIFT ()
319
320 Implements in-place ``TOS = TOS1 << TOS``.
321
322
323.. opcode:: INPLACE_RSHIFT ()
324
325 Implements in-place ``TOS = TOS1 >> TOS``.
326
327
328.. opcode:: INPLACE_AND ()
329
330 Implements in-place ``TOS = TOS1 & TOS``.
331
332
333.. opcode:: INPLACE_XOR ()
334
335 Implements in-place ``TOS = TOS1 ^ TOS``.
336
337
338.. opcode:: INPLACE_OR ()
339
340 Implements in-place ``TOS = TOS1 | TOS``.
341
Georg Brandl116aa622007-08-15 14:28:22 +0000342
343.. opcode:: STORE_SUBSCR ()
344
345 Implements ``TOS1[TOS] = TOS2``.
346
347
348.. opcode:: DELETE_SUBSCR ()
349
350 Implements ``del TOS1[TOS]``.
351
352Miscellaneous opcodes.
353
354
355.. opcode:: PRINT_EXPR ()
356
357 Implements the expression statement for the interactive mode. TOS is removed
358 from the stack and printed. In non-interactive mode, an expression statement is
359 terminated with ``POP_STACK``.
360
361
362.. opcode:: BREAK_LOOP ()
363
364 Terminates a loop due to a :keyword:`break` statement.
365
366
367.. opcode:: CONTINUE_LOOP (target)
368
369 Continues a loop due to a :keyword:`continue` statement. *target* is the
370 address to jump to (which should be a ``FOR_ITER`` instruction).
371
372
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000373.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000374
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000375 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000376
377
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000378.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000379
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000380 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
381
382
383.. opcode:: MAP_ADD (i)
384
385 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
386 comprehensions.
387
388
389For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the
390added value or key/value pair is popped off, the container object remains on
391the stack so that it is available for further iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000392
393
394.. opcode:: LOAD_LOCALS ()
395
396 Pushes a reference to the locals of the current scope on the stack. This is used
397 in the code for a class definition: After the class body is evaluated, the
398 locals are passed to the class definition.
399
400
401.. opcode:: RETURN_VALUE ()
402
403 Returns with TOS to the caller of the function.
404
405
406.. opcode:: YIELD_VALUE ()
407
Georg Brandl9afde1c2007-11-01 20:32:30 +0000408 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000409
410
411.. opcode:: IMPORT_STAR ()
412
413 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
414 local namespace. The module is popped after loading all names. This opcode
415 implements ``from module import *``.
416
417
418.. opcode:: POP_BLOCK ()
419
420 Removes one block from the block stack. Per frame, there is a stack of blocks,
421 denoting nested loops, try statements, and such.
422
423
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000424.. opcode:: POP_EXCEPT ()
425
426 Removes one block from the block stack. The popped block must be an exception
427 handler block, as implicitly created when entering an except handler.
428 In addition to popping extraneous values from the frame stack, the
429 last three popped values are used to restore the exception state.
430
431
Georg Brandl116aa622007-08-15 14:28:22 +0000432.. opcode:: END_FINALLY ()
433
434 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
435 exception has to be re-raised, or whether the function returns, and continues
436 with the outer-next block.
437
438
Benjamin Peterson69164c72008-07-03 14:45:20 +0000439.. opcode:: LOAD_BUILD_CLASS ()
Georg Brandl116aa622007-08-15 14:28:22 +0000440
Georg Brandl5ac22302008-07-20 21:39:03 +0000441 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Benjamin Petersonaac8fd32008-07-20 22:02:26 +0000442 by ``CALL_FUNCTION`` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000443
Guido van Rossum04110fb2007-08-24 16:32:05 +0000444
445.. opcode:: WITH_CLEANUP ()
446
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000447 Cleans up the stack when a :keyword:`with` statement block exits. TOS is
448 the context manager's :meth:`__exit__` bound method. Below TOS are 1--3
449 values indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000450
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000451 * SECOND = ``None``
452 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
453 * SECOND = ``WHY_*``; no retval below it
454 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000455
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000456 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
457 ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000458
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000459 If the stack represents an exception, *and* the function call returns
460 a 'true' value, this information is "zapped" and replaced with a single
461 ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception.
462 (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000463
Georg Brandl9afde1c2007-11-01 20:32:30 +0000464 .. XXX explain the WHY stuff!
465
Guido van Rossum04110fb2007-08-24 16:32:05 +0000466
Georg Brandl5ac22302008-07-20 21:39:03 +0000467.. opcode:: STORE_LOCALS
468
469 Pops TOS from the stack and stores it as the current frame's ``f_locals``.
470 This is used in class construction.
471
472
Georg Brandl116aa622007-08-15 14:28:22 +0000473All of the following opcodes expect arguments. An argument is two bytes, with
474the more significant byte last.
475
Georg Brandl116aa622007-08-15 14:28:22 +0000476.. opcode:: STORE_NAME (namei)
477
478 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Christian Heimes8640e742008-02-23 16:23:06 +0000479 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl116aa622007-08-15 14:28:22 +0000480 or ``STORE_GLOBAL`` if possible.
481
482
483.. opcode:: DELETE_NAME (namei)
484
485 Implements ``del name``, where *namei* is the index into :attr:`co_names`
486 attribute of the code object.
487
488
489.. opcode:: UNPACK_SEQUENCE (count)
490
491 Unpacks TOS into *count* individual values, which are put onto the stack
492 right-to-left.
493
Georg Brandl116aa622007-08-15 14:28:22 +0000494
Georg Brandl5ac22302008-07-20 21:39:03 +0000495.. opcode:: UNPACK_EX (counts)
496
497 Implements assignment with a starred target: Unpacks an iterable in TOS into
498 individual values, where the total number of values can be smaller than the
499 number of items in the iterable: one the new values will be a list of all
500 leftover items.
501
502 The low byte of *counts* is the number of values before the list value, the
503 high byte of *counts* the number of values after it. The resulting values
504 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000505
Georg Brandl5ac22302008-07-20 21:39:03 +0000506
Georg Brandl116aa622007-08-15 14:28:22 +0000507.. opcode:: DUP_TOPX (count)
508
509 Duplicate *count* items, keeping them in the same order. Due to implementation
510 limits, *count* should be between 1 and 5 inclusive.
511
512
513.. opcode:: STORE_ATTR (namei)
514
515 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
516 :attr:`co_names`.
517
518
519.. opcode:: DELETE_ATTR (namei)
520
521 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
522
523
524.. opcode:: STORE_GLOBAL (namei)
525
526 Works as ``STORE_NAME``, but stores the name as a global.
527
528
529.. opcode:: DELETE_GLOBAL (namei)
530
531 Works as ``DELETE_NAME``, but deletes a global name.
532
Georg Brandl116aa622007-08-15 14:28:22 +0000533
534.. opcode:: LOAD_CONST (consti)
535
536 Pushes ``co_consts[consti]`` onto the stack.
537
538
539.. opcode:: LOAD_NAME (namei)
540
541 Pushes the value associated with ``co_names[namei]`` onto the stack.
542
543
544.. opcode:: BUILD_TUPLE (count)
545
546 Creates a tuple consuming *count* items from the stack, and pushes the resulting
547 tuple onto the stack.
548
549
550.. opcode:: BUILD_LIST (count)
551
552 Works as ``BUILD_TUPLE``, but creates a list.
553
554
555.. opcode:: BUILD_SET (count)
556
557 Works as ``BUILD_TUPLE``, but creates a set.
558
559
Christian Heimesa62da1d2008-01-12 19:39:10 +0000560.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000561
Christian Heimesa62da1d2008-01-12 19:39:10 +0000562 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
563 to hold *count* entries.
Georg Brandl116aa622007-08-15 14:28:22 +0000564
565
566.. opcode:: LOAD_ATTR (namei)
567
568 Replaces TOS with ``getattr(TOS, co_names[namei])``.
569
570
571.. opcode:: COMPARE_OP (opname)
572
573 Performs a Boolean operation. The operation name can be found in
574 ``cmp_op[opname]``.
575
576
577.. opcode:: IMPORT_NAME (namei)
578
Christian Heimesa342c012008-04-20 21:01:16 +0000579 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
580 the *fromlist* and *level* arguments of :func:`__import__`. The module
581 object is pushed onto the stack. The current namespace is not affected:
582 for a proper import statement, a subsequent ``STORE_FAST`` instruction
583 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000584
585
586.. opcode:: IMPORT_FROM (namei)
587
588 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
589 resulting object is pushed onto the stack, to be subsequently stored by a
590 ``STORE_FAST`` instruction.
591
592
593.. opcode:: JUMP_FORWARD (delta)
594
Georg Brandl9afde1c2007-11-01 20:32:30 +0000595 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000596
597
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000598.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000599
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000600 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000601
602
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000603.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000604
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000605 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
606
607
608.. opcode:: JUMP_IF_TRUE_OR_POP (target)
609
610 If TOS is true, sets the bytecode counter to *target* and leaves TOS
611 on the stack. Otherwise (TOS is false), TOS is popped.
612
613
614.. opcode:: JUMP_IF_FALSE_OR_POP (target)
615
616 If TOS is false, sets the bytecode counter to *target* and leaves
617 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000618
619
620.. opcode:: JUMP_ABSOLUTE (target)
621
Georg Brandl9afde1c2007-11-01 20:32:30 +0000622 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000623
624
625.. opcode:: FOR_ITER (delta)
626
Georg Brandl9afde1c2007-11-01 20:32:30 +0000627 ``TOS`` is an :term:`iterator`. Call its :meth:`__next__` method. If this
628 yields a new value, push it on the stack (leaving the iterator below it). If
629 the iterator indicates it is exhausted ``TOS`` is popped, and the byte code
630 counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000631
Georg Brandl116aa622007-08-15 14:28:22 +0000632
633.. opcode:: LOAD_GLOBAL (namei)
634
635 Loads the global named ``co_names[namei]`` onto the stack.
636
Georg Brandl116aa622007-08-15 14:28:22 +0000637
638.. opcode:: SETUP_LOOP (delta)
639
640 Pushes a block for a loop onto the block stack. The block spans from the
641 current instruction with a size of *delta* bytes.
642
643
644.. opcode:: SETUP_EXCEPT (delta)
645
646 Pushes a try block from a try-except clause onto the block stack. *delta* points
647 to the first except block.
648
649
650.. opcode:: SETUP_FINALLY (delta)
651
652 Pushes a try block from a try-except clause onto the block stack. *delta* points
653 to the finally block.
654
Christian Heimesa62da1d2008-01-12 19:39:10 +0000655.. opcode:: STORE_MAP ()
656
657 Store a key and value pair in a dictionary. Pops the key and value while leaving
658 the dictionary on the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000659
660.. opcode:: LOAD_FAST (var_num)
661
662 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
663
664
665.. opcode:: STORE_FAST (var_num)
666
667 Stores TOS into the local ``co_varnames[var_num]``.
668
669
670.. opcode:: DELETE_FAST (var_num)
671
672 Deletes local ``co_varnames[var_num]``.
673
674
675.. opcode:: LOAD_CLOSURE (i)
676
677 Pushes a reference to the cell contained in slot *i* of the cell and free
678 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
679 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
680 len(co_cellvars)]``.
681
682
683.. opcode:: LOAD_DEREF (i)
684
685 Loads the cell contained in slot *i* of the cell and free variable storage.
686 Pushes a reference to the object the cell contains on the stack.
687
688
689.. opcode:: STORE_DEREF (i)
690
691 Stores TOS into the cell contained in slot *i* of the cell and free variable
692 storage.
693
694
695.. opcode:: SET_LINENO (lineno)
696
697 This opcode is obsolete.
698
699
700.. opcode:: RAISE_VARARGS (argc)
701
702 Raises an exception. *argc* indicates the number of parameters to the raise
703 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
704 the parameter as TOS1, and the exception as TOS.
705
706
707.. opcode:: CALL_FUNCTION (argc)
708
709 Calls a function. The low byte of *argc* indicates the number of positional
710 parameters, the high byte the number of keyword parameters. On the stack, the
711 opcode finds the keyword parameters first. For each keyword argument, the value
712 is on top of the key. Below the keyword parameters, the positional parameters
713 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl48310cd2009-01-03 21:18:54 +0000714 the function object to call is on the stack. Pops all function arguments, and
Benjamin Peterson206e3072008-10-19 14:07:49 +0000715 the function itself off the stack, and pushes the return value.
Georg Brandl116aa622007-08-15 14:28:22 +0000716
717
718.. opcode:: MAKE_FUNCTION (argc)
719
720 Pushes a new function object on the stack. TOS is the code associated with the
721 function. The function object is defined to have *argc* default parameters,
722 which are found below TOS.
723
724
725.. opcode:: MAKE_CLOSURE (argc)
726
Guido van Rossum04110fb2007-08-24 16:32:05 +0000727 Creates a new function object, sets its *__closure__* slot, and pushes it on
728 the stack. TOS is the code associated with the function, TOS1 the tuple
729 containing cells for the closure's free variables. The function also has
730 *argc* default parameters, which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000731
732
733.. opcode:: BUILD_SLICE (argc)
734
735 .. index:: builtin: slice
736
737 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
738 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000739 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000740
741
742.. opcode:: EXTENDED_ARG (ext)
743
744 Prefixes any opcode which has an argument too big to fit into the default two
745 bytes. *ext* holds two additional bytes which, taken together with the
746 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
747 most-significant bytes.
748
749
750.. opcode:: CALL_FUNCTION_VAR (argc)
751
752 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
753 on the stack contains the variable argument list, followed by keyword and
754 positional arguments.
755
756
757.. opcode:: CALL_FUNCTION_KW (argc)
758
759 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
760 on the stack contains the keyword arguments dictionary, followed by explicit
761 keyword and positional arguments.
762
763
764.. opcode:: CALL_FUNCTION_VAR_KW (argc)
765
766 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
767 element on the stack contains the keyword arguments dictionary, followed by the
768 variable-arguments tuple, followed by explicit keyword and positional arguments.
769
770
771.. opcode:: HAVE_ARGUMENT ()
772
773 This is not really an opcode. It identifies the dividing line between opcodes
774 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
775 HAVE_ARGUMENT``.
776