blob: 9addfe740245d7e7bead53167ab973d801c02d50 [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
Brett Cannon8c4fa112010-07-21 09:52:10 +00009The :mod:`dis` module supports the analysis of CPython :term:`bytecode` by
10disassembling it. The CPython 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.
13
Raymond Hettingere0e08222010-11-06 07:10:31 +000014.. seealso::
15
16 Latest version of the `dis module Python source code
17 <http://svn.python.org/view/python/branches/release27-maint/Lib/dis.py?view=markup>`_
18
Brett Cannon8c4fa112010-07-21 09:52:10 +000019.. impl-detail::
20
21 Bytecode is an implementation detail of the CPython interpreter! No
22 guarantees are made that bytecode will not be added, removed, or changed
23 between versions of Python. Use of this module should not be considered to
24 work across Python VMs or Python releases.
Georg Brandl8ec7f652007-08-15 14:28:01 +000025
26Example: Given the function :func:`myfunc`::
27
28 def myfunc(alist):
29 return len(alist)
30
31the following command can be used to get the disassembly of :func:`myfunc`::
32
33 >>> dis.dis(myfunc)
34 2 0 LOAD_GLOBAL 0 (len)
35 3 LOAD_FAST 0 (alist)
36 6 CALL_FUNCTION 1
37 9 RETURN_VALUE
38
39(The "2" is a line number).
40
41The :mod:`dis` module defines the following functions and constants:
42
43
44.. function:: dis([bytesource])
45
46 Disassemble the *bytesource* object. *bytesource* can denote either a module, a
47 class, a method, a function, or a code object. For a module, it disassembles
48 all functions. For a class, it disassembles all methods. For a single code
Georg Brandl63fa1682007-10-21 10:24:20 +000049 sequence, it prints one line per bytecode instruction. If no object is
Georg Brandl8ec7f652007-08-15 14:28:01 +000050 provided, it disassembles the last traceback.
51
52
53.. function:: distb([tb])
54
55 Disassembles the top-of-stack function of a traceback, using the last traceback
56 if none was passed. The instruction causing the exception is indicated.
57
58
59.. function:: disassemble(code[, lasti])
60
61 Disassembles a code object, indicating the last instruction if *lasti* was
62 provided. The output is divided in the following columns:
63
64 #. the line number, for the first instruction of each line
65 #. the current instruction, indicated as ``-->``,
66 #. a labelled instruction, indicated with ``>>``,
67 #. the address of the instruction,
68 #. the operation code name,
69 #. operation parameters, and
70 #. interpretation of the parameters in parentheses.
71
72 The parameter interpretation recognizes local and global variable names,
73 constant values, branch targets, and compare operators.
74
75
76.. function:: disco(code[, lasti])
77
Georg Brandl775c3072009-01-01 12:09:40 +000078 A synonym for :func:`disassemble`. It is more convenient to type, and kept
79 for compatibility with earlier Python releases.
Georg Brandl8ec7f652007-08-15 14:28:01 +000080
81
Georg Brandl775c3072009-01-01 12:09:40 +000082.. function:: findlinestarts(code)
83
84 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
85 attributes of the code object *code* to find the offsets which are starts of
86 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
87
88
89.. function:: findlabels(code)
90
91 Detect all offsets in the code object *code* which are jump targets, and
92 return a list of these offsets.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000093
94
Georg Brandl8ec7f652007-08-15 14:28:01 +000095.. data:: opname
96
Georg Brandl63fa1682007-10-21 10:24:20 +000097 Sequence of operation names, indexable using the bytecode.
Georg Brandl8ec7f652007-08-15 14:28:01 +000098
99
100.. data:: opmap
101
Georg Brandlb8d0e362010-11-26 07:53:50 +0000102 Dictionary mapping operation names to bytecodes.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000103
104
105.. data:: cmp_op
106
107 Sequence of all compare operation names.
108
109
110.. data:: hasconst
111
Georg Brandl63fa1682007-10-21 10:24:20 +0000112 Sequence of bytecodes that have a constant parameter.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000113
114
115.. data:: hasfree
116
Georg Brandl63fa1682007-10-21 10:24:20 +0000117 Sequence of bytecodes that access a free variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000118
119
120.. data:: hasname
121
Georg Brandl63fa1682007-10-21 10:24:20 +0000122 Sequence of bytecodes that access an attribute by name.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000123
124
125.. data:: hasjrel
126
Georg Brandl63fa1682007-10-21 10:24:20 +0000127 Sequence of bytecodes that have a relative jump target.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000128
129
130.. data:: hasjabs
131
Georg Brandl63fa1682007-10-21 10:24:20 +0000132 Sequence of bytecodes that have an absolute jump target.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000133
134
135.. data:: haslocal
136
Georg Brandl63fa1682007-10-21 10:24:20 +0000137 Sequence of bytecodes that access a local variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000138
139
140.. data:: hascompare
141
Georg Brandl63fa1682007-10-21 10:24:20 +0000142 Sequence of bytecodes of Boolean operations.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000143
144
145.. _bytecodes:
146
Georg Brandl63fa1682007-10-21 10:24:20 +0000147Python Bytecode Instructions
148----------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000149
Georg Brandl63fa1682007-10-21 10:24:20 +0000150The Python compiler currently generates the following bytecode instructions.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000151
152
153.. opcode:: STOP_CODE ()
154
155 Indicates end-of-code to the compiler, not used by the interpreter.
156
157
158.. opcode:: NOP ()
159
160 Do nothing code. Used as a placeholder by the bytecode optimizer.
161
162
163.. opcode:: POP_TOP ()
164
165 Removes the top-of-stack (TOS) item.
166
167
168.. opcode:: ROT_TWO ()
169
170 Swaps the two top-most stack items.
171
172
173.. opcode:: ROT_THREE ()
174
175 Lifts second and third stack item one position up, moves top down to position
176 three.
177
178
179.. opcode:: ROT_FOUR ()
180
181 Lifts second, third and forth stack item one position up, moves top down to
182 position four.
183
184
185.. opcode:: DUP_TOP ()
186
187 Duplicates the reference on top of the stack.
188
189Unary Operations take the top of the stack, apply the operation, and push the
190result back on the stack.
191
192
193.. opcode:: UNARY_POSITIVE ()
194
195 Implements ``TOS = +TOS``.
196
197
198.. opcode:: UNARY_NEGATIVE ()
199
200 Implements ``TOS = -TOS``.
201
202
203.. opcode:: UNARY_NOT ()
204
205 Implements ``TOS = not TOS``.
206
207
208.. opcode:: UNARY_CONVERT ()
209
210 Implements ``TOS = `TOS```.
211
212
213.. opcode:: UNARY_INVERT ()
214
215 Implements ``TOS = ~TOS``.
216
217
218.. opcode:: GET_ITER ()
219
220 Implements ``TOS = iter(TOS)``.
221
222Binary operations remove the top of the stack (TOS) and the second top-most
223stack item (TOS1) from the stack. They perform the operation, and put the
224result back on the stack.
225
226
227.. opcode:: BINARY_POWER ()
228
229 Implements ``TOS = TOS1 ** TOS``.
230
231
232.. opcode:: BINARY_MULTIPLY ()
233
234 Implements ``TOS = TOS1 * TOS``.
235
236
237.. opcode:: BINARY_DIVIDE ()
238
239 Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is not
240 in effect.
241
242
243.. opcode:: BINARY_FLOOR_DIVIDE ()
244
245 Implements ``TOS = TOS1 // TOS``.
246
247
248.. opcode:: BINARY_TRUE_DIVIDE ()
249
250 Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is in
251 effect.
252
253
254.. opcode:: BINARY_MODULO ()
255
256 Implements ``TOS = TOS1 % TOS``.
257
258
259.. opcode:: BINARY_ADD ()
260
261 Implements ``TOS = TOS1 + TOS``.
262
263
264.. opcode:: BINARY_SUBTRACT ()
265
266 Implements ``TOS = TOS1 - TOS``.
267
268
269.. opcode:: BINARY_SUBSCR ()
270
271 Implements ``TOS = TOS1[TOS]``.
272
273
274.. opcode:: BINARY_LSHIFT ()
275
276 Implements ``TOS = TOS1 << TOS``.
277
278
279.. opcode:: BINARY_RSHIFT ()
280
281 Implements ``TOS = TOS1 >> TOS``.
282
283
284.. opcode:: BINARY_AND ()
285
286 Implements ``TOS = TOS1 & TOS``.
287
288
289.. opcode:: BINARY_XOR ()
290
291 Implements ``TOS = TOS1 ^ TOS``.
292
293
294.. opcode:: BINARY_OR ()
295
296 Implements ``TOS = TOS1 | TOS``.
297
298In-place operations are like binary operations, in that they remove TOS and
299TOS1, and push the result back on the stack, but the operation is done in-place
300when TOS1 supports it, and the resulting TOS may be (but does not have to be)
301the original TOS1.
302
303
304.. opcode:: INPLACE_POWER ()
305
306 Implements in-place ``TOS = TOS1 ** TOS``.
307
308
309.. opcode:: INPLACE_MULTIPLY ()
310
311 Implements in-place ``TOS = TOS1 * TOS``.
312
313
314.. opcode:: INPLACE_DIVIDE ()
315
316 Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
317 division`` is not in effect.
318
319
320.. opcode:: INPLACE_FLOOR_DIVIDE ()
321
322 Implements in-place ``TOS = TOS1 // TOS``.
323
324
325.. opcode:: INPLACE_TRUE_DIVIDE ()
326
327 Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
328 division`` is in effect.
329
330
331.. opcode:: INPLACE_MODULO ()
332
333 Implements in-place ``TOS = TOS1 % TOS``.
334
335
336.. opcode:: INPLACE_ADD ()
337
338 Implements in-place ``TOS = TOS1 + TOS``.
339
340
341.. opcode:: INPLACE_SUBTRACT ()
342
343 Implements in-place ``TOS = TOS1 - TOS``.
344
345
346.. opcode:: INPLACE_LSHIFT ()
347
348 Implements in-place ``TOS = TOS1 << TOS``.
349
350
351.. opcode:: INPLACE_RSHIFT ()
352
353 Implements in-place ``TOS = TOS1 >> TOS``.
354
355
356.. opcode:: INPLACE_AND ()
357
358 Implements in-place ``TOS = TOS1 & TOS``.
359
360
361.. opcode:: INPLACE_XOR ()
362
363 Implements in-place ``TOS = TOS1 ^ TOS``.
364
365
366.. opcode:: INPLACE_OR ()
367
368 Implements in-place ``TOS = TOS1 | TOS``.
369
370The slice opcodes take up to three parameters.
371
372
373.. opcode:: SLICE+0 ()
374
375 Implements ``TOS = TOS[:]``.
376
377
378.. opcode:: SLICE+1 ()
379
380 Implements ``TOS = TOS1[TOS:]``.
381
382
383.. opcode:: SLICE+2 ()
384
385 Implements ``TOS = TOS1[:TOS]``.
386
387
388.. opcode:: SLICE+3 ()
389
390 Implements ``TOS = TOS2[TOS1:TOS]``.
391
392Slice assignment needs even an additional parameter. As any statement, they put
393nothing on the stack.
394
395
396.. opcode:: STORE_SLICE+0 ()
397
398 Implements ``TOS[:] = TOS1``.
399
400
401.. opcode:: STORE_SLICE+1 ()
402
403 Implements ``TOS1[TOS:] = TOS2``.
404
405
406.. opcode:: STORE_SLICE+2 ()
407
408 Implements ``TOS1[:TOS] = TOS2``.
409
410
411.. opcode:: STORE_SLICE+3 ()
412
413 Implements ``TOS2[TOS1:TOS] = TOS3``.
414
415
416.. opcode:: DELETE_SLICE+0 ()
417
418 Implements ``del TOS[:]``.
419
420
421.. opcode:: DELETE_SLICE+1 ()
422
423 Implements ``del TOS1[TOS:]``.
424
425
426.. opcode:: DELETE_SLICE+2 ()
427
428 Implements ``del TOS1[:TOS]``.
429
430
431.. opcode:: DELETE_SLICE+3 ()
432
433 Implements ``del TOS2[TOS1:TOS]``.
434
435
436.. opcode:: STORE_SUBSCR ()
437
438 Implements ``TOS1[TOS] = TOS2``.
439
440
441.. opcode:: DELETE_SUBSCR ()
442
443 Implements ``del TOS1[TOS]``.
444
445Miscellaneous opcodes.
446
447
448.. opcode:: PRINT_EXPR ()
449
450 Implements the expression statement for the interactive mode. TOS is removed
451 from the stack and printed. In non-interactive mode, an expression statement is
452 terminated with ``POP_STACK``.
453
454
455.. opcode:: PRINT_ITEM ()
456
457 Prints TOS to the file-like object bound to ``sys.stdout``. There is one such
458 instruction for each item in the :keyword:`print` statement.
459
460
461.. opcode:: PRINT_ITEM_TO ()
462
463 Like ``PRINT_ITEM``, but prints the item second from TOS to the file-like object
464 at TOS. This is used by the extended print statement.
465
466
467.. opcode:: PRINT_NEWLINE ()
468
469 Prints a new line on ``sys.stdout``. This is generated as the last operation of
470 a :keyword:`print` statement, unless the statement ends with a comma.
471
472
473.. opcode:: PRINT_NEWLINE_TO ()
474
475 Like ``PRINT_NEWLINE``, but prints the new line on the file-like object on the
476 TOS. This is used by the extended print statement.
477
478
479.. opcode:: BREAK_LOOP ()
480
481 Terminates a loop due to a :keyword:`break` statement.
482
483
484.. opcode:: CONTINUE_LOOP (target)
485
486 Continues a loop due to a :keyword:`continue` statement. *target* is the
487 address to jump to (which should be a ``FOR_ITER`` instruction).
488
489
Antoine Pitroud0c35152008-12-17 00:38:28 +0000490.. opcode:: LIST_APPEND (i)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000491
Antoine Pitroud0c35152008-12-17 00:38:28 +0000492 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
493 While the appended value is popped off, the list object remains on the
494 stack so that it is available for further iterations of the loop.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000495
496
497.. opcode:: LOAD_LOCALS ()
498
499 Pushes a reference to the locals of the current scope on the stack. This is used
500 in the code for a class definition: After the class body is evaluated, the
501 locals are passed to the class definition.
502
503
504.. opcode:: RETURN_VALUE ()
505
506 Returns with TOS to the caller of the function.
507
508
509.. opcode:: YIELD_VALUE ()
510
Georg Brandlcf3fb252007-10-21 10:52:38 +0000511 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000512
513
514.. opcode:: IMPORT_STAR ()
515
516 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
517 local namespace. The module is popped after loading all names. This opcode
518 implements ``from module import *``.
519
520
521.. opcode:: EXEC_STMT ()
522
523 Implements ``exec TOS2,TOS1,TOS``. The compiler fills missing optional
524 parameters with ``None``.
525
526
527.. opcode:: POP_BLOCK ()
528
529 Removes one block from the block stack. Per frame, there is a stack of blocks,
530 denoting nested loops, try statements, and such.
531
532
533.. opcode:: END_FINALLY ()
534
535 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
536 exception has to be re-raised, or whether the function returns, and continues
537 with the outer-next block.
538
539
540.. opcode:: BUILD_CLASS ()
541
542 Creates a new class object. TOS is the methods dictionary, TOS1 the tuple of
543 the names of the base classes, and TOS2 the class name.
544
Georg Brandl4debd552007-08-23 17:54:11 +0000545
Benjamin Peterson1880d8b2009-05-25 13:13:44 +0000546.. opcode:: SETUP_WITH (delta)
547
548 This opcode performs several operations before a with block starts. First,
549 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
550 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
551 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
552 is pushed. Finally, the result of calling the enter method is pushed onto
553 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
554 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
555 :opcode:`UNPACK_SEQUENCE`).
556
557
Georg Brandl4debd552007-08-23 17:54:11 +0000558.. opcode:: WITH_CLEANUP ()
559
Nick Coghlan7af53be2008-03-07 14:13:28 +0000560 Cleans up the stack when a :keyword:`with` statement block exits. On top of
561 the stack are 1--3 values indicating how/why the finally clause was entered:
Georg Brandl4debd552007-08-23 17:54:11 +0000562
Nick Coghlan7af53be2008-03-07 14:13:28 +0000563 * TOP = ``None``
564 * (TOP, SECOND) = (``WHY_{RETURN,CONTINUE}``), retval
565 * TOP = ``WHY_*``; no retval below it
566 * (TOP, SECOND, THIRD) = exc_info()
Georg Brandl4debd552007-08-23 17:54:11 +0000567
Nick Coghlan7af53be2008-03-07 14:13:28 +0000568 Under them is EXIT, the context manager's :meth:`__exit__` bound method.
Georg Brandl4debd552007-08-23 17:54:11 +0000569
Nick Coghlan7af53be2008-03-07 14:13:28 +0000570 In the last case, ``EXIT(TOP, SECOND, THIRD)`` is called, otherwise
571 ``EXIT(None, None, None)``.
572
573 EXIT is removed from the stack, leaving the values above it in the same
574 order. In addition, if the stack represents an exception, *and* the function
575 call returns a 'true' value, this information is "zapped", to prevent
576 ``END_FINALLY`` from re-raising the exception. (But non-local gotos should
577 still be resumed.)
Georg Brandl4debd552007-08-23 17:54:11 +0000578
Georg Brandlff27e0c2007-10-20 13:22:53 +0000579 .. XXX explain the WHY stuff!
580
Georg Brandl4debd552007-08-23 17:54:11 +0000581
Georg Brandl8ec7f652007-08-15 14:28:01 +0000582All of the following opcodes expect arguments. An argument is two bytes, with
583the more significant byte last.
584
Georg Brandl8ec7f652007-08-15 14:28:01 +0000585.. opcode:: STORE_NAME (namei)
586
587 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Georg Brandl59724932008-02-23 15:43:48 +0000588 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl8ec7f652007-08-15 14:28:01 +0000589 or ``STORE_GLOBAL`` if possible.
590
591
592.. opcode:: DELETE_NAME (namei)
593
594 Implements ``del name``, where *namei* is the index into :attr:`co_names`
595 attribute of the code object.
596
597
598.. opcode:: UNPACK_SEQUENCE (count)
599
600 Unpacks TOS into *count* individual values, which are put onto the stack
601 right-to-left.
602
Georg Brandl8ec7f652007-08-15 14:28:01 +0000603
604.. opcode:: DUP_TOPX (count)
605
606 Duplicate *count* items, keeping them in the same order. Due to implementation
607 limits, *count* should be between 1 and 5 inclusive.
608
609
610.. opcode:: STORE_ATTR (namei)
611
612 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
613 :attr:`co_names`.
614
615
616.. opcode:: DELETE_ATTR (namei)
617
618 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
619
620
621.. opcode:: STORE_GLOBAL (namei)
622
623 Works as ``STORE_NAME``, but stores the name as a global.
624
625
626.. opcode:: DELETE_GLOBAL (namei)
627
628 Works as ``DELETE_NAME``, but deletes a global name.
629
Georg Brandl8ec7f652007-08-15 14:28:01 +0000630
631.. opcode:: LOAD_CONST (consti)
632
633 Pushes ``co_consts[consti]`` onto the stack.
634
635
636.. opcode:: LOAD_NAME (namei)
637
638 Pushes the value associated with ``co_names[namei]`` onto the stack.
639
640
641.. opcode:: BUILD_TUPLE (count)
642
643 Creates a tuple consuming *count* items from the stack, and pushes the resulting
644 tuple onto the stack.
645
646
647.. opcode:: BUILD_LIST (count)
648
649 Works as ``BUILD_TUPLE``, but creates a list.
650
651
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000652.. opcode:: BUILD_MAP (count)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000653
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000654 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
655 to hold *count* entries.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000656
657
658.. opcode:: LOAD_ATTR (namei)
659
660 Replaces TOS with ``getattr(TOS, co_names[namei])``.
661
662
663.. opcode:: COMPARE_OP (opname)
664
665 Performs a Boolean operation. The operation name can be found in
666 ``cmp_op[opname]``.
667
668
669.. opcode:: IMPORT_NAME (namei)
670
Georg Brandl2fb8a532008-04-19 16:59:16 +0000671 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
672 the *fromlist* and *level* arguments of :func:`__import__`. The module
673 object is pushed onto the stack. The current namespace is not affected:
674 for a proper import statement, a subsequent ``STORE_FAST`` instruction
675 modifies the namespace.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000676
677
678.. opcode:: IMPORT_FROM (namei)
679
680 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
681 resulting object is pushed onto the stack, to be subsequently stored by a
682 ``STORE_FAST`` instruction.
683
684
685.. opcode:: JUMP_FORWARD (delta)
686
Georg Brandl63fa1682007-10-21 10:24:20 +0000687 Increments bytecode counter by *delta*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000688
689
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000690.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000691
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000692 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000693
694
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000695.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000696
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000697 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
698
699
700.. opcode:: JUMP_IF_TRUE_OR_POP (target)
701
702 If TOS is true, sets the bytecode counter to *target* and leaves TOS
703 on the stack. Otherwise (TOS is false), TOS is popped.
704
705
706.. opcode:: JUMP_IF_FALSE_OR_POP (target)
707
708 If TOS is false, sets the bytecode counter to *target* and leaves
709 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000710
711
712.. opcode:: JUMP_ABSOLUTE (target)
713
Georg Brandl63fa1682007-10-21 10:24:20 +0000714 Set bytecode counter to *target*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000715
716
717.. opcode:: FOR_ITER (delta)
718
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000719 ``TOS`` is an :term:`iterator`. Call its :meth:`!next` method. If this
Georg Brandle7a09902007-10-21 12:10:28 +0000720 yields a new value, push it on the stack (leaving the iterator below it). If
721 the iterator indicates it is exhausted ``TOS`` is popped, and the bytecode
722 counter is incremented by *delta*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000723
Georg Brandl8ec7f652007-08-15 14:28:01 +0000724
725.. opcode:: LOAD_GLOBAL (namei)
726
727 Loads the global named ``co_names[namei]`` onto the stack.
728
Georg Brandl8ec7f652007-08-15 14:28:01 +0000729
730.. opcode:: SETUP_LOOP (delta)
731
732 Pushes a block for a loop onto the block stack. The block spans from the
733 current instruction with a size of *delta* bytes.
734
735
736.. opcode:: SETUP_EXCEPT (delta)
737
738 Pushes a try block from a try-except clause onto the block stack. *delta* points
739 to the first except block.
740
741
742.. opcode:: SETUP_FINALLY (delta)
743
744 Pushes a try block from a try-except clause onto the block stack. *delta* points
745 to the finally block.
746
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000747.. opcode:: STORE_MAP ()
748
749 Store a key and value pair in a dictionary. Pops the key and value while leaving
750 the dictionary on the stack.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000751
752.. opcode:: LOAD_FAST (var_num)
753
754 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
755
756
757.. opcode:: STORE_FAST (var_num)
758
759 Stores TOS into the local ``co_varnames[var_num]``.
760
761
762.. opcode:: DELETE_FAST (var_num)
763
764 Deletes local ``co_varnames[var_num]``.
765
766
767.. opcode:: LOAD_CLOSURE (i)
768
769 Pushes a reference to the cell contained in slot *i* of the cell and free
770 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
771 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
772 len(co_cellvars)]``.
773
774
775.. opcode:: LOAD_DEREF (i)
776
777 Loads the cell contained in slot *i* of the cell and free variable storage.
778 Pushes a reference to the object the cell contains on the stack.
779
780
781.. opcode:: STORE_DEREF (i)
782
783 Stores TOS into the cell contained in slot *i* of the cell and free variable
784 storage.
785
786
787.. opcode:: SET_LINENO (lineno)
788
789 This opcode is obsolete.
790
791
792.. opcode:: RAISE_VARARGS (argc)
793
794 Raises an exception. *argc* indicates the number of parameters to the raise
795 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
796 the parameter as TOS1, and the exception as TOS.
797
798
799.. opcode:: CALL_FUNCTION (argc)
800
801 Calls a function. The low byte of *argc* indicates the number of positional
802 parameters, the high byte the number of keyword parameters. On the stack, the
803 opcode finds the keyword parameters first. For each keyword argument, the value
804 is on top of the key. Below the keyword parameters, the positional parameters
805 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000806 the function object to call is on the stack. Pops all function arguments, and
Benjamin Petersonc2f14402008-10-17 20:01:01 +0000807 the function itself off the stack, and pushes the return value.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000808
809
810.. opcode:: MAKE_FUNCTION (argc)
811
812 Pushes a new function object on the stack. TOS is the code associated with the
813 function. The function object is defined to have *argc* default parameters,
814 which are found below TOS.
815
816
817.. opcode:: MAKE_CLOSURE (argc)
818
819 Creates a new function object, sets its *func_closure* slot, and pushes it on
Georg Brandl4debd552007-08-23 17:54:11 +0000820 the stack. TOS is the code associated with the function, TOS1 the tuple
821 containing cells for the closure's free variables. The function also has
822 *argc* default parameters, which are found below the cells.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000823
824
825.. opcode:: BUILD_SLICE (argc)
826
827 .. index:: builtin: slice
828
829 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
830 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Georg Brandlb19be572007-12-29 10:57:00 +0000831 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000832
833
834.. opcode:: EXTENDED_ARG (ext)
835
836 Prefixes any opcode which has an argument too big to fit into the default two
837 bytes. *ext* holds two additional bytes which, taken together with the
838 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
839 most-significant bytes.
840
841
842.. opcode:: CALL_FUNCTION_VAR (argc)
843
844 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
845 on the stack contains the variable argument list, followed by keyword and
846 positional arguments.
847
848
849.. opcode:: CALL_FUNCTION_KW (argc)
850
851 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
852 on the stack contains the keyword arguments dictionary, followed by explicit
853 keyword and positional arguments.
854
855
856.. opcode:: CALL_FUNCTION_VAR_KW (argc)
857
858 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
859 element on the stack contains the keyword arguments dictionary, followed by the
860 variable-arguments tuple, followed by explicit keyword and positional arguments.
861
862
863.. opcode:: HAVE_ARGUMENT ()
864
865 This is not really an opcode. It identifies the dividing line between opcodes
866 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
867 HAVE_ARGUMENT``.
868