blob: 6871d7e9fc2412da79bacfe507e73b71a64d64fe [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
Benjamin Peterson1880d8b2009-05-25 13:13:44 +0000535.. opcode:: SETUP_WITH (delta)
536
537 This opcode performs several operations before a with block starts. First,
538 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
539 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
540 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
541 is pushed. Finally, the result of calling the enter method is pushed onto
542 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
543 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
544 :opcode:`UNPACK_SEQUENCE`).
545
546
Georg Brandl4debd552007-08-23 17:54:11 +0000547.. opcode:: WITH_CLEANUP ()
548
Nick Coghlan7af53be2008-03-07 14:13:28 +0000549 Cleans up the stack when a :keyword:`with` statement block exits. On top of
550 the stack are 1--3 values indicating how/why the finally clause was entered:
Georg Brandl4debd552007-08-23 17:54:11 +0000551
Nick Coghlan7af53be2008-03-07 14:13:28 +0000552 * TOP = ``None``
553 * (TOP, SECOND) = (``WHY_{RETURN,CONTINUE}``), retval
554 * TOP = ``WHY_*``; no retval below it
555 * (TOP, SECOND, THIRD) = exc_info()
Georg Brandl4debd552007-08-23 17:54:11 +0000556
Nick Coghlan7af53be2008-03-07 14:13:28 +0000557 Under them is EXIT, the context manager's :meth:`__exit__` bound method.
Georg Brandl4debd552007-08-23 17:54:11 +0000558
Nick Coghlan7af53be2008-03-07 14:13:28 +0000559 In the last case, ``EXIT(TOP, SECOND, THIRD)`` is called, otherwise
560 ``EXIT(None, None, None)``.
561
562 EXIT is removed from the stack, leaving the values above it in the same
563 order. In addition, if the stack represents an exception, *and* the function
564 call returns a 'true' value, this information is "zapped", to prevent
565 ``END_FINALLY`` from re-raising the exception. (But non-local gotos should
566 still be resumed.)
Georg Brandl4debd552007-08-23 17:54:11 +0000567
Georg Brandlff27e0c2007-10-20 13:22:53 +0000568 .. XXX explain the WHY stuff!
569
Georg Brandl4debd552007-08-23 17:54:11 +0000570
Georg Brandl8ec7f652007-08-15 14:28:01 +0000571All of the following opcodes expect arguments. An argument is two bytes, with
572the more significant byte last.
573
Georg Brandl8ec7f652007-08-15 14:28:01 +0000574.. opcode:: STORE_NAME (namei)
575
576 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Georg Brandl59724932008-02-23 15:43:48 +0000577 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl8ec7f652007-08-15 14:28:01 +0000578 or ``STORE_GLOBAL`` if possible.
579
580
581.. opcode:: DELETE_NAME (namei)
582
583 Implements ``del name``, where *namei* is the index into :attr:`co_names`
584 attribute of the code object.
585
586
587.. opcode:: UNPACK_SEQUENCE (count)
588
589 Unpacks TOS into *count* individual values, which are put onto the stack
590 right-to-left.
591
Georg Brandl8ec7f652007-08-15 14:28:01 +0000592
593.. opcode:: DUP_TOPX (count)
594
595 Duplicate *count* items, keeping them in the same order. Due to implementation
596 limits, *count* should be between 1 and 5 inclusive.
597
598
599.. opcode:: STORE_ATTR (namei)
600
601 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
602 :attr:`co_names`.
603
604
605.. opcode:: DELETE_ATTR (namei)
606
607 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
608
609
610.. opcode:: STORE_GLOBAL (namei)
611
612 Works as ``STORE_NAME``, but stores the name as a global.
613
614
615.. opcode:: DELETE_GLOBAL (namei)
616
617 Works as ``DELETE_NAME``, but deletes a global name.
618
Georg Brandl8ec7f652007-08-15 14:28:01 +0000619
620.. opcode:: LOAD_CONST (consti)
621
622 Pushes ``co_consts[consti]`` onto the stack.
623
624
625.. opcode:: LOAD_NAME (namei)
626
627 Pushes the value associated with ``co_names[namei]`` onto the stack.
628
629
630.. opcode:: BUILD_TUPLE (count)
631
632 Creates a tuple consuming *count* items from the stack, and pushes the resulting
633 tuple onto the stack.
634
635
636.. opcode:: BUILD_LIST (count)
637
638 Works as ``BUILD_TUPLE``, but creates a list.
639
640
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000641.. opcode:: BUILD_MAP (count)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000642
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000643 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
644 to hold *count* entries.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000645
646
647.. opcode:: LOAD_ATTR (namei)
648
649 Replaces TOS with ``getattr(TOS, co_names[namei])``.
650
651
652.. opcode:: COMPARE_OP (opname)
653
654 Performs a Boolean operation. The operation name can be found in
655 ``cmp_op[opname]``.
656
657
658.. opcode:: IMPORT_NAME (namei)
659
Georg Brandl2fb8a532008-04-19 16:59:16 +0000660 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
661 the *fromlist* and *level* arguments of :func:`__import__`. The module
662 object is pushed onto the stack. The current namespace is not affected:
663 for a proper import statement, a subsequent ``STORE_FAST`` instruction
664 modifies the namespace.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000665
666
667.. opcode:: IMPORT_FROM (namei)
668
669 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
670 resulting object is pushed onto the stack, to be subsequently stored by a
671 ``STORE_FAST`` instruction.
672
673
674.. opcode:: JUMP_FORWARD (delta)
675
Georg Brandl63fa1682007-10-21 10:24:20 +0000676 Increments bytecode counter by *delta*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000677
678
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000679.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000680
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000681 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000682
683
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000684.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000685
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000686 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
687
688
689.. opcode:: JUMP_IF_TRUE_OR_POP (target)
690
691 If TOS is true, sets the bytecode counter to *target* and leaves TOS
692 on the stack. Otherwise (TOS is false), TOS is popped.
693
694
695.. opcode:: JUMP_IF_FALSE_OR_POP (target)
696
697 If TOS is false, sets the bytecode counter to *target* and leaves
698 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000699
700
701.. opcode:: JUMP_ABSOLUTE (target)
702
Georg Brandl63fa1682007-10-21 10:24:20 +0000703 Set bytecode counter to *target*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000704
705
706.. opcode:: FOR_ITER (delta)
707
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000708 ``TOS`` is an :term:`iterator`. Call its :meth:`!next` method. If this
Georg Brandle7a09902007-10-21 12:10:28 +0000709 yields a new value, push it on the stack (leaving the iterator below it). If
710 the iterator indicates it is exhausted ``TOS`` is popped, and the bytecode
711 counter is incremented by *delta*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000712
Georg Brandl8ec7f652007-08-15 14:28:01 +0000713
714.. opcode:: LOAD_GLOBAL (namei)
715
716 Loads the global named ``co_names[namei]`` onto the stack.
717
Georg Brandl8ec7f652007-08-15 14:28:01 +0000718
719.. opcode:: SETUP_LOOP (delta)
720
721 Pushes a block for a loop onto the block stack. The block spans from the
722 current instruction with a size of *delta* bytes.
723
724
725.. opcode:: SETUP_EXCEPT (delta)
726
727 Pushes a try block from a try-except clause onto the block stack. *delta* points
728 to the first except block.
729
730
731.. opcode:: SETUP_FINALLY (delta)
732
733 Pushes a try block from a try-except clause onto the block stack. *delta* points
734 to the finally block.
735
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000736.. opcode:: STORE_MAP ()
737
738 Store a key and value pair in a dictionary. Pops the key and value while leaving
739 the dictionary on the stack.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000740
741.. opcode:: LOAD_FAST (var_num)
742
743 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
744
745
746.. opcode:: STORE_FAST (var_num)
747
748 Stores TOS into the local ``co_varnames[var_num]``.
749
750
751.. opcode:: DELETE_FAST (var_num)
752
753 Deletes local ``co_varnames[var_num]``.
754
755
756.. opcode:: LOAD_CLOSURE (i)
757
758 Pushes a reference to the cell contained in slot *i* of the cell and free
759 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
760 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
761 len(co_cellvars)]``.
762
763
764.. opcode:: LOAD_DEREF (i)
765
766 Loads the cell contained in slot *i* of the cell and free variable storage.
767 Pushes a reference to the object the cell contains on the stack.
768
769
770.. opcode:: STORE_DEREF (i)
771
772 Stores TOS into the cell contained in slot *i* of the cell and free variable
773 storage.
774
775
776.. opcode:: SET_LINENO (lineno)
777
778 This opcode is obsolete.
779
780
781.. opcode:: RAISE_VARARGS (argc)
782
783 Raises an exception. *argc* indicates the number of parameters to the raise
784 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
785 the parameter as TOS1, and the exception as TOS.
786
787
788.. opcode:: CALL_FUNCTION (argc)
789
790 Calls a function. The low byte of *argc* indicates the number of positional
791 parameters, the high byte the number of keyword parameters. On the stack, the
792 opcode finds the keyword parameters first. For each keyword argument, the value
793 is on top of the key. Below the keyword parameters, the positional parameters
794 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000795 the function object to call is on the stack. Pops all function arguments, and
Benjamin Petersonc2f14402008-10-17 20:01:01 +0000796 the function itself off the stack, and pushes the return value.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000797
798
799.. opcode:: MAKE_FUNCTION (argc)
800
801 Pushes a new function object on the stack. TOS is the code associated with the
802 function. The function object is defined to have *argc* default parameters,
803 which are found below TOS.
804
805
806.. opcode:: MAKE_CLOSURE (argc)
807
808 Creates a new function object, sets its *func_closure* slot, and pushes it on
Georg Brandl4debd552007-08-23 17:54:11 +0000809 the stack. TOS is the code associated with the function, TOS1 the tuple
810 containing cells for the closure's free variables. The function also has
811 *argc* default parameters, which are found below the cells.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000812
813
814.. opcode:: BUILD_SLICE (argc)
815
816 .. index:: builtin: slice
817
818 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
819 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Georg Brandlb19be572007-12-29 10:57:00 +0000820 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000821
822
823.. opcode:: EXTENDED_ARG (ext)
824
825 Prefixes any opcode which has an argument too big to fit into the default two
826 bytes. *ext* holds two additional bytes which, taken together with the
827 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
828 most-significant bytes.
829
830
831.. opcode:: CALL_FUNCTION_VAR (argc)
832
833 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
834 on the stack contains the variable argument list, followed by keyword and
835 positional arguments.
836
837
838.. opcode:: CALL_FUNCTION_KW (argc)
839
840 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
841 on the stack contains the keyword arguments dictionary, followed by explicit
842 keyword and positional arguments.
843
844
845.. opcode:: CALL_FUNCTION_VAR_KW (argc)
846
847 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
848 element on the stack contains the keyword arguments dictionary, followed by the
849 variable-arguments tuple, followed by explicit keyword and positional arguments.
850
851
852.. opcode:: HAVE_ARGUMENT ()
853
854 This is not really an opcode. It identifies the dividing line between opcodes
855 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
856 HAVE_ARGUMENT``.
857