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