blob: 3dd528b23df015fd81c82a18ba18e691dae8f2c1 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
Georg Brandl63fa1682007-10-21 10:24:20 +00002:mod:`dis` --- Disassembler for Python bytecode
3===============================================
Georg Brandl8ec7f652007-08-15 14:28:01 +00004
5.. module:: dis
Georg Brandl63fa1682007-10-21 10:24:20 +00006 :synopsis: Disassembler for Python bytecode.
Georg Brandl8ec7f652007-08-15 14:28:01 +00007
8
Georg Brandl63fa1682007-10-21 10:24:20 +00009The :mod:`dis` module supports the analysis of Python :term:`bytecode` by disassembling
Georg Brandl8ec7f652007-08-15 14:28:01 +000010it. Since there is no Python assembler, this module defines the Python assembly
Georg Brandl63fa1682007-10-21 10:24:20 +000011language. The Python bytecode which this module takes as an input is defined
Georg Brandl8ec7f652007-08-15 14:28:01 +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 Brandl63fa1682007-10-21 10:24:20 +000038 sequence, it prints one line per bytecode instruction. If no object is
Georg Brandl8ec7f652007-08-15 14:28:01 +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
Georg Brandl775c3072009-01-01 12:09:40 +000067 A synonym for :func:`disassemble`. It is more convenient to type, and kept
68 for compatibility with earlier Python releases.
Georg Brandl8ec7f652007-08-15 14:28:01 +000069
70
Georg Brandl775c3072009-01-01 12:09:40 +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 Brandlc62ef8b2009-01-03 20:55:06 +000082
83
Georg Brandl8ec7f652007-08-15 14:28:01 +000084.. data:: opname
85
Georg Brandl63fa1682007-10-21 10:24:20 +000086 Sequence of operation names, indexable using the bytecode.
Georg Brandl8ec7f652007-08-15 14:28:01 +000087
88
89.. data:: opmap
90
Georg Brandl63fa1682007-10-21 10:24:20 +000091 Dictionary mapping bytecodes to operation names.
Georg Brandl8ec7f652007-08-15 14:28:01 +000092
93
94.. data:: cmp_op
95
96 Sequence of all compare operation names.
97
98
99.. data:: hasconst
100
Georg Brandl63fa1682007-10-21 10:24:20 +0000101 Sequence of bytecodes that have a constant parameter.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000102
103
104.. data:: hasfree
105
Georg Brandl63fa1682007-10-21 10:24:20 +0000106 Sequence of bytecodes that access a free variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000107
108
109.. data:: hasname
110
Georg Brandl63fa1682007-10-21 10:24:20 +0000111 Sequence of bytecodes that access an attribute by name.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000112
113
114.. data:: hasjrel
115
Georg Brandl63fa1682007-10-21 10:24:20 +0000116 Sequence of bytecodes that have a relative jump target.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000117
118
119.. data:: hasjabs
120
Georg Brandl63fa1682007-10-21 10:24:20 +0000121 Sequence of bytecodes that have an absolute jump target.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000122
123
124.. data:: haslocal
125
Georg Brandl63fa1682007-10-21 10:24:20 +0000126 Sequence of bytecodes that access a local variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000127
128
129.. data:: hascompare
130
Georg Brandl63fa1682007-10-21 10:24:20 +0000131 Sequence of bytecodes of Boolean operations.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000132
133
134.. _bytecodes:
135
Georg Brandl63fa1682007-10-21 10:24:20 +0000136Python Bytecode Instructions
137----------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000138
Georg Brandl63fa1682007-10-21 10:24:20 +0000139The Python compiler currently generates the following bytecode instructions.
Georg Brandl8ec7f652007-08-15 14:28:01 +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_CONVERT ()
198
199 Implements ``TOS = `TOS```.
200
201
202.. opcode:: UNARY_INVERT ()
203
204 Implements ``TOS = ~TOS``.
205
206
207.. opcode:: GET_ITER ()
208
209 Implements ``TOS = iter(TOS)``.
210
211Binary operations remove the top of the stack (TOS) and the second top-most
212stack item (TOS1) from the stack. They perform the operation, and put the
213result back on the stack.
214
215
216.. opcode:: BINARY_POWER ()
217
218 Implements ``TOS = TOS1 ** TOS``.
219
220
221.. opcode:: BINARY_MULTIPLY ()
222
223 Implements ``TOS = TOS1 * TOS``.
224
225
226.. opcode:: BINARY_DIVIDE ()
227
228 Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is not
229 in effect.
230
231
232.. opcode:: BINARY_FLOOR_DIVIDE ()
233
234 Implements ``TOS = TOS1 // TOS``.
235
236
237.. opcode:: BINARY_TRUE_DIVIDE ()
238
239 Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is in
240 effect.
241
242
243.. opcode:: BINARY_MODULO ()
244
245 Implements ``TOS = TOS1 % TOS``.
246
247
248.. opcode:: BINARY_ADD ()
249
250 Implements ``TOS = TOS1 + TOS``.
251
252
253.. opcode:: BINARY_SUBTRACT ()
254
255 Implements ``TOS = TOS1 - TOS``.
256
257
258.. opcode:: BINARY_SUBSCR ()
259
260 Implements ``TOS = TOS1[TOS]``.
261
262
263.. opcode:: BINARY_LSHIFT ()
264
265 Implements ``TOS = TOS1 << TOS``.
266
267
268.. opcode:: BINARY_RSHIFT ()
269
270 Implements ``TOS = TOS1 >> TOS``.
271
272
273.. opcode:: BINARY_AND ()
274
275 Implements ``TOS = TOS1 & TOS``.
276
277
278.. opcode:: BINARY_XOR ()
279
280 Implements ``TOS = TOS1 ^ TOS``.
281
282
283.. opcode:: BINARY_OR ()
284
285 Implements ``TOS = TOS1 | TOS``.
286
287In-place operations are like binary operations, in that they remove TOS and
288TOS1, and push the result back on the stack, but the operation is done in-place
289when TOS1 supports it, and the resulting TOS may be (but does not have to be)
290the original TOS1.
291
292
293.. opcode:: INPLACE_POWER ()
294
295 Implements in-place ``TOS = TOS1 ** TOS``.
296
297
298.. opcode:: INPLACE_MULTIPLY ()
299
300 Implements in-place ``TOS = TOS1 * TOS``.
301
302
303.. opcode:: INPLACE_DIVIDE ()
304
305 Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
306 division`` is not in effect.
307
308
309.. opcode:: INPLACE_FLOOR_DIVIDE ()
310
311 Implements in-place ``TOS = TOS1 // TOS``.
312
313
314.. opcode:: INPLACE_TRUE_DIVIDE ()
315
316 Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
317 division`` is in effect.
318
319
320.. opcode:: INPLACE_MODULO ()
321
322 Implements in-place ``TOS = TOS1 % TOS``.
323
324
325.. opcode:: INPLACE_ADD ()
326
327 Implements in-place ``TOS = TOS1 + TOS``.
328
329
330.. opcode:: INPLACE_SUBTRACT ()
331
332 Implements in-place ``TOS = TOS1 - TOS``.
333
334
335.. opcode:: INPLACE_LSHIFT ()
336
337 Implements in-place ``TOS = TOS1 << TOS``.
338
339
340.. opcode:: INPLACE_RSHIFT ()
341
342 Implements in-place ``TOS = TOS1 >> TOS``.
343
344
345.. opcode:: INPLACE_AND ()
346
347 Implements in-place ``TOS = TOS1 & TOS``.
348
349
350.. opcode:: INPLACE_XOR ()
351
352 Implements in-place ``TOS = TOS1 ^ TOS``.
353
354
355.. opcode:: INPLACE_OR ()
356
357 Implements in-place ``TOS = TOS1 | TOS``.
358
359The slice opcodes take up to three parameters.
360
361
362.. opcode:: SLICE+0 ()
363
364 Implements ``TOS = TOS[:]``.
365
366
367.. opcode:: SLICE+1 ()
368
369 Implements ``TOS = TOS1[TOS:]``.
370
371
372.. opcode:: SLICE+2 ()
373
374 Implements ``TOS = TOS1[:TOS]``.
375
376
377.. opcode:: SLICE+3 ()
378
379 Implements ``TOS = TOS2[TOS1:TOS]``.
380
381Slice assignment needs even an additional parameter. As any statement, they put
382nothing on the stack.
383
384
385.. opcode:: STORE_SLICE+0 ()
386
387 Implements ``TOS[:] = TOS1``.
388
389
390.. opcode:: STORE_SLICE+1 ()
391
392 Implements ``TOS1[TOS:] = TOS2``.
393
394
395.. opcode:: STORE_SLICE+2 ()
396
397 Implements ``TOS1[:TOS] = TOS2``.
398
399
400.. opcode:: STORE_SLICE+3 ()
401
402 Implements ``TOS2[TOS1:TOS] = TOS3``.
403
404
405.. opcode:: DELETE_SLICE+0 ()
406
407 Implements ``del TOS[:]``.
408
409
410.. opcode:: DELETE_SLICE+1 ()
411
412 Implements ``del TOS1[TOS:]``.
413
414
415.. opcode:: DELETE_SLICE+2 ()
416
417 Implements ``del TOS1[:TOS]``.
418
419
420.. opcode:: DELETE_SLICE+3 ()
421
422 Implements ``del TOS2[TOS1:TOS]``.
423
424
425.. opcode:: STORE_SUBSCR ()
426
427 Implements ``TOS1[TOS] = TOS2``.
428
429
430.. opcode:: DELETE_SUBSCR ()
431
432 Implements ``del TOS1[TOS]``.
433
434Miscellaneous opcodes.
435
436
437.. opcode:: PRINT_EXPR ()
438
439 Implements the expression statement for the interactive mode. TOS is removed
440 from the stack and printed. In non-interactive mode, an expression statement is
441 terminated with ``POP_STACK``.
442
443
444.. opcode:: PRINT_ITEM ()
445
446 Prints TOS to the file-like object bound to ``sys.stdout``. There is one such
447 instruction for each item in the :keyword:`print` statement.
448
449
450.. opcode:: PRINT_ITEM_TO ()
451
452 Like ``PRINT_ITEM``, but prints the item second from TOS to the file-like object
453 at TOS. This is used by the extended print statement.
454
455
456.. opcode:: PRINT_NEWLINE ()
457
458 Prints a new line on ``sys.stdout``. This is generated as the last operation of
459 a :keyword:`print` statement, unless the statement ends with a comma.
460
461
462.. opcode:: PRINT_NEWLINE_TO ()
463
464 Like ``PRINT_NEWLINE``, but prints the new line on the file-like object on the
465 TOS. This is used by the extended print statement.
466
467
468.. opcode:: BREAK_LOOP ()
469
470 Terminates a loop due to a :keyword:`break` statement.
471
472
473.. opcode:: CONTINUE_LOOP (target)
474
475 Continues a loop due to a :keyword:`continue` statement. *target* is the
476 address to jump to (which should be a ``FOR_ITER`` instruction).
477
478
Antoine Pitroud0c35152008-12-17 00:38:28 +0000479.. opcode:: LIST_APPEND (i)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000480
Antoine Pitroud0c35152008-12-17 00:38:28 +0000481 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
482 While the appended value is popped off, the list object remains on the
483 stack so that it is available for further iterations of the loop.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000484
485
486.. opcode:: LOAD_LOCALS ()
487
488 Pushes a reference to the locals of the current scope on the stack. This is used
489 in the code for a class definition: After the class body is evaluated, the
490 locals are passed to the class definition.
491
492
493.. opcode:: RETURN_VALUE ()
494
495 Returns with TOS to the caller of the function.
496
497
498.. opcode:: YIELD_VALUE ()
499
Georg Brandlcf3fb252007-10-21 10:52:38 +0000500 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000501
502
503.. opcode:: IMPORT_STAR ()
504
505 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
506 local namespace. The module is popped after loading all names. This opcode
507 implements ``from module import *``.
508
509
510.. opcode:: EXEC_STMT ()
511
512 Implements ``exec TOS2,TOS1,TOS``. The compiler fills missing optional
513 parameters with ``None``.
514
515
516.. opcode:: POP_BLOCK ()
517
518 Removes one block from the block stack. Per frame, there is a stack of blocks,
519 denoting nested loops, try statements, and such.
520
521
522.. opcode:: END_FINALLY ()
523
524 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
525 exception has to be re-raised, or whether the function returns, and continues
526 with the outer-next block.
527
528
529.. opcode:: BUILD_CLASS ()
530
531 Creates a new class object. TOS is the methods dictionary, TOS1 the tuple of
532 the names of the base classes, and TOS2 the class name.
533
Georg Brandl4debd552007-08-23 17:54:11 +0000534
535.. opcode:: WITH_CLEANUP ()
536
Nick Coghlan7af53be2008-03-07 14:13:28 +0000537 Cleans up the stack when a :keyword:`with` statement block exits. On top of
538 the stack are 1--3 values indicating how/why the finally clause was entered:
Georg Brandl4debd552007-08-23 17:54:11 +0000539
Nick Coghlan7af53be2008-03-07 14:13:28 +0000540 * TOP = ``None``
541 * (TOP, SECOND) = (``WHY_{RETURN,CONTINUE}``), retval
542 * TOP = ``WHY_*``; no retval below it
543 * (TOP, SECOND, THIRD) = exc_info()
Georg Brandl4debd552007-08-23 17:54:11 +0000544
Nick Coghlan7af53be2008-03-07 14:13:28 +0000545 Under them is EXIT, the context manager's :meth:`__exit__` bound method.
Georg Brandl4debd552007-08-23 17:54:11 +0000546
Nick Coghlan7af53be2008-03-07 14:13:28 +0000547 In the last case, ``EXIT(TOP, SECOND, THIRD)`` is called, otherwise
548 ``EXIT(None, None, None)``.
549
550 EXIT is removed from the stack, leaving the values above it in the same
551 order. In addition, if the stack represents an exception, *and* the function
552 call returns a 'true' value, this information is "zapped", to prevent
553 ``END_FINALLY`` from re-raising the exception. (But non-local gotos should
554 still be resumed.)
Georg Brandl4debd552007-08-23 17:54:11 +0000555
Georg Brandlff27e0c2007-10-20 13:22:53 +0000556 .. XXX explain the WHY stuff!
557
Georg Brandl4debd552007-08-23 17:54:11 +0000558
Georg Brandl8ec7f652007-08-15 14:28:01 +0000559All of the following opcodes expect arguments. An argument is two bytes, with
560the more significant byte last.
561
Georg Brandl8ec7f652007-08-15 14:28:01 +0000562.. opcode:: STORE_NAME (namei)
563
564 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Georg Brandl59724932008-02-23 15:43:48 +0000565 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl8ec7f652007-08-15 14:28:01 +0000566 or ``STORE_GLOBAL`` if possible.
567
568
569.. opcode:: DELETE_NAME (namei)
570
571 Implements ``del name``, where *namei* is the index into :attr:`co_names`
572 attribute of the code object.
573
574
575.. opcode:: UNPACK_SEQUENCE (count)
576
577 Unpacks TOS into *count* individual values, which are put onto the stack
578 right-to-left.
579
Georg Brandl8ec7f652007-08-15 14:28:01 +0000580
581.. opcode:: DUP_TOPX (count)
582
583 Duplicate *count* items, keeping them in the same order. Due to implementation
584 limits, *count* should be between 1 and 5 inclusive.
585
586
587.. opcode:: STORE_ATTR (namei)
588
589 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
590 :attr:`co_names`.
591
592
593.. opcode:: DELETE_ATTR (namei)
594
595 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
596
597
598.. opcode:: STORE_GLOBAL (namei)
599
600 Works as ``STORE_NAME``, but stores the name as a global.
601
602
603.. opcode:: DELETE_GLOBAL (namei)
604
605 Works as ``DELETE_NAME``, but deletes a global name.
606
Georg Brandl8ec7f652007-08-15 14:28:01 +0000607
608.. opcode:: LOAD_CONST (consti)
609
610 Pushes ``co_consts[consti]`` onto the stack.
611
612
613.. opcode:: LOAD_NAME (namei)
614
615 Pushes the value associated with ``co_names[namei]`` onto the stack.
616
617
618.. opcode:: BUILD_TUPLE (count)
619
620 Creates a tuple consuming *count* items from the stack, and pushes the resulting
621 tuple onto the stack.
622
623
624.. opcode:: BUILD_LIST (count)
625
626 Works as ``BUILD_TUPLE``, but creates a list.
627
628
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000629.. opcode:: BUILD_MAP (count)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000630
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000631 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
632 to hold *count* entries.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000633
634
635.. opcode:: LOAD_ATTR (namei)
636
637 Replaces TOS with ``getattr(TOS, co_names[namei])``.
638
639
640.. opcode:: COMPARE_OP (opname)
641
642 Performs a Boolean operation. The operation name can be found in
643 ``cmp_op[opname]``.
644
645
646.. opcode:: IMPORT_NAME (namei)
647
Georg Brandl2fb8a532008-04-19 16:59:16 +0000648 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
649 the *fromlist* and *level* arguments of :func:`__import__`. The module
650 object is pushed onto the stack. The current namespace is not affected:
651 for a proper import statement, a subsequent ``STORE_FAST`` instruction
652 modifies the namespace.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000653
654
655.. opcode:: IMPORT_FROM (namei)
656
657 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
658 resulting object is pushed onto the stack, to be subsequently stored by a
659 ``STORE_FAST`` instruction.
660
661
662.. opcode:: JUMP_FORWARD (delta)
663
Georg Brandl63fa1682007-10-21 10:24:20 +0000664 Increments bytecode counter by *delta*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000665
666
667.. opcode:: JUMP_IF_TRUE (delta)
668
Georg Brandl63fa1682007-10-21 10:24:20 +0000669 If TOS is true, increment the bytecode counter by *delta*. TOS is left on the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000670 stack.
671
672
673.. opcode:: JUMP_IF_FALSE (delta)
674
Georg Brandl63fa1682007-10-21 10:24:20 +0000675 If TOS is false, increment the bytecode counter by *delta*. TOS is not
Georg Brandl8ec7f652007-08-15 14:28:01 +0000676 changed.
677
678
679.. opcode:: JUMP_ABSOLUTE (target)
680
Georg Brandl63fa1682007-10-21 10:24:20 +0000681 Set bytecode counter to *target*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000682
683
684.. opcode:: FOR_ITER (delta)
685
Georg Brandle7a09902007-10-21 12:10:28 +0000686 ``TOS`` is an :term:`iterator`. Call its :meth:`next` method. If this
687 yields a new value, push it on the stack (leaving the iterator below it). If
688 the iterator indicates it is exhausted ``TOS`` is popped, and the bytecode
689 counter is incremented by *delta*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000690
Georg Brandl8ec7f652007-08-15 14:28:01 +0000691
692.. opcode:: LOAD_GLOBAL (namei)
693
694 Loads the global named ``co_names[namei]`` onto the stack.
695
Georg Brandl8ec7f652007-08-15 14:28:01 +0000696
697.. opcode:: SETUP_LOOP (delta)
698
699 Pushes a block for a loop onto the block stack. The block spans from the
700 current instruction with a size of *delta* bytes.
701
702
703.. opcode:: SETUP_EXCEPT (delta)
704
705 Pushes a try block from a try-except clause onto the block stack. *delta* points
706 to the first except block.
707
708
709.. opcode:: SETUP_FINALLY (delta)
710
711 Pushes a try block from a try-except clause onto the block stack. *delta* points
712 to the finally block.
713
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000714.. opcode:: STORE_MAP ()
715
716 Store a key and value pair in a dictionary. Pops the key and value while leaving
717 the dictionary on the stack.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000718
719.. opcode:: LOAD_FAST (var_num)
720
721 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
722
723
724.. opcode:: STORE_FAST (var_num)
725
726 Stores TOS into the local ``co_varnames[var_num]``.
727
728
729.. opcode:: DELETE_FAST (var_num)
730
731 Deletes local ``co_varnames[var_num]``.
732
733
734.. opcode:: LOAD_CLOSURE (i)
735
736 Pushes a reference to the cell contained in slot *i* of the cell and free
737 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
738 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
739 len(co_cellvars)]``.
740
741
742.. opcode:: LOAD_DEREF (i)
743
744 Loads the cell contained in slot *i* of the cell and free variable storage.
745 Pushes a reference to the object the cell contains on the stack.
746
747
748.. opcode:: STORE_DEREF (i)
749
750 Stores TOS into the cell contained in slot *i* of the cell and free variable
751 storage.
752
753
754.. opcode:: SET_LINENO (lineno)
755
756 This opcode is obsolete.
757
758
759.. opcode:: RAISE_VARARGS (argc)
760
761 Raises an exception. *argc* indicates the number of parameters to the raise
762 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
763 the parameter as TOS1, and the exception as TOS.
764
765
766.. opcode:: CALL_FUNCTION (argc)
767
768 Calls a function. The low byte of *argc* indicates the number of positional
769 parameters, the high byte the number of keyword parameters. On the stack, the
770 opcode finds the keyword parameters first. For each keyword argument, the value
771 is on top of the key. Below the keyword parameters, the positional parameters
772 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000773 the function object to call is on the stack. Pops all function arguments, and
Benjamin Petersonc2f14402008-10-17 20:01:01 +0000774 the function itself off the stack, and pushes the return value.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000775
776
777.. opcode:: MAKE_FUNCTION (argc)
778
779 Pushes a new function object on the stack. TOS is the code associated with the
780 function. The function object is defined to have *argc* default parameters,
781 which are found below TOS.
782
783
784.. opcode:: MAKE_CLOSURE (argc)
785
786 Creates a new function object, sets its *func_closure* slot, and pushes it on
Georg Brandl4debd552007-08-23 17:54:11 +0000787 the stack. TOS is the code associated with the function, TOS1 the tuple
788 containing cells for the closure's free variables. The function also has
789 *argc* default parameters, which are found below the cells.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000790
791
792.. opcode:: BUILD_SLICE (argc)
793
794 .. index:: builtin: slice
795
796 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
797 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Georg Brandlb19be572007-12-29 10:57:00 +0000798 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000799
800
801.. opcode:: EXTENDED_ARG (ext)
802
803 Prefixes any opcode which has an argument too big to fit into the default two
804 bytes. *ext* holds two additional bytes which, taken together with the
805 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
806 most-significant bytes.
807
808
809.. opcode:: CALL_FUNCTION_VAR (argc)
810
811 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
812 on the stack contains the variable argument list, followed by keyword and
813 positional arguments.
814
815
816.. opcode:: CALL_FUNCTION_KW (argc)
817
818 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
819 on the stack contains the keyword arguments dictionary, followed by explicit
820 keyword and positional arguments.
821
822
823.. opcode:: CALL_FUNCTION_VAR_KW (argc)
824
825 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
826 element on the stack contains the keyword arguments dictionary, followed by the
827 variable-arguments tuple, followed by explicit keyword and positional arguments.
828
829
830.. opcode:: HAVE_ARGUMENT ()
831
832 This is not really an opcode. It identifies the dividing line between opcodes
833 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
834 HAVE_ARGUMENT``.
835