blob: c9417ead407090b8c78938d503e8dbd926429108 [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
Brett Cannon8315fd12010-07-02 22:03:00 +000013.. warning:: Bytecode is an implementation detail of the CPython interpreter! No guarantees are made that bytecode will not be added, removed, or changed between versions of Python. Use of this module should not be considered to work across Python VMs or Python releases.
14
Georg Brandl116aa622007-08-15 14:28:22 +000015Example: Given the function :func:`myfunc`::
16
17 def myfunc(alist):
18 return len(alist)
19
20the following command can be used to get the disassembly of :func:`myfunc`::
21
22 >>> dis.dis(myfunc)
23 2 0 LOAD_GLOBAL 0 (len)
24 3 LOAD_FAST 0 (alist)
25 6 CALL_FUNCTION 1
26 9 RETURN_VALUE
27
28(The "2" is a line number).
29
30The :mod:`dis` module defines the following functions and constants:
31
32
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000033.. function:: dis(x=None)
Georg Brandl116aa622007-08-15 14:28:22 +000034
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000035 Disassemble the *x* object. *x* can denote either a module, a
Georg Brandl116aa622007-08-15 14:28:22 +000036 class, a method, a function, or a code object. For a module, it disassembles
37 all functions. For a class, it disassembles all methods. For a single code
Georg Brandl9afde1c2007-11-01 20:32:30 +000038 sequence, it prints one line per bytecode instruction. If no object is
Georg Brandl116aa622007-08-15 14:28:22 +000039 provided, it disassembles the last traceback.
40
41
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000042.. function:: distb(tb=None)
Georg Brandl116aa622007-08-15 14:28:22 +000043
44 Disassembles the top-of-stack function of a traceback, using the last traceback
45 if none was passed. The instruction causing the exception is indicated.
46
47
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000048.. function:: disassemble(code, lasti=-1)
49 disco(code, lasti=-1)
Georg Brandl116aa622007-08-15 14:28:22 +000050
51 Disassembles a code object, indicating the last instruction if *lasti* was
52 provided. The output is divided in the following columns:
53
54 #. the line number, for the first instruction of each line
55 #. the current instruction, indicated as ``-->``,
56 #. a labelled instruction, indicated with ``>>``,
57 #. the address of the instruction,
58 #. the operation code name,
59 #. operation parameters, and
60 #. interpretation of the parameters in parentheses.
61
62 The parameter interpretation recognizes local and global variable names,
63 constant values, branch targets, and compare operators.
64
65
Benjamin Peterson75edad02009-01-01 15:05:06 +000066.. function:: findlinestarts(code)
67
68 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
69 attributes of the code object *code* to find the offsets which are starts of
70 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
71
72
73.. function:: findlabels(code)
74
75 Detect all offsets in the code object *code* which are jump targets, and
76 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +000077
78
Georg Brandl116aa622007-08-15 14:28:22 +000079.. data:: opname
80
Georg Brandl9afde1c2007-11-01 20:32:30 +000081 Sequence of operation names, indexable using the bytecode.
Georg Brandl116aa622007-08-15 14:28:22 +000082
83
84.. data:: opmap
85
Georg Brandl9afde1c2007-11-01 20:32:30 +000086 Dictionary mapping bytecodes to operation names.
Georg Brandl116aa622007-08-15 14:28:22 +000087
88
89.. data:: cmp_op
90
91 Sequence of all compare operation names.
92
93
94.. data:: hasconst
95
Georg Brandl9afde1c2007-11-01 20:32:30 +000096 Sequence of bytecodes that have a constant parameter.
Georg Brandl116aa622007-08-15 14:28:22 +000097
98
99.. data:: hasfree
100
Georg Brandl9afde1c2007-11-01 20:32:30 +0000101 Sequence of bytecodes that access a free variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000102
103
104.. data:: hasname
105
Georg Brandl9afde1c2007-11-01 20:32:30 +0000106 Sequence of bytecodes that access an attribute by name.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108
109.. data:: hasjrel
110
Georg Brandl9afde1c2007-11-01 20:32:30 +0000111 Sequence of bytecodes that have a relative jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113
114.. data:: hasjabs
115
Georg Brandl9afde1c2007-11-01 20:32:30 +0000116 Sequence of bytecodes that have an absolute jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118
119.. data:: haslocal
120
Georg Brandl9afde1c2007-11-01 20:32:30 +0000121 Sequence of bytecodes that access a local variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123
124.. data:: hascompare
125
Georg Brandl9afde1c2007-11-01 20:32:30 +0000126 Sequence of bytecodes of Boolean operations.
Georg Brandl116aa622007-08-15 14:28:22 +0000127
128
129.. _bytecodes:
130
Georg Brandl9afde1c2007-11-01 20:32:30 +0000131Python Bytecode Instructions
132----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000133
Georg Brandl9afde1c2007-11-01 20:32:30 +0000134The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136
137.. opcode:: STOP_CODE ()
138
139 Indicates end-of-code to the compiler, not used by the interpreter.
140
141
142.. opcode:: NOP ()
143
144 Do nothing code. Used as a placeholder by the bytecode optimizer.
145
146
147.. opcode:: POP_TOP ()
148
149 Removes the top-of-stack (TOS) item.
150
151
152.. opcode:: ROT_TWO ()
153
154 Swaps the two top-most stack items.
155
156
157.. opcode:: ROT_THREE ()
158
159 Lifts second and third stack item one position up, moves top down to position
160 three.
161
162
163.. opcode:: ROT_FOUR ()
164
165 Lifts second, third and forth stack item one position up, moves top down to
166 position four.
167
168
169.. opcode:: DUP_TOP ()
170
171 Duplicates the reference on top of the stack.
172
173Unary Operations take the top of the stack, apply the operation, and push the
174result back on the stack.
175
176
177.. opcode:: UNARY_POSITIVE ()
178
179 Implements ``TOS = +TOS``.
180
181
182.. opcode:: UNARY_NEGATIVE ()
183
184 Implements ``TOS = -TOS``.
185
186
187.. opcode:: UNARY_NOT ()
188
189 Implements ``TOS = not TOS``.
190
191
192.. opcode:: UNARY_INVERT ()
193
194 Implements ``TOS = ~TOS``.
195
196
197.. opcode:: GET_ITER ()
198
199 Implements ``TOS = iter(TOS)``.
200
201Binary operations remove the top of the stack (TOS) and the second top-most
202stack item (TOS1) from the stack. They perform the operation, and put the
203result back on the stack.
204
205
206.. opcode:: BINARY_POWER ()
207
208 Implements ``TOS = TOS1 ** TOS``.
209
210
211.. opcode:: BINARY_MULTIPLY ()
212
213 Implements ``TOS = TOS1 * TOS``.
214
215
216.. opcode:: BINARY_FLOOR_DIVIDE ()
217
218 Implements ``TOS = TOS1 // TOS``.
219
220
221.. opcode:: BINARY_TRUE_DIVIDE ()
222
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000223 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225
226.. opcode:: BINARY_MODULO ()
227
228 Implements ``TOS = TOS1 % TOS``.
229
230
231.. opcode:: BINARY_ADD ()
232
233 Implements ``TOS = TOS1 + TOS``.
234
235
236.. opcode:: BINARY_SUBTRACT ()
237
238 Implements ``TOS = TOS1 - TOS``.
239
240
241.. opcode:: BINARY_SUBSCR ()
242
243 Implements ``TOS = TOS1[TOS]``.
244
245
246.. opcode:: BINARY_LSHIFT ()
247
248 Implements ``TOS = TOS1 << TOS``.
249
250
251.. opcode:: BINARY_RSHIFT ()
252
253 Implements ``TOS = TOS1 >> TOS``.
254
255
256.. opcode:: BINARY_AND ()
257
258 Implements ``TOS = TOS1 & TOS``.
259
260
261.. opcode:: BINARY_XOR ()
262
263 Implements ``TOS = TOS1 ^ TOS``.
264
265
266.. opcode:: BINARY_OR ()
267
268 Implements ``TOS = TOS1 | TOS``.
269
270In-place operations are like binary operations, in that they remove TOS and
271TOS1, and push the result back on the stack, but the operation is done in-place
272when TOS1 supports it, and the resulting TOS may be (but does not have to be)
273the original TOS1.
274
275
276.. opcode:: INPLACE_POWER ()
277
278 Implements in-place ``TOS = TOS1 ** TOS``.
279
280
281.. opcode:: INPLACE_MULTIPLY ()
282
283 Implements in-place ``TOS = TOS1 * TOS``.
284
285
286.. opcode:: INPLACE_FLOOR_DIVIDE ()
287
288 Implements in-place ``TOS = TOS1 // TOS``.
289
290
291.. opcode:: INPLACE_TRUE_DIVIDE ()
292
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000293 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000294
295
296.. opcode:: INPLACE_MODULO ()
297
298 Implements in-place ``TOS = TOS1 % TOS``.
299
300
301.. opcode:: INPLACE_ADD ()
302
303 Implements in-place ``TOS = TOS1 + TOS``.
304
305
306.. opcode:: INPLACE_SUBTRACT ()
307
308 Implements in-place ``TOS = TOS1 - TOS``.
309
310
311.. opcode:: INPLACE_LSHIFT ()
312
313 Implements in-place ``TOS = TOS1 << TOS``.
314
315
316.. opcode:: INPLACE_RSHIFT ()
317
318 Implements in-place ``TOS = TOS1 >> TOS``.
319
320
321.. opcode:: INPLACE_AND ()
322
323 Implements in-place ``TOS = TOS1 & TOS``.
324
325
326.. opcode:: INPLACE_XOR ()
327
328 Implements in-place ``TOS = TOS1 ^ TOS``.
329
330
331.. opcode:: INPLACE_OR ()
332
333 Implements in-place ``TOS = TOS1 | TOS``.
334
Georg Brandl116aa622007-08-15 14:28:22 +0000335
336.. opcode:: STORE_SUBSCR ()
337
338 Implements ``TOS1[TOS] = TOS2``.
339
340
341.. opcode:: DELETE_SUBSCR ()
342
343 Implements ``del TOS1[TOS]``.
344
345Miscellaneous opcodes.
346
347
348.. opcode:: PRINT_EXPR ()
349
350 Implements the expression statement for the interactive mode. TOS is removed
351 from the stack and printed. In non-interactive mode, an expression statement is
352 terminated with ``POP_STACK``.
353
354
355.. opcode:: BREAK_LOOP ()
356
357 Terminates a loop due to a :keyword:`break` statement.
358
359
360.. opcode:: CONTINUE_LOOP (target)
361
362 Continues a loop due to a :keyword:`continue` statement. *target* is the
363 address to jump to (which should be a ``FOR_ITER`` instruction).
364
365
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000366.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000367
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000368 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000369
370
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000371.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000372
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000373 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
374
375
376.. opcode:: MAP_ADD (i)
377
378 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
379 comprehensions.
380
381
382For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the
383added value or key/value pair is popped off, the container object remains on
384the stack so that it is available for further iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000385
386
Georg Brandl116aa622007-08-15 14:28:22 +0000387.. opcode:: RETURN_VALUE ()
388
389 Returns with TOS to the caller of the function.
390
391
392.. opcode:: YIELD_VALUE ()
393
Georg Brandl9afde1c2007-11-01 20:32:30 +0000394 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000395
396
397.. opcode:: IMPORT_STAR ()
398
399 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
400 local namespace. The module is popped after loading all names. This opcode
401 implements ``from module import *``.
402
403
404.. opcode:: POP_BLOCK ()
405
406 Removes one block from the block stack. Per frame, there is a stack of blocks,
407 denoting nested loops, try statements, and such.
408
409
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000410.. opcode:: POP_EXCEPT ()
411
412 Removes one block from the block stack. The popped block must be an exception
413 handler block, as implicitly created when entering an except handler.
414 In addition to popping extraneous values from the frame stack, the
415 last three popped values are used to restore the exception state.
416
417
Georg Brandl116aa622007-08-15 14:28:22 +0000418.. opcode:: END_FINALLY ()
419
420 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
421 exception has to be re-raised, or whether the function returns, and continues
422 with the outer-next block.
423
424
Benjamin Peterson69164c72008-07-03 14:45:20 +0000425.. opcode:: LOAD_BUILD_CLASS ()
Georg Brandl116aa622007-08-15 14:28:22 +0000426
Georg Brandl5ac22302008-07-20 21:39:03 +0000427 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Benjamin Petersonaac8fd32008-07-20 22:02:26 +0000428 by ``CALL_FUNCTION`` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000429
Guido van Rossum04110fb2007-08-24 16:32:05 +0000430
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000431.. opcode:: SETUP_WITH (delta)
432
433 This opcode performs several operations before a with block starts. First,
434 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
435 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
436 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
437 is pushed. Finally, the result of calling the enter method is pushed onto
438 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
439 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
440 :opcode:`UNPACK_SEQUENCE`).
441
442
Guido van Rossum04110fb2007-08-24 16:32:05 +0000443.. opcode:: WITH_CLEANUP ()
444
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000445 Cleans up the stack when a :keyword:`with` statement block exits. TOS is
446 the context manager's :meth:`__exit__` bound method. Below TOS are 1--3
447 values indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000448
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000449 * SECOND = ``None``
450 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
451 * SECOND = ``WHY_*``; no retval below it
452 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000453
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000454 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
455 ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000456
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000457 If the stack represents an exception, *and* the function call returns
458 a 'true' value, this information is "zapped" and replaced with a single
459 ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception.
460 (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000461
Georg Brandl9afde1c2007-11-01 20:32:30 +0000462 .. XXX explain the WHY stuff!
463
Guido van Rossum04110fb2007-08-24 16:32:05 +0000464
Georg Brandl5ac22302008-07-20 21:39:03 +0000465.. opcode:: STORE_LOCALS
466
467 Pops TOS from the stack and stores it as the current frame's ``f_locals``.
468 This is used in class construction.
469
470
Georg Brandl116aa622007-08-15 14:28:22 +0000471All of the following opcodes expect arguments. An argument is two bytes, with
472the more significant byte last.
473
Georg Brandl116aa622007-08-15 14:28:22 +0000474.. opcode:: STORE_NAME (namei)
475
476 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Christian Heimes8640e742008-02-23 16:23:06 +0000477 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl116aa622007-08-15 14:28:22 +0000478 or ``STORE_GLOBAL`` if possible.
479
480
481.. opcode:: DELETE_NAME (namei)
482
483 Implements ``del name``, where *namei* is the index into :attr:`co_names`
484 attribute of the code object.
485
486
487.. opcode:: UNPACK_SEQUENCE (count)
488
489 Unpacks TOS into *count* individual values, which are put onto the stack
490 right-to-left.
491
Georg Brandl116aa622007-08-15 14:28:22 +0000492
Georg Brandl5ac22302008-07-20 21:39:03 +0000493.. opcode:: UNPACK_EX (counts)
494
495 Implements assignment with a starred target: Unpacks an iterable in TOS into
496 individual values, where the total number of values can be smaller than the
497 number of items in the iterable: one the new values will be a list of all
498 leftover items.
499
500 The low byte of *counts* is the number of values before the list value, the
501 high byte of *counts* the number of values after it. The resulting values
502 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000503
Georg Brandl5ac22302008-07-20 21:39:03 +0000504
Georg Brandl116aa622007-08-15 14:28:22 +0000505.. opcode:: DUP_TOPX (count)
506
507 Duplicate *count* items, keeping them in the same order. Due to implementation
508 limits, *count* should be between 1 and 5 inclusive.
509
510
511.. opcode:: STORE_ATTR (namei)
512
513 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
514 :attr:`co_names`.
515
516
517.. opcode:: DELETE_ATTR (namei)
518
519 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
520
521
522.. opcode:: STORE_GLOBAL (namei)
523
524 Works as ``STORE_NAME``, but stores the name as a global.
525
526
527.. opcode:: DELETE_GLOBAL (namei)
528
529 Works as ``DELETE_NAME``, but deletes a global name.
530
Georg Brandl116aa622007-08-15 14:28:22 +0000531
532.. opcode:: LOAD_CONST (consti)
533
534 Pushes ``co_consts[consti]`` onto the stack.
535
536
537.. opcode:: LOAD_NAME (namei)
538
539 Pushes the value associated with ``co_names[namei]`` onto the stack.
540
541
542.. opcode:: BUILD_TUPLE (count)
543
544 Creates a tuple consuming *count* items from the stack, and pushes the resulting
545 tuple onto the stack.
546
547
548.. opcode:: BUILD_LIST (count)
549
550 Works as ``BUILD_TUPLE``, but creates a list.
551
552
553.. opcode:: BUILD_SET (count)
554
555 Works as ``BUILD_TUPLE``, but creates a set.
556
557
Christian Heimesa62da1d2008-01-12 19:39:10 +0000558.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000559
Christian Heimesa62da1d2008-01-12 19:39:10 +0000560 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
561 to hold *count* entries.
Georg Brandl116aa622007-08-15 14:28:22 +0000562
563
564.. opcode:: LOAD_ATTR (namei)
565
566 Replaces TOS with ``getattr(TOS, co_names[namei])``.
567
568
569.. opcode:: COMPARE_OP (opname)
570
571 Performs a Boolean operation. The operation name can be found in
572 ``cmp_op[opname]``.
573
574
575.. opcode:: IMPORT_NAME (namei)
576
Christian Heimesa342c012008-04-20 21:01:16 +0000577 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
578 the *fromlist* and *level* arguments of :func:`__import__`. The module
579 object is pushed onto the stack. The current namespace is not affected:
580 for a proper import statement, a subsequent ``STORE_FAST`` instruction
581 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000582
583
584.. opcode:: IMPORT_FROM (namei)
585
586 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
587 resulting object is pushed onto the stack, to be subsequently stored by a
588 ``STORE_FAST`` instruction.
589
590
591.. opcode:: JUMP_FORWARD (delta)
592
Georg Brandl9afde1c2007-11-01 20:32:30 +0000593 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000594
595
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000596.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000597
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000598 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000599
600
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000601.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000602
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000603 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
604
605
606.. opcode:: JUMP_IF_TRUE_OR_POP (target)
607
608 If TOS is true, sets the bytecode counter to *target* and leaves TOS
609 on the stack. Otherwise (TOS is false), TOS is popped.
610
611
612.. opcode:: JUMP_IF_FALSE_OR_POP (target)
613
614 If TOS is false, sets the bytecode counter to *target* and leaves
615 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000616
617
618.. opcode:: JUMP_ABSOLUTE (target)
619
Georg Brandl9afde1c2007-11-01 20:32:30 +0000620 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000621
622
623.. opcode:: FOR_ITER (delta)
624
Georg Brandl9afde1c2007-11-01 20:32:30 +0000625 ``TOS`` is an :term:`iterator`. Call its :meth:`__next__` method. If this
626 yields a new value, push it on the stack (leaving the iterator below it). If
627 the iterator indicates it is exhausted ``TOS`` is popped, and the byte code
628 counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000629
Georg Brandl116aa622007-08-15 14:28:22 +0000630
631.. opcode:: LOAD_GLOBAL (namei)
632
633 Loads the global named ``co_names[namei]`` onto the stack.
634
Georg Brandl116aa622007-08-15 14:28:22 +0000635
636.. opcode:: SETUP_LOOP (delta)
637
638 Pushes a block for a loop onto the block stack. The block spans from the
639 current instruction with a size of *delta* bytes.
640
641
642.. opcode:: SETUP_EXCEPT (delta)
643
644 Pushes a try block from a try-except clause onto the block stack. *delta* points
645 to the first except block.
646
647
648.. opcode:: SETUP_FINALLY (delta)
649
650 Pushes a try block from a try-except clause onto the block stack. *delta* points
651 to the finally block.
652
Christian Heimesa62da1d2008-01-12 19:39:10 +0000653.. opcode:: STORE_MAP ()
654
655 Store a key and value pair in a dictionary. Pops the key and value while leaving
656 the dictionary on the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000657
658.. opcode:: LOAD_FAST (var_num)
659
660 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
661
662
663.. opcode:: STORE_FAST (var_num)
664
665 Stores TOS into the local ``co_varnames[var_num]``.
666
667
668.. opcode:: DELETE_FAST (var_num)
669
670 Deletes local ``co_varnames[var_num]``.
671
672
673.. opcode:: LOAD_CLOSURE (i)
674
675 Pushes a reference to the cell contained in slot *i* of the cell and free
676 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
677 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
678 len(co_cellvars)]``.
679
680
681.. opcode:: LOAD_DEREF (i)
682
683 Loads the cell contained in slot *i* of the cell and free variable storage.
684 Pushes a reference to the object the cell contains on the stack.
685
686
687.. opcode:: STORE_DEREF (i)
688
689 Stores TOS into the cell contained in slot *i* of the cell and free variable
690 storage.
691
692
693.. opcode:: SET_LINENO (lineno)
694
695 This opcode is obsolete.
696
697
698.. opcode:: RAISE_VARARGS (argc)
699
700 Raises an exception. *argc* indicates the number of parameters to the raise
701 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
702 the parameter as TOS1, and the exception as TOS.
703
704
705.. opcode:: CALL_FUNCTION (argc)
706
707 Calls a function. The low byte of *argc* indicates the number of positional
708 parameters, the high byte the number of keyword parameters. On the stack, the
709 opcode finds the keyword parameters first. For each keyword argument, the value
710 is on top of the key. Below the keyword parameters, the positional parameters
711 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl48310cd2009-01-03 21:18:54 +0000712 the function object to call is on the stack. Pops all function arguments, and
Benjamin Peterson206e3072008-10-19 14:07:49 +0000713 the function itself off the stack, and pushes the return value.
Georg Brandl116aa622007-08-15 14:28:22 +0000714
715
716.. opcode:: MAKE_FUNCTION (argc)
717
718 Pushes a new function object on the stack. TOS is the code associated with the
719 function. The function object is defined to have *argc* default parameters,
720 which are found below TOS.
721
722
723.. opcode:: MAKE_CLOSURE (argc)
724
Guido van Rossum04110fb2007-08-24 16:32:05 +0000725 Creates a new function object, sets its *__closure__* slot, and pushes it on
726 the stack. TOS is the code associated with the function, TOS1 the tuple
727 containing cells for the closure's free variables. The function also has
728 *argc* default parameters, which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000729
730
731.. opcode:: BUILD_SLICE (argc)
732
733 .. index:: builtin: slice
734
735 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
736 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000737 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000738
739
740.. opcode:: EXTENDED_ARG (ext)
741
742 Prefixes any opcode which has an argument too big to fit into the default two
743 bytes. *ext* holds two additional bytes which, taken together with the
744 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
745 most-significant bytes.
746
747
748.. opcode:: CALL_FUNCTION_VAR (argc)
749
750 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
751 on the stack contains the variable argument list, followed by keyword and
752 positional arguments.
753
754
755.. opcode:: CALL_FUNCTION_KW (argc)
756
757 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
758 on the stack contains the keyword arguments dictionary, followed by explicit
759 keyword and positional arguments.
760
761
762.. opcode:: CALL_FUNCTION_VAR_KW (argc)
763
764 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
765 element on the stack contains the keyword arguments dictionary, followed by the
766 variable-arguments tuple, followed by explicit keyword and positional arguments.
767
768
769.. opcode:: HAVE_ARGUMENT ()
770
771 This is not really an opcode. It identifies the dividing line between opcodes
772 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
773 HAVE_ARGUMENT``.
774