blob: 04b9b15386336e88b297dcfa2585ff5ecdc50668 [file] [log] [blame]
Georg Brandl63fa1682007-10-21 10:24:20 +00001:mod:`dis` --- Disassembler for Python bytecode
2===============================================
Georg Brandl8ec7f652007-08-15 14:28:01 +00003
4.. module:: dis
Georg Brandl63fa1682007-10-21 10:24:20 +00005 :synopsis: Disassembler for Python bytecode.
Georg Brandl8ec7f652007-08-15 14:28:01 +00006
Éric Araujo29a0b572011-08-19 02:14:03 +02007**Source code:** :source:`Lib/dis.py`
8
9--------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000010
Brett Cannon8c4fa112010-07-21 09:52:10 +000011The :mod:`dis` module supports the analysis of CPython :term:`bytecode` by
Benjamin Petersonb198ad92015-03-02 09:34:31 -050012disassembling it. The CPython bytecode which this module takes as an input is
13defined in the file :file:`Include/opcode.h` and used by the compiler and the
14interpreter.
Brett Cannon8c4fa112010-07-21 09:52:10 +000015
16.. impl-detail::
17
18 Bytecode is an implementation detail of the CPython interpreter! No
19 guarantees are made that bytecode will not be added, removed, or changed
20 between versions of Python. Use of this module should not be considered to
21 work across Python VMs or Python releases.
Georg Brandl8ec7f652007-08-15 14:28:01 +000022
23Example: Given the function :func:`myfunc`::
24
25 def myfunc(alist):
26 return len(alist)
27
28the following command can be used to get the disassembly of :func:`myfunc`::
29
30 >>> dis.dis(myfunc)
31 2 0 LOAD_GLOBAL 0 (len)
32 3 LOAD_FAST 0 (alist)
33 6 CALL_FUNCTION 1
34 9 RETURN_VALUE
35
36(The "2" is a line number).
37
38The :mod:`dis` module defines the following functions and constants:
39
40
41.. function:: dis([bytesource])
42
Benjamin Petersonb198ad92015-03-02 09:34:31 -050043 Disassemble the *bytesource* object. *bytesource* can denote either a module,
44 a class, a method, a function, or a code object. For a module, it
45 disassembles all functions. For a class, it disassembles all methods. For a
46 single code sequence, it prints one line per bytecode instruction. If no
47 object is provided, it disassembles the last traceback.
Georg Brandl8ec7f652007-08-15 14:28:01 +000048
49
50.. function:: distb([tb])
51
Benjamin Petersonb198ad92015-03-02 09:34:31 -050052 Disassembles the top-of-stack function of a traceback, using the last
53 traceback if none was passed. The instruction causing the exception is
54 indicated.
Georg Brandl8ec7f652007-08-15 14:28:01 +000055
56
57.. function:: disassemble(code[, lasti])
58
59 Disassembles a code object, indicating the last instruction if *lasti* was
60 provided. The output is divided in the following columns:
61
62 #. the line number, for the first instruction of each line
63 #. the current instruction, indicated as ``-->``,
64 #. a labelled instruction, indicated with ``>>``,
65 #. the address of the instruction,
66 #. the operation code name,
67 #. operation parameters, and
68 #. interpretation of the parameters in parentheses.
69
70 The parameter interpretation recognizes local and global variable names,
71 constant values, branch targets, and compare operators.
72
73
74.. function:: disco(code[, lasti])
75
Georg Brandl775c3072009-01-01 12:09:40 +000076 A synonym for :func:`disassemble`. It is more convenient to type, and kept
77 for compatibility with earlier Python releases.
Georg Brandl8ec7f652007-08-15 14:28:01 +000078
79
Georg Brandl775c3072009-01-01 12:09:40 +000080.. function:: findlinestarts(code)
81
82 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
83 attributes of the code object *code* to find the offsets which are starts of
84 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
85
86
87.. function:: findlabels(code)
88
89 Detect all offsets in the code object *code* which are jump targets, and
90 return a list of these offsets.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000091
92
Georg Brandl8ec7f652007-08-15 14:28:01 +000093.. data:: opname
94
Georg Brandl63fa1682007-10-21 10:24:20 +000095 Sequence of operation names, indexable using the bytecode.
Georg Brandl8ec7f652007-08-15 14:28:01 +000096
97
98.. data:: opmap
99
Georg Brandlb8d0e362010-11-26 07:53:50 +0000100 Dictionary mapping operation names to bytecodes.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000101
102
103.. data:: cmp_op
104
105 Sequence of all compare operation names.
106
107
108.. data:: hasconst
109
Georg Brandl63fa1682007-10-21 10:24:20 +0000110 Sequence of bytecodes that have a constant parameter.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000111
112
113.. data:: hasfree
114
Georg Brandl63fa1682007-10-21 10:24:20 +0000115 Sequence of bytecodes that access a free variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000116
117
118.. data:: hasname
119
Georg Brandl63fa1682007-10-21 10:24:20 +0000120 Sequence of bytecodes that access an attribute by name.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000121
122
123.. data:: hasjrel
124
Georg Brandl63fa1682007-10-21 10:24:20 +0000125 Sequence of bytecodes that have a relative jump target.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000126
127
128.. data:: hasjabs
129
Georg Brandl63fa1682007-10-21 10:24:20 +0000130 Sequence of bytecodes that have an absolute jump target.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000131
132
133.. data:: haslocal
134
Georg Brandl63fa1682007-10-21 10:24:20 +0000135 Sequence of bytecodes that access a local variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000136
137
138.. data:: hascompare
139
Georg Brandl63fa1682007-10-21 10:24:20 +0000140 Sequence of bytecodes of Boolean operations.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000141
142
143.. _bytecodes:
144
Georg Brandl63fa1682007-10-21 10:24:20 +0000145Python Bytecode Instructions
146----------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000147
Georg Brandl63fa1682007-10-21 10:24:20 +0000148The Python compiler currently generates the following bytecode instructions.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000149
150
151.. opcode:: STOP_CODE ()
152
153 Indicates end-of-code to the compiler, not used by the interpreter.
154
155
156.. opcode:: NOP ()
157
158 Do nothing code. Used as a placeholder by the bytecode optimizer.
159
160
161.. opcode:: POP_TOP ()
162
163 Removes the top-of-stack (TOS) item.
164
165
166.. opcode:: ROT_TWO ()
167
168 Swaps the two top-most stack items.
169
170
171.. opcode:: ROT_THREE ()
172
173 Lifts second and third stack item one position up, moves top down to position
174 three.
175
176
177.. opcode:: ROT_FOUR ()
178
179 Lifts second, third and forth stack item one position up, moves top down to
180 position four.
181
182
183.. opcode:: DUP_TOP ()
184
185 Duplicates the reference on top of the stack.
186
187Unary Operations take the top of the stack, apply the operation, and push the
188result back on the stack.
189
190
191.. opcode:: UNARY_POSITIVE ()
192
193 Implements ``TOS = +TOS``.
194
195
196.. opcode:: UNARY_NEGATIVE ()
197
198 Implements ``TOS = -TOS``.
199
200
201.. opcode:: UNARY_NOT ()
202
203 Implements ``TOS = not TOS``.
204
205
206.. opcode:: UNARY_CONVERT ()
207
208 Implements ``TOS = `TOS```.
209
210
211.. opcode:: UNARY_INVERT ()
212
213 Implements ``TOS = ~TOS``.
214
215
216.. opcode:: GET_ITER ()
217
218 Implements ``TOS = iter(TOS)``.
219
220Binary operations remove the top of the stack (TOS) and the second top-most
221stack item (TOS1) from the stack. They perform the operation, and put the
222result back on the stack.
223
224
225.. opcode:: BINARY_POWER ()
226
227 Implements ``TOS = TOS1 ** TOS``.
228
229
230.. opcode:: BINARY_MULTIPLY ()
231
232 Implements ``TOS = TOS1 * TOS``.
233
234
235.. opcode:: BINARY_DIVIDE ()
236
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500237 Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is
238 not in effect.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000239
240
241.. opcode:: BINARY_FLOOR_DIVIDE ()
242
243 Implements ``TOS = TOS1 // TOS``.
244
245
246.. opcode:: BINARY_TRUE_DIVIDE ()
247
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500248 Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is
249 in effect.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000250
251
252.. opcode:: BINARY_MODULO ()
253
254 Implements ``TOS = TOS1 % TOS``.
255
256
257.. opcode:: BINARY_ADD ()
258
259 Implements ``TOS = TOS1 + TOS``.
260
261
262.. opcode:: BINARY_SUBTRACT ()
263
264 Implements ``TOS = TOS1 - TOS``.
265
266
267.. opcode:: BINARY_SUBSCR ()
268
269 Implements ``TOS = TOS1[TOS]``.
270
271
272.. opcode:: BINARY_LSHIFT ()
273
274 Implements ``TOS = TOS1 << TOS``.
275
276
277.. opcode:: BINARY_RSHIFT ()
278
279 Implements ``TOS = TOS1 >> TOS``.
280
281
282.. opcode:: BINARY_AND ()
283
284 Implements ``TOS = TOS1 & TOS``.
285
286
287.. opcode:: BINARY_XOR ()
288
289 Implements ``TOS = TOS1 ^ TOS``.
290
291
292.. opcode:: BINARY_OR ()
293
294 Implements ``TOS = TOS1 | TOS``.
295
296In-place operations are like binary operations, in that they remove TOS and
297TOS1, and push the result back on the stack, but the operation is done in-place
298when TOS1 supports it, and the resulting TOS may be (but does not have to be)
299the original TOS1.
300
301
302.. opcode:: INPLACE_POWER ()
303
304 Implements in-place ``TOS = TOS1 ** TOS``.
305
306
307.. opcode:: INPLACE_MULTIPLY ()
308
309 Implements in-place ``TOS = TOS1 * TOS``.
310
311
312.. opcode:: INPLACE_DIVIDE ()
313
314 Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
315 division`` is not in effect.
316
317
318.. opcode:: INPLACE_FLOOR_DIVIDE ()
319
320 Implements in-place ``TOS = TOS1 // TOS``.
321
322
323.. opcode:: INPLACE_TRUE_DIVIDE ()
324
325 Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
326 division`` is in effect.
327
328
329.. opcode:: INPLACE_MODULO ()
330
331 Implements in-place ``TOS = TOS1 % TOS``.
332
333
334.. opcode:: INPLACE_ADD ()
335
336 Implements in-place ``TOS = TOS1 + TOS``.
337
338
339.. opcode:: INPLACE_SUBTRACT ()
340
341 Implements in-place ``TOS = TOS1 - TOS``.
342
343
344.. opcode:: INPLACE_LSHIFT ()
345
346 Implements in-place ``TOS = TOS1 << TOS``.
347
348
349.. opcode:: INPLACE_RSHIFT ()
350
351 Implements in-place ``TOS = TOS1 >> TOS``.
352
353
354.. opcode:: INPLACE_AND ()
355
356 Implements in-place ``TOS = TOS1 & TOS``.
357
358
359.. opcode:: INPLACE_XOR ()
360
361 Implements in-place ``TOS = TOS1 ^ TOS``.
362
363
364.. opcode:: INPLACE_OR ()
365
366 Implements in-place ``TOS = TOS1 | TOS``.
367
368The slice opcodes take up to three parameters.
369
370
371.. opcode:: SLICE+0 ()
372
373 Implements ``TOS = TOS[:]``.
374
375
376.. opcode:: SLICE+1 ()
377
378 Implements ``TOS = TOS1[TOS:]``.
379
380
381.. opcode:: SLICE+2 ()
382
383 Implements ``TOS = TOS1[:TOS]``.
384
385
386.. opcode:: SLICE+3 ()
387
388 Implements ``TOS = TOS2[TOS1:TOS]``.
389
390Slice assignment needs even an additional parameter. As any statement, they put
391nothing on the stack.
392
393
394.. opcode:: STORE_SLICE+0 ()
395
396 Implements ``TOS[:] = TOS1``.
397
398
399.. opcode:: STORE_SLICE+1 ()
400
401 Implements ``TOS1[TOS:] = TOS2``.
402
403
404.. opcode:: STORE_SLICE+2 ()
405
406 Implements ``TOS1[:TOS] = TOS2``.
407
408
409.. opcode:: STORE_SLICE+3 ()
410
411 Implements ``TOS2[TOS1:TOS] = TOS3``.
412
413
414.. opcode:: DELETE_SLICE+0 ()
415
416 Implements ``del TOS[:]``.
417
418
419.. opcode:: DELETE_SLICE+1 ()
420
421 Implements ``del TOS1[TOS:]``.
422
423
424.. opcode:: DELETE_SLICE+2 ()
425
426 Implements ``del TOS1[:TOS]``.
427
428
429.. opcode:: DELETE_SLICE+3 ()
430
431 Implements ``del TOS2[TOS1:TOS]``.
432
433
434.. opcode:: STORE_SUBSCR ()
435
436 Implements ``TOS1[TOS] = TOS2``.
437
438
439.. opcode:: DELETE_SUBSCR ()
440
441 Implements ``del TOS1[TOS]``.
442
443Miscellaneous opcodes.
444
445
446.. opcode:: PRINT_EXPR ()
447
448 Implements the expression statement for the interactive mode. TOS is removed
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500449 from the stack and printed. In non-interactive mode, an expression statement
450 is terminated with :opcode:`POP_TOP`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000451
452
453.. opcode:: PRINT_ITEM ()
454
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500455 Prints TOS to the file-like object bound to ``sys.stdout``. There is one
456 such instruction for each item in the :keyword:`print` statement.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000457
458
459.. opcode:: PRINT_ITEM_TO ()
460
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500461 Like ``PRINT_ITEM``, but prints the item second from TOS to the file-like
462 object at TOS. This is used by the extended print statement.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000463
464
465.. opcode:: PRINT_NEWLINE ()
466
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500467 Prints a new line on ``sys.stdout``. This is generated as the last operation
468 of a :keyword:`print` statement, unless the statement ends with a comma.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000469
470
471.. opcode:: PRINT_NEWLINE_TO ()
472
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500473 Like ``PRINT_NEWLINE``, but prints the new line on the file-like object on
474 the TOS. This is used by the extended print statement.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000475
476
477.. opcode:: BREAK_LOOP ()
478
479 Terminates a loop due to a :keyword:`break` statement.
480
481
482.. opcode:: CONTINUE_LOOP (target)
483
484 Continues a loop due to a :keyword:`continue` statement. *target* is the
Serhiy Storchaka04eee632014-11-11 10:02:57 +0200485 address to jump to (which should be a :opcode:`FOR_ITER` instruction).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000486
487
Antoine Pitroud0c35152008-12-17 00:38:28 +0000488.. opcode:: LIST_APPEND (i)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000489
Antoine Pitroud0c35152008-12-17 00:38:28 +0000490 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500491 While the appended value is popped off, the list object remains on the stack
492 so that it is available for further iterations of the loop.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000493
494
495.. opcode:: LOAD_LOCALS ()
496
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500497 Pushes a reference to the locals of the current scope on the stack. This is
498 used in the code for a class definition: After the class body is evaluated,
499 the locals are passed to the class definition.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000500
501
502.. opcode:: RETURN_VALUE ()
503
504 Returns with TOS to the caller of the function.
505
506
507.. opcode:: YIELD_VALUE ()
508
Georg Brandlcf3fb252007-10-21 10:52:38 +0000509 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000510
511
512.. opcode:: IMPORT_STAR ()
513
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500514 Loads all symbols not starting with ``'_'`` directly from the module TOS to
515 the local namespace. The module is popped after loading all names. This
516 opcode implements ``from module import *``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000517
518
519.. opcode:: EXEC_STMT ()
520
521 Implements ``exec TOS2,TOS1,TOS``. The compiler fills missing optional
522 parameters with ``None``.
523
524
525.. opcode:: POP_BLOCK ()
526
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500527 Removes one block from the block stack. Per frame, there is a stack of
528 blocks, denoting nested loops, try statements, and such.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000529
530
531.. opcode:: END_FINALLY ()
532
533 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
534 exception has to be re-raised, or whether the function returns, and continues
535 with the outer-next block.
536
537
538.. opcode:: BUILD_CLASS ()
539
540 Creates a new class object. TOS is the methods dictionary, TOS1 the tuple of
541 the names of the base classes, and TOS2 the class name.
542
Georg Brandl4debd552007-08-23 17:54:11 +0000543
Benjamin Peterson1880d8b2009-05-25 13:13:44 +0000544.. opcode:: SETUP_WITH (delta)
545
546 This opcode performs several operations before a with block starts. First,
547 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
548 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
549 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
550 is pushed. Finally, the result of calling the enter method is pushed onto
551 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
552 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
553 :opcode:`UNPACK_SEQUENCE`).
554
555
Georg Brandl4debd552007-08-23 17:54:11 +0000556.. opcode:: WITH_CLEANUP ()
557
Nick Coghlan7af53be2008-03-07 14:13:28 +0000558 Cleans up the stack when a :keyword:`with` statement block exits. On top of
559 the stack are 1--3 values indicating how/why the finally clause was entered:
Georg Brandl4debd552007-08-23 17:54:11 +0000560
Nick Coghlan7af53be2008-03-07 14:13:28 +0000561 * TOP = ``None``
562 * (TOP, SECOND) = (``WHY_{RETURN,CONTINUE}``), retval
563 * TOP = ``WHY_*``; no retval below it
564 * (TOP, SECOND, THIRD) = exc_info()
Georg Brandl4debd552007-08-23 17:54:11 +0000565
Nick Coghlan7af53be2008-03-07 14:13:28 +0000566 Under them is EXIT, the context manager's :meth:`__exit__` bound method.
Georg Brandl4debd552007-08-23 17:54:11 +0000567
Nick Coghlan7af53be2008-03-07 14:13:28 +0000568 In the last case, ``EXIT(TOP, SECOND, THIRD)`` is called, otherwise
569 ``EXIT(None, None, None)``.
570
571 EXIT is removed from the stack, leaving the values above it in the same
572 order. In addition, if the stack represents an exception, *and* the function
573 call returns a 'true' value, this information is "zapped", to prevent
574 ``END_FINALLY`` from re-raising the exception. (But non-local gotos should
575 still be resumed.)
Georg Brandl4debd552007-08-23 17:54:11 +0000576
Georg Brandlff27e0c2007-10-20 13:22:53 +0000577 .. XXX explain the WHY stuff!
578
Georg Brandl4debd552007-08-23 17:54:11 +0000579
Georg Brandl8ec7f652007-08-15 14:28:01 +0000580All of the following opcodes expect arguments. An argument is two bytes, with
581the more significant byte last.
582
Georg Brandl8ec7f652007-08-15 14:28:01 +0000583.. opcode:: STORE_NAME (namei)
584
585 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Georg Brandl59724932008-02-23 15:43:48 +0000586 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl8ec7f652007-08-15 14:28:01 +0000587 or ``STORE_GLOBAL`` if possible.
588
589
590.. opcode:: DELETE_NAME (namei)
591
592 Implements ``del name``, where *namei* is the index into :attr:`co_names`
593 attribute of the code object.
594
595
596.. opcode:: UNPACK_SEQUENCE (count)
597
598 Unpacks TOS into *count* individual values, which are put onto the stack
599 right-to-left.
600
Georg Brandl8ec7f652007-08-15 14:28:01 +0000601
602.. opcode:: DUP_TOPX (count)
603
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500604 Duplicate *count* items, keeping them in the same order. Due to
605 implementation limits, *count* should be between 1 and 5 inclusive.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000606
607
608.. opcode:: STORE_ATTR (namei)
609
610 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
611 :attr:`co_names`.
612
613
614.. opcode:: DELETE_ATTR (namei)
615
616 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
617
618
619.. opcode:: STORE_GLOBAL (namei)
620
621 Works as ``STORE_NAME``, but stores the name as a global.
622
623
624.. opcode:: DELETE_GLOBAL (namei)
625
626 Works as ``DELETE_NAME``, but deletes a global name.
627
Georg Brandl8ec7f652007-08-15 14:28:01 +0000628
629.. opcode:: LOAD_CONST (consti)
630
631 Pushes ``co_consts[consti]`` onto the stack.
632
633
634.. opcode:: LOAD_NAME (namei)
635
636 Pushes the value associated with ``co_names[namei]`` onto the stack.
637
638
639.. opcode:: BUILD_TUPLE (count)
640
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500641 Creates a tuple consuming *count* items from the stack, and pushes the
642 resulting tuple onto the stack.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000643
644
645.. opcode:: BUILD_LIST (count)
646
647 Works as ``BUILD_TUPLE``, but creates a list.
648
649
Zachary Waredb7a1d62015-11-12 10:02:06 -0600650.. opcode:: BUILD_SET (count)
651
652 Works as ``BUILD_TUPLE``, but creates a set.
653
654 .. versionadded:: 2.7
655
656
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000657.. opcode:: BUILD_MAP (count)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000658
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000659 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
660 to hold *count* entries.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000661
662
663.. opcode:: LOAD_ATTR (namei)
664
665 Replaces TOS with ``getattr(TOS, co_names[namei])``.
666
667
668.. opcode:: COMPARE_OP (opname)
669
670 Performs a Boolean operation. The operation name can be found in
671 ``cmp_op[opname]``.
672
673
674.. opcode:: IMPORT_NAME (namei)
675
Georg Brandl2fb8a532008-04-19 16:59:16 +0000676 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
677 the *fromlist* and *level* arguments of :func:`__import__`. The module
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500678 object is pushed onto the stack. The current namespace is not affected: for
679 a proper import statement, a subsequent ``STORE_FAST`` instruction modifies
680 the namespace.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000681
682
683.. opcode:: IMPORT_FROM (namei)
684
685 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
686 resulting object is pushed onto the stack, to be subsequently stored by a
687 ``STORE_FAST`` instruction.
688
689
690.. opcode:: JUMP_FORWARD (delta)
691
Georg Brandl63fa1682007-10-21 10:24:20 +0000692 Increments bytecode counter by *delta*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000693
694
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000695.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000696
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000697 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000698
699
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000700.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000701
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000702 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
703
704
705.. opcode:: JUMP_IF_TRUE_OR_POP (target)
706
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500707 If TOS is true, sets the bytecode counter to *target* and leaves TOS on the
708 stack. Otherwise (TOS is false), TOS is popped.
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000709
710
711.. opcode:: JUMP_IF_FALSE_OR_POP (target)
712
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500713 If TOS is false, sets the bytecode counter to *target* and leaves TOS on the
714 stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000715
716
717.. opcode:: JUMP_ABSOLUTE (target)
718
Georg Brandl63fa1682007-10-21 10:24:20 +0000719 Set bytecode counter to *target*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000720
721
722.. opcode:: FOR_ITER (delta)
723
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000724 ``TOS`` is an :term:`iterator`. Call its :meth:`!next` method. If this
Georg Brandle7a09902007-10-21 12:10:28 +0000725 yields a new value, push it on the stack (leaving the iterator below it). If
726 the iterator indicates it is exhausted ``TOS`` is popped, and the bytecode
727 counter is incremented by *delta*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000728
Georg Brandl8ec7f652007-08-15 14:28:01 +0000729
730.. opcode:: LOAD_GLOBAL (namei)
731
732 Loads the global named ``co_names[namei]`` onto the stack.
733
Georg Brandl8ec7f652007-08-15 14:28:01 +0000734
735.. opcode:: SETUP_LOOP (delta)
736
737 Pushes a block for a loop onto the block stack. The block spans from the
738 current instruction with a size of *delta* bytes.
739
740
741.. opcode:: SETUP_EXCEPT (delta)
742
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500743 Pushes a try block from a try-except clause onto the block stack. *delta*
744 points to the first except block.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000745
746
747.. opcode:: SETUP_FINALLY (delta)
748
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500749 Pushes a try block from a try-except clause onto the block stack. *delta*
750 points to the finally block.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000751
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000752.. opcode:: STORE_MAP ()
753
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500754 Store a key and value pair in a dictionary. Pops the key and value while
755 leaving the dictionary on the stack.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000756
757.. opcode:: LOAD_FAST (var_num)
758
759 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
760
761
762.. opcode:: STORE_FAST (var_num)
763
764 Stores TOS into the local ``co_varnames[var_num]``.
765
766
767.. opcode:: DELETE_FAST (var_num)
768
769 Deletes local ``co_varnames[var_num]``.
770
771
772.. opcode:: LOAD_CLOSURE (i)
773
774 Pushes a reference to the cell contained in slot *i* of the cell and free
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500775 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
776 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
Georg Brandl8ec7f652007-08-15 14:28:01 +0000777 len(co_cellvars)]``.
778
779
780.. opcode:: LOAD_DEREF (i)
781
782 Loads the cell contained in slot *i* of the cell and free variable storage.
783 Pushes a reference to the object the cell contains on the stack.
784
785
786.. opcode:: STORE_DEREF (i)
787
788 Stores TOS into the cell contained in slot *i* of the cell and free variable
789 storage.
790
791
792.. opcode:: SET_LINENO (lineno)
793
794 This opcode is obsolete.
795
796
797.. opcode:: RAISE_VARARGS (argc)
798
799 Raises an exception. *argc* indicates the number of parameters to the raise
800 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
801 the parameter as TOS1, and the exception as TOS.
802
803
804.. opcode:: CALL_FUNCTION (argc)
805
806 Calls a function. The low byte of *argc* indicates the number of positional
807 parameters, the high byte the number of keyword parameters. On the stack, the
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500808 opcode finds the keyword parameters first. For each keyword argument, the
809 value is on top of the key. Below the keyword parameters, the positional
810 parameters are on the stack, with the right-most parameter on top. Below the
811 parameters, the function object to call is on the stack. Pops all function
812 arguments, and the function itself off the stack, and pushes the return
813 value.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000814
815
816.. opcode:: MAKE_FUNCTION (argc)
817
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500818 Pushes a new function object on the stack. TOS is the code associated with
819 the function. The function object is defined to have *argc* default
820 parameters, which are found below TOS.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000821
822
823.. opcode:: MAKE_CLOSURE (argc)
824
825 Creates a new function object, sets its *func_closure* slot, and pushes it on
Georg Brandl4debd552007-08-23 17:54:11 +0000826 the stack. TOS is the code associated with the function, TOS1 the tuple
827 containing cells for the closure's free variables. The function also has
828 *argc* default parameters, which are found below the cells.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000829
830
831.. opcode:: BUILD_SLICE (argc)
832
833 .. index:: builtin: slice
834
835 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
836 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Georg Brandlb19be572007-12-29 10:57:00 +0000837 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000838
839
840.. opcode:: EXTENDED_ARG (ext)
841
842 Prefixes any opcode which has an argument too big to fit into the default two
843 bytes. *ext* holds two additional bytes which, taken together with the
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500844 subsequent opcode's argument, comprise a four-byte argument, *ext* being the
845 two most-significant bytes.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000846
847
848.. opcode:: CALL_FUNCTION_VAR (argc)
849
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500850 Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
851 top element on the stack contains the variable argument list, followed by
852 keyword and positional arguments.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000853
854
855.. opcode:: CALL_FUNCTION_KW (argc)
856
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500857 Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
858 top element on the stack contains the keyword arguments dictionary, followed
859 by explicit keyword and positional arguments.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000860
861
862.. opcode:: CALL_FUNCTION_VAR_KW (argc)
863
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500864 Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
865 top element on the stack contains the keyword arguments dictionary, followed
866 by the variable-arguments tuple, followed by explicit keyword and positional
867 arguments.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000868
869
870.. opcode:: HAVE_ARGUMENT ()
871
Benjamin Petersonb198ad92015-03-02 09:34:31 -0500872 This is not really an opcode. It identifies the dividing line between
873 opcodes which don't take arguments ``< HAVE_ARGUMENT`` and those which do
874 ``>= HAVE_ARGUMENT``.