blob: 1b486bf5defcc45db28199acd23ea76f9f811a6c [file] [log] [blame]
Georg Brandl9afde1c2007-11-01 20:32:30 +00001:mod:`dis` --- Disassembler for Python bytecode
2===============================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: dis
Georg Brandl9afde1c2007-11-01 20:32:30 +00005 :synopsis: Disassembler for Python bytecode.
Georg Brandl116aa622007-08-15 14:28:22 +00006
7
Brett Cannon8315fd12010-07-02 22:03:00 +00008The :mod:`dis` module supports the analysis of CPython :term:`bytecode` by
9disassembling it. The CPython bytecode which this module takes as an
Georg Brandl71515ca2009-05-17 12:29:12 +000010input is defined in the file :file:`Include/opcode.h` and used by the compiler
11and the interpreter.
Georg Brandl116aa622007-08-15 14:28:22 +000012
Georg Brandl19b7a872010-07-03 10:21:50 +000013.. impl-detail::
14
15 Bytecode is an implementation detail of the CPython interpreter! No
16 guarantees are made that bytecode will not be added, removed, or changed
17 between versions of Python. Use of this module should not be considered to
18 work across Python VMs or Python releases.
19
Brett Cannon8315fd12010-07-02 22:03:00 +000020
Georg Brandl116aa622007-08-15 14:28:22 +000021Example: 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
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000039.. function:: dis(x=None)
Georg Brandl116aa622007-08-15 14:28:22 +000040
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000041 Disassemble the *x* object. *x* can denote either a module, a
Nick Coghlan5c8b54e2010-07-03 07:36:51 +000042 class, a method, a function, a code object, a string of source code or a
43 byte sequence of raw bytecode. For a module, it disassembles all
44 functions. For a class, it disassembles all methods. For a code object
45 or sequence of raw bytecode, it prints one line per bytecode instruction.
46 Strings are first compiled to code objects with the :func:`compile`
47 built-in function before being disassembled. If no object is provided,
48 this function disassembles the last traceback.
Georg Brandl116aa622007-08-15 14:28:22 +000049
50
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000051.. function:: distb(tb=None)
Georg Brandl116aa622007-08-15 14:28:22 +000052
53 Disassembles the top-of-stack function of a traceback, using the last traceback
54 if none was passed. The instruction causing the exception is indicated.
55
56
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000057.. function:: disassemble(code, lasti=-1)
58 disco(code, lasti=-1)
Georg Brandl116aa622007-08-15 14:28:22 +000059
60 Disassembles a code object, indicating the last instruction if *lasti* was
61 provided. The output is divided in the following columns:
62
63 #. the line number, for the first instruction of each line
64 #. the current instruction, indicated as ``-->``,
65 #. a labelled instruction, indicated with ``>>``,
66 #. the address of the instruction,
67 #. the operation code name,
68 #. operation parameters, and
69 #. interpretation of the parameters in parentheses.
70
71 The parameter interpretation recognizes local and global variable names,
72 constant values, branch targets, and compare operators.
73
74
Benjamin Peterson75edad02009-01-01 15:05:06 +000075.. function:: findlinestarts(code)
76
77 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
78 attributes of the code object *code* to find the offsets which are starts of
79 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
80
81
82.. function:: findlabels(code)
83
84 Detect all offsets in the code object *code* which are jump targets, and
85 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +000086
87
Georg Brandl116aa622007-08-15 14:28:22 +000088.. data:: opname
89
Georg Brandl9afde1c2007-11-01 20:32:30 +000090 Sequence of operation names, indexable using the bytecode.
Georg Brandl116aa622007-08-15 14:28:22 +000091
92
93.. data:: opmap
94
Georg Brandl9afde1c2007-11-01 20:32:30 +000095 Dictionary mapping bytecodes to operation names.
Georg Brandl116aa622007-08-15 14:28:22 +000096
97
98.. data:: cmp_op
99
100 Sequence of all compare operation names.
101
102
103.. data:: hasconst
104
Georg Brandl9afde1c2007-11-01 20:32:30 +0000105 Sequence of bytecodes that have a constant parameter.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107
108.. data:: hasfree
109
Georg Brandl9afde1c2007-11-01 20:32:30 +0000110 Sequence of bytecodes that access a free variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112
113.. data:: hasname
114
Georg Brandl9afde1c2007-11-01 20:32:30 +0000115 Sequence of bytecodes that access an attribute by name.
Georg Brandl116aa622007-08-15 14:28:22 +0000116
117
118.. data:: hasjrel
119
Georg Brandl9afde1c2007-11-01 20:32:30 +0000120 Sequence of bytecodes that have a relative jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000121
122
123.. data:: hasjabs
124
Georg Brandl9afde1c2007-11-01 20:32:30 +0000125 Sequence of bytecodes that have an absolute jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127
128.. data:: haslocal
129
Georg Brandl9afde1c2007-11-01 20:32:30 +0000130 Sequence of bytecodes that access a local variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000131
132
133.. data:: hascompare
134
Georg Brandl9afde1c2007-11-01 20:32:30 +0000135 Sequence of bytecodes of Boolean operations.
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137
138.. _bytecodes:
139
Georg Brandl9afde1c2007-11-01 20:32:30 +0000140Python Bytecode Instructions
141----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Georg Brandl9afde1c2007-11-01 20:32:30 +0000143The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000144
145
146.. opcode:: STOP_CODE ()
147
148 Indicates end-of-code to the compiler, not used by the interpreter.
149
150
151.. opcode:: NOP ()
152
153 Do nothing code. Used as a placeholder by the bytecode optimizer.
154
155
156.. opcode:: POP_TOP ()
157
158 Removes the top-of-stack (TOS) item.
159
160
161.. opcode:: ROT_TWO ()
162
163 Swaps the two top-most stack items.
164
165
166.. opcode:: ROT_THREE ()
167
168 Lifts second and third stack item one position up, moves top down to position
169 three.
170
171
172.. opcode:: ROT_FOUR ()
173
174 Lifts second, third and forth stack item one position up, moves top down to
175 position four.
176
177
178.. opcode:: DUP_TOP ()
179
180 Duplicates the reference on top of the stack.
181
182Unary Operations take the top of the stack, apply the operation, and push the
183result back on the stack.
184
185
186.. opcode:: UNARY_POSITIVE ()
187
188 Implements ``TOS = +TOS``.
189
190
191.. opcode:: UNARY_NEGATIVE ()
192
193 Implements ``TOS = -TOS``.
194
195
196.. opcode:: UNARY_NOT ()
197
198 Implements ``TOS = not TOS``.
199
200
201.. opcode:: UNARY_INVERT ()
202
203 Implements ``TOS = ~TOS``.
204
205
206.. opcode:: GET_ITER ()
207
208 Implements ``TOS = iter(TOS)``.
209
210Binary operations remove the top of the stack (TOS) and the second top-most
211stack item (TOS1) from the stack. They perform the operation, and put the
212result back on the stack.
213
214
215.. opcode:: BINARY_POWER ()
216
217 Implements ``TOS = TOS1 ** TOS``.
218
219
220.. opcode:: BINARY_MULTIPLY ()
221
222 Implements ``TOS = TOS1 * TOS``.
223
224
225.. opcode:: BINARY_FLOOR_DIVIDE ()
226
227 Implements ``TOS = TOS1 // TOS``.
228
229
230.. opcode:: BINARY_TRUE_DIVIDE ()
231
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000232 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000233
234
235.. opcode:: BINARY_MODULO ()
236
237 Implements ``TOS = TOS1 % TOS``.
238
239
240.. opcode:: BINARY_ADD ()
241
242 Implements ``TOS = TOS1 + TOS``.
243
244
245.. opcode:: BINARY_SUBTRACT ()
246
247 Implements ``TOS = TOS1 - TOS``.
248
249
250.. opcode:: BINARY_SUBSCR ()
251
252 Implements ``TOS = TOS1[TOS]``.
253
254
255.. opcode:: BINARY_LSHIFT ()
256
257 Implements ``TOS = TOS1 << TOS``.
258
259
260.. opcode:: BINARY_RSHIFT ()
261
262 Implements ``TOS = TOS1 >> TOS``.
263
264
265.. opcode:: BINARY_AND ()
266
267 Implements ``TOS = TOS1 & TOS``.
268
269
270.. opcode:: BINARY_XOR ()
271
272 Implements ``TOS = TOS1 ^ TOS``.
273
274
275.. opcode:: BINARY_OR ()
276
277 Implements ``TOS = TOS1 | TOS``.
278
279In-place operations are like binary operations, in that they remove TOS and
280TOS1, and push the result back on the stack, but the operation is done in-place
281when TOS1 supports it, and the resulting TOS may be (but does not have to be)
282the original TOS1.
283
284
285.. opcode:: INPLACE_POWER ()
286
287 Implements in-place ``TOS = TOS1 ** TOS``.
288
289
290.. opcode:: INPLACE_MULTIPLY ()
291
292 Implements in-place ``TOS = TOS1 * TOS``.
293
294
295.. opcode:: INPLACE_FLOOR_DIVIDE ()
296
297 Implements in-place ``TOS = TOS1 // TOS``.
298
299
300.. opcode:: INPLACE_TRUE_DIVIDE ()
301
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000302 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000303
304
305.. opcode:: INPLACE_MODULO ()
306
307 Implements in-place ``TOS = TOS1 % TOS``.
308
309
310.. opcode:: INPLACE_ADD ()
311
312 Implements in-place ``TOS = TOS1 + TOS``.
313
314
315.. opcode:: INPLACE_SUBTRACT ()
316
317 Implements in-place ``TOS = TOS1 - TOS``.
318
319
320.. opcode:: INPLACE_LSHIFT ()
321
322 Implements in-place ``TOS = TOS1 << TOS``.
323
324
325.. opcode:: INPLACE_RSHIFT ()
326
327 Implements in-place ``TOS = TOS1 >> TOS``.
328
329
330.. opcode:: INPLACE_AND ()
331
332 Implements in-place ``TOS = TOS1 & TOS``.
333
334
335.. opcode:: INPLACE_XOR ()
336
337 Implements in-place ``TOS = TOS1 ^ TOS``.
338
339
340.. opcode:: INPLACE_OR ()
341
342 Implements in-place ``TOS = TOS1 | TOS``.
343
Georg Brandl116aa622007-08-15 14:28:22 +0000344
345.. opcode:: STORE_SUBSCR ()
346
347 Implements ``TOS1[TOS] = TOS2``.
348
349
350.. opcode:: DELETE_SUBSCR ()
351
352 Implements ``del TOS1[TOS]``.
353
354Miscellaneous opcodes.
355
356
357.. opcode:: PRINT_EXPR ()
358
359 Implements the expression statement for the interactive mode. TOS is removed
360 from the stack and printed. In non-interactive mode, an expression statement is
361 terminated with ``POP_STACK``.
362
363
364.. opcode:: BREAK_LOOP ()
365
366 Terminates a loop due to a :keyword:`break` statement.
367
368
369.. opcode:: CONTINUE_LOOP (target)
370
371 Continues a loop due to a :keyword:`continue` statement. *target* is the
372 address to jump to (which should be a ``FOR_ITER`` instruction).
373
374
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000375.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000376
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000377 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000378
379
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000380.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000381
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000382 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
383
384
385.. opcode:: MAP_ADD (i)
386
387 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
388 comprehensions.
389
390
391For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the
392added value or key/value pair is popped off, the container object remains on
393the stack so that it is available for further iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000394
395
Georg Brandl116aa622007-08-15 14:28:22 +0000396.. opcode:: RETURN_VALUE ()
397
398 Returns with TOS to the caller of the function.
399
400
401.. opcode:: YIELD_VALUE ()
402
Georg Brandl9afde1c2007-11-01 20:32:30 +0000403 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000404
405
406.. opcode:: IMPORT_STAR ()
407
408 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
409 local namespace. The module is popped after loading all names. This opcode
410 implements ``from module import *``.
411
412
413.. opcode:: POP_BLOCK ()
414
415 Removes one block from the block stack. Per frame, there is a stack of blocks,
416 denoting nested loops, try statements, and such.
417
418
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000419.. opcode:: POP_EXCEPT ()
420
421 Removes one block from the block stack. The popped block must be an exception
422 handler block, as implicitly created when entering an except handler.
423 In addition to popping extraneous values from the frame stack, the
424 last three popped values are used to restore the exception state.
425
426
Georg Brandl116aa622007-08-15 14:28:22 +0000427.. opcode:: END_FINALLY ()
428
429 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
430 exception has to be re-raised, or whether the function returns, and continues
431 with the outer-next block.
432
433
Benjamin Peterson69164c72008-07-03 14:45:20 +0000434.. opcode:: LOAD_BUILD_CLASS ()
Georg Brandl116aa622007-08-15 14:28:22 +0000435
Georg Brandl5ac22302008-07-20 21:39:03 +0000436 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Benjamin Petersonaac8fd32008-07-20 22:02:26 +0000437 by ``CALL_FUNCTION`` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000438
Guido van Rossum04110fb2007-08-24 16:32:05 +0000439
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000440.. opcode:: SETUP_WITH (delta)
441
442 This opcode performs several operations before a with block starts. First,
443 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
444 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
445 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
446 is pushed. Finally, the result of calling the enter method is pushed onto
447 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
448 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
449 :opcode:`UNPACK_SEQUENCE`).
450
451
Guido van Rossum04110fb2007-08-24 16:32:05 +0000452.. opcode:: WITH_CLEANUP ()
453
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000454 Cleans up the stack when a :keyword:`with` statement block exits. TOS is
455 the context manager's :meth:`__exit__` bound method. Below TOS are 1--3
456 values indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000457
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000458 * SECOND = ``None``
459 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
460 * SECOND = ``WHY_*``; no retval below it
461 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000462
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000463 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
464 ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000465
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000466 If the stack represents an exception, *and* the function call returns
467 a 'true' value, this information is "zapped" and replaced with a single
468 ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception.
469 (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000470
Georg Brandl9afde1c2007-11-01 20:32:30 +0000471 .. XXX explain the WHY stuff!
472
Guido van Rossum04110fb2007-08-24 16:32:05 +0000473
Georg Brandl5ac22302008-07-20 21:39:03 +0000474.. opcode:: STORE_LOCALS
475
476 Pops TOS from the stack and stores it as the current frame's ``f_locals``.
477 This is used in class construction.
478
479
Georg Brandl116aa622007-08-15 14:28:22 +0000480All of the following opcodes expect arguments. An argument is two bytes, with
481the more significant byte last.
482
Georg Brandl116aa622007-08-15 14:28:22 +0000483.. opcode:: STORE_NAME (namei)
484
485 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Christian Heimes8640e742008-02-23 16:23:06 +0000486 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl116aa622007-08-15 14:28:22 +0000487 or ``STORE_GLOBAL`` if possible.
488
489
490.. opcode:: DELETE_NAME (namei)
491
492 Implements ``del name``, where *namei* is the index into :attr:`co_names`
493 attribute of the code object.
494
495
496.. opcode:: UNPACK_SEQUENCE (count)
497
498 Unpacks TOS into *count* individual values, which are put onto the stack
499 right-to-left.
500
Georg Brandl116aa622007-08-15 14:28:22 +0000501
Georg Brandl5ac22302008-07-20 21:39:03 +0000502.. opcode:: UNPACK_EX (counts)
503
504 Implements assignment with a starred target: Unpacks an iterable in TOS into
505 individual values, where the total number of values can be smaller than the
506 number of items in the iterable: one the new values will be a list of all
507 leftover items.
508
509 The low byte of *counts* is the number of values before the list value, the
510 high byte of *counts* the number of values after it. The resulting values
511 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000512
Georg Brandl5ac22302008-07-20 21:39:03 +0000513
Georg Brandl116aa622007-08-15 14:28:22 +0000514.. opcode:: DUP_TOPX (count)
515
516 Duplicate *count* items, keeping them in the same order. Due to implementation
517 limits, *count* should be between 1 and 5 inclusive.
518
519
520.. opcode:: STORE_ATTR (namei)
521
522 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
523 :attr:`co_names`.
524
525
526.. opcode:: DELETE_ATTR (namei)
527
528 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
529
530
531.. opcode:: STORE_GLOBAL (namei)
532
533 Works as ``STORE_NAME``, but stores the name as a global.
534
535
536.. opcode:: DELETE_GLOBAL (namei)
537
538 Works as ``DELETE_NAME``, but deletes a global name.
539
Georg Brandl116aa622007-08-15 14:28:22 +0000540
541.. opcode:: LOAD_CONST (consti)
542
543 Pushes ``co_consts[consti]`` onto the stack.
544
545
546.. opcode:: LOAD_NAME (namei)
547
548 Pushes the value associated with ``co_names[namei]`` onto the stack.
549
550
551.. opcode:: BUILD_TUPLE (count)
552
553 Creates a tuple consuming *count* items from the stack, and pushes the resulting
554 tuple onto the stack.
555
556
557.. opcode:: BUILD_LIST (count)
558
559 Works as ``BUILD_TUPLE``, but creates a list.
560
561
562.. opcode:: BUILD_SET (count)
563
564 Works as ``BUILD_TUPLE``, but creates a set.
565
566
Christian Heimesa62da1d2008-01-12 19:39:10 +0000567.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000568
Christian Heimesa62da1d2008-01-12 19:39:10 +0000569 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
570 to hold *count* entries.
Georg Brandl116aa622007-08-15 14:28:22 +0000571
572
573.. opcode:: LOAD_ATTR (namei)
574
575 Replaces TOS with ``getattr(TOS, co_names[namei])``.
576
577
578.. opcode:: COMPARE_OP (opname)
579
580 Performs a Boolean operation. The operation name can be found in
581 ``cmp_op[opname]``.
582
583
584.. opcode:: IMPORT_NAME (namei)
585
Christian Heimesa342c012008-04-20 21:01:16 +0000586 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
587 the *fromlist* and *level* arguments of :func:`__import__`. The module
588 object is pushed onto the stack. The current namespace is not affected:
589 for a proper import statement, a subsequent ``STORE_FAST`` instruction
590 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000591
592
593.. opcode:: IMPORT_FROM (namei)
594
595 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
596 resulting object is pushed onto the stack, to be subsequently stored by a
597 ``STORE_FAST`` instruction.
598
599
600.. opcode:: JUMP_FORWARD (delta)
601
Georg Brandl9afde1c2007-11-01 20:32:30 +0000602 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000603
604
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000605.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000606
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000607 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000608
609
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000610.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000611
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000612 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
613
614
615.. opcode:: JUMP_IF_TRUE_OR_POP (target)
616
617 If TOS is true, sets the bytecode counter to *target* and leaves TOS
618 on the stack. Otherwise (TOS is false), TOS is popped.
619
620
621.. opcode:: JUMP_IF_FALSE_OR_POP (target)
622
623 If TOS is false, sets the bytecode counter to *target* and leaves
624 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000625
626
627.. opcode:: JUMP_ABSOLUTE (target)
628
Georg Brandl9afde1c2007-11-01 20:32:30 +0000629 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000630
631
632.. opcode:: FOR_ITER (delta)
633
Georg Brandl9afde1c2007-11-01 20:32:30 +0000634 ``TOS`` is an :term:`iterator`. Call its :meth:`__next__` method. If this
635 yields a new value, push it on the stack (leaving the iterator below it). If
636 the iterator indicates it is exhausted ``TOS`` is popped, and the byte code
637 counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000638
Georg Brandl116aa622007-08-15 14:28:22 +0000639
640.. opcode:: LOAD_GLOBAL (namei)
641
642 Loads the global named ``co_names[namei]`` onto the stack.
643
Georg Brandl116aa622007-08-15 14:28:22 +0000644
645.. opcode:: SETUP_LOOP (delta)
646
647 Pushes a block for a loop onto the block stack. The block spans from the
648 current instruction with a size of *delta* bytes.
649
650
651.. opcode:: SETUP_EXCEPT (delta)
652
653 Pushes a try block from a try-except clause onto the block stack. *delta* points
654 to the first except block.
655
656
657.. opcode:: SETUP_FINALLY (delta)
658
659 Pushes a try block from a try-except clause onto the block stack. *delta* points
660 to the finally block.
661
Christian Heimesa62da1d2008-01-12 19:39:10 +0000662.. opcode:: STORE_MAP ()
663
664 Store a key and value pair in a dictionary. Pops the key and value while leaving
665 the dictionary on the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000666
667.. opcode:: LOAD_FAST (var_num)
668
669 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
670
671
672.. opcode:: STORE_FAST (var_num)
673
674 Stores TOS into the local ``co_varnames[var_num]``.
675
676
677.. opcode:: DELETE_FAST (var_num)
678
679 Deletes local ``co_varnames[var_num]``.
680
681
682.. opcode:: LOAD_CLOSURE (i)
683
684 Pushes a reference to the cell contained in slot *i* of the cell and free
685 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
686 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
687 len(co_cellvars)]``.
688
689
690.. opcode:: LOAD_DEREF (i)
691
692 Loads the cell contained in slot *i* of the cell and free variable storage.
693 Pushes a reference to the object the cell contains on the stack.
694
695
696.. opcode:: STORE_DEREF (i)
697
698 Stores TOS into the cell contained in slot *i* of the cell and free variable
699 storage.
700
701
702.. opcode:: SET_LINENO (lineno)
703
704 This opcode is obsolete.
705
706
707.. opcode:: RAISE_VARARGS (argc)
708
709 Raises an exception. *argc* indicates the number of parameters to the raise
710 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
711 the parameter as TOS1, and the exception as TOS.
712
713
714.. opcode:: CALL_FUNCTION (argc)
715
716 Calls a function. The low byte of *argc* indicates the number of positional
717 parameters, the high byte the number of keyword parameters. On the stack, the
718 opcode finds the keyword parameters first. For each keyword argument, the value
719 is on top of the key. Below the keyword parameters, the positional parameters
720 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl48310cd2009-01-03 21:18:54 +0000721 the function object to call is on the stack. Pops all function arguments, and
Benjamin Peterson206e3072008-10-19 14:07:49 +0000722 the function itself off the stack, and pushes the return value.
Georg Brandl116aa622007-08-15 14:28:22 +0000723
724
725.. opcode:: MAKE_FUNCTION (argc)
726
727 Pushes a new function object on the stack. TOS is the code associated with the
728 function. The function object is defined to have *argc* default parameters,
729 which are found below TOS.
730
731
732.. opcode:: MAKE_CLOSURE (argc)
733
Guido van Rossum04110fb2007-08-24 16:32:05 +0000734 Creates a new function object, sets its *__closure__* slot, and pushes it on
735 the stack. TOS is the code associated with the function, TOS1 the tuple
736 containing cells for the closure's free variables. The function also has
737 *argc* default parameters, which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000738
739
740.. opcode:: BUILD_SLICE (argc)
741
742 .. index:: builtin: slice
743
744 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
745 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000746 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000747
748
749.. opcode:: EXTENDED_ARG (ext)
750
751 Prefixes any opcode which has an argument too big to fit into the default two
752 bytes. *ext* holds two additional bytes which, taken together with the
753 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
754 most-significant bytes.
755
756
757.. opcode:: CALL_FUNCTION_VAR (argc)
758
759 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
760 on the stack contains the variable argument list, followed by keyword and
761 positional arguments.
762
763
764.. opcode:: CALL_FUNCTION_KW (argc)
765
766 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
767 on the stack contains the keyword arguments dictionary, followed by explicit
768 keyword and positional arguments.
769
770
771.. opcode:: CALL_FUNCTION_VAR_KW (argc)
772
773 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
774 element on the stack contains the keyword arguments dictionary, followed by the
775 variable-arguments tuple, followed by explicit keyword and positional arguments.
776
777
778.. opcode:: HAVE_ARGUMENT ()
779
780 This is not really an opcode. It identifies the dividing line between opcodes
781 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
782 HAVE_ARGUMENT``.
783