blob: c5a7ea8fc304b510029f75d7b3d40362a3b36ff8 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
Georg Brandl9afde1c2007-11-01 20:32:30 +00002:mod:`dis` --- Disassembler for Python bytecode
3===============================================
Georg Brandl116aa622007-08-15 14:28:22 +00004
5.. module:: dis
Georg Brandl9afde1c2007-11-01 20:32:30 +00006 :synopsis: Disassembler for Python bytecode.
Georg Brandl116aa622007-08-15 14:28:22 +00007
8
Georg Brandl9afde1c2007-11-01 20:32:30 +00009The :mod:`dis` module supports the analysis of Python :term:`bytecode` by disassembling
Georg Brandl116aa622007-08-15 14:28:22 +000010it. Since there is no Python assembler, this module defines the Python assembly
Georg Brandl9afde1c2007-11-01 20:32:30 +000011language. The Python bytecode which this module takes as an input is defined
Georg Brandl116aa622007-08-15 14:28:22 +000012in the file :file:`Include/opcode.h` and used by the compiler and the
13interpreter.
14
15Example: 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
223 Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is in
224 effect.
225
226
227.. opcode:: BINARY_MODULO ()
228
229 Implements ``TOS = TOS1 % TOS``.
230
231
232.. opcode:: BINARY_ADD ()
233
234 Implements ``TOS = TOS1 + TOS``.
235
236
237.. opcode:: BINARY_SUBTRACT ()
238
239 Implements ``TOS = TOS1 - TOS``.
240
241
242.. opcode:: BINARY_SUBSCR ()
243
244 Implements ``TOS = TOS1[TOS]``.
245
246
247.. opcode:: BINARY_LSHIFT ()
248
249 Implements ``TOS = TOS1 << TOS``.
250
251
252.. opcode:: BINARY_RSHIFT ()
253
254 Implements ``TOS = TOS1 >> TOS``.
255
256
257.. opcode:: BINARY_AND ()
258
259 Implements ``TOS = TOS1 & TOS``.
260
261
262.. opcode:: BINARY_XOR ()
263
264 Implements ``TOS = TOS1 ^ TOS``.
265
266
267.. opcode:: BINARY_OR ()
268
269 Implements ``TOS = TOS1 | TOS``.
270
271In-place operations are like binary operations, in that they remove TOS and
272TOS1, and push the result back on the stack, but the operation is done in-place
273when TOS1 supports it, and the resulting TOS may be (but does not have to be)
274the original TOS1.
275
276
277.. opcode:: INPLACE_POWER ()
278
279 Implements in-place ``TOS = TOS1 ** TOS``.
280
281
282.. opcode:: INPLACE_MULTIPLY ()
283
284 Implements in-place ``TOS = TOS1 * TOS``.
285
286
287.. opcode:: INPLACE_FLOOR_DIVIDE ()
288
289 Implements in-place ``TOS = TOS1 // TOS``.
290
291
292.. opcode:: INPLACE_TRUE_DIVIDE ()
293
294 Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
295 division`` is in effect.
296
297
298.. opcode:: INPLACE_MODULO ()
299
300 Implements in-place ``TOS = TOS1 % TOS``.
301
302
303.. opcode:: INPLACE_ADD ()
304
305 Implements in-place ``TOS = TOS1 + TOS``.
306
307
308.. opcode:: INPLACE_SUBTRACT ()
309
310 Implements in-place ``TOS = TOS1 - TOS``.
311
312
313.. opcode:: INPLACE_LSHIFT ()
314
315 Implements in-place ``TOS = TOS1 << TOS``.
316
317
318.. opcode:: INPLACE_RSHIFT ()
319
320 Implements in-place ``TOS = TOS1 >> TOS``.
321
322
323.. opcode:: INPLACE_AND ()
324
325 Implements in-place ``TOS = TOS1 & TOS``.
326
327
328.. opcode:: INPLACE_XOR ()
329
330 Implements in-place ``TOS = TOS1 ^ TOS``.
331
332
333.. opcode:: INPLACE_OR ()
334
335 Implements in-place ``TOS = TOS1 | TOS``.
336
Georg Brandl116aa622007-08-15 14:28:22 +0000337
338.. opcode:: STORE_SUBSCR ()
339
340 Implements ``TOS1[TOS] = TOS2``.
341
342
343.. opcode:: DELETE_SUBSCR ()
344
345 Implements ``del TOS1[TOS]``.
346
347Miscellaneous opcodes.
348
349
350.. opcode:: PRINT_EXPR ()
351
352 Implements the expression statement for the interactive mode. TOS is removed
353 from the stack and printed. In non-interactive mode, an expression statement is
354 terminated with ``POP_STACK``.
355
356
357.. opcode:: BREAK_LOOP ()
358
359 Terminates a loop due to a :keyword:`break` statement.
360
361
362.. opcode:: CONTINUE_LOOP (target)
363
364 Continues a loop due to a :keyword:`continue` statement. *target* is the
365 address to jump to (which should be a ``FOR_ITER`` instruction).
366
367
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000368.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000369
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000370 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000371
372
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000373.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000374
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000375 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
376
377
378.. opcode:: MAP_ADD (i)
379
380 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
381 comprehensions.
382
383
384For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the
385added value or key/value pair is popped off, the container object remains on
386the stack so that it is available for further iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000387
388
389.. opcode:: LOAD_LOCALS ()
390
391 Pushes a reference to the locals of the current scope on the stack. This is used
392 in the code for a class definition: After the class body is evaluated, the
393 locals are passed to the class definition.
394
395
396.. 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
440.. opcode:: WITH_CLEANUP ()
441
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000442 Cleans up the stack when a :keyword:`with` statement block exits. TOS is
443 the context manager's :meth:`__exit__` bound method. Below TOS are 1--3
444 values indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000445
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000446 * SECOND = ``None``
447 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
448 * SECOND = ``WHY_*``; no retval below it
449 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000450
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000451 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
452 ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000453
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000454 If the stack represents an exception, *and* the function call returns
455 a 'true' value, this information is "zapped" and replaced with a single
456 ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception.
457 (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000458
Georg Brandl9afde1c2007-11-01 20:32:30 +0000459 .. XXX explain the WHY stuff!
460
Guido van Rossum04110fb2007-08-24 16:32:05 +0000461
Georg Brandl5ac22302008-07-20 21:39:03 +0000462.. opcode:: STORE_LOCALS
463
464 Pops TOS from the stack and stores it as the current frame's ``f_locals``.
465 This is used in class construction.
466
467
Georg Brandl116aa622007-08-15 14:28:22 +0000468All of the following opcodes expect arguments. An argument is two bytes, with
469the more significant byte last.
470
Georg Brandl116aa622007-08-15 14:28:22 +0000471.. opcode:: STORE_NAME (namei)
472
473 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Christian Heimes8640e742008-02-23 16:23:06 +0000474 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl116aa622007-08-15 14:28:22 +0000475 or ``STORE_GLOBAL`` if possible.
476
477
478.. opcode:: DELETE_NAME (namei)
479
480 Implements ``del name``, where *namei* is the index into :attr:`co_names`
481 attribute of the code object.
482
483
484.. opcode:: UNPACK_SEQUENCE (count)
485
486 Unpacks TOS into *count* individual values, which are put onto the stack
487 right-to-left.
488
Georg Brandl116aa622007-08-15 14:28:22 +0000489
Georg Brandl5ac22302008-07-20 21:39:03 +0000490.. opcode:: UNPACK_EX (counts)
491
492 Implements assignment with a starred target: Unpacks an iterable in TOS into
493 individual values, where the total number of values can be smaller than the
494 number of items in the iterable: one the new values will be a list of all
495 leftover items.
496
497 The low byte of *counts* is the number of values before the list value, the
498 high byte of *counts* the number of values after it. The resulting values
499 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000500
Georg Brandl5ac22302008-07-20 21:39:03 +0000501
Georg Brandl116aa622007-08-15 14:28:22 +0000502.. opcode:: DUP_TOPX (count)
503
504 Duplicate *count* items, keeping them in the same order. Due to implementation
505 limits, *count* should be between 1 and 5 inclusive.
506
507
508.. opcode:: STORE_ATTR (namei)
509
510 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
511 :attr:`co_names`.
512
513
514.. opcode:: DELETE_ATTR (namei)
515
516 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
517
518
519.. opcode:: STORE_GLOBAL (namei)
520
521 Works as ``STORE_NAME``, but stores the name as a global.
522
523
524.. opcode:: DELETE_GLOBAL (namei)
525
526 Works as ``DELETE_NAME``, but deletes a global name.
527
Georg Brandl116aa622007-08-15 14:28:22 +0000528
529.. opcode:: LOAD_CONST (consti)
530
531 Pushes ``co_consts[consti]`` onto the stack.
532
533
534.. opcode:: LOAD_NAME (namei)
535
536 Pushes the value associated with ``co_names[namei]`` onto the stack.
537
538
539.. opcode:: BUILD_TUPLE (count)
540
541 Creates a tuple consuming *count* items from the stack, and pushes the resulting
542 tuple onto the stack.
543
544
545.. opcode:: BUILD_LIST (count)
546
547 Works as ``BUILD_TUPLE``, but creates a list.
548
549
550.. opcode:: BUILD_SET (count)
551
552 Works as ``BUILD_TUPLE``, but creates a set.
553
554
Christian Heimesa62da1d2008-01-12 19:39:10 +0000555.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000556
Christian Heimesa62da1d2008-01-12 19:39:10 +0000557 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
558 to hold *count* entries.
Georg Brandl116aa622007-08-15 14:28:22 +0000559
560
561.. opcode:: LOAD_ATTR (namei)
562
563 Replaces TOS with ``getattr(TOS, co_names[namei])``.
564
565
566.. opcode:: COMPARE_OP (opname)
567
568 Performs a Boolean operation. The operation name can be found in
569 ``cmp_op[opname]``.
570
571
572.. opcode:: IMPORT_NAME (namei)
573
Christian Heimesa342c012008-04-20 21:01:16 +0000574 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
575 the *fromlist* and *level* arguments of :func:`__import__`. The module
576 object is pushed onto the stack. The current namespace is not affected:
577 for a proper import statement, a subsequent ``STORE_FAST`` instruction
578 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000579
580
581.. opcode:: IMPORT_FROM (namei)
582
583 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
584 resulting object is pushed onto the stack, to be subsequently stored by a
585 ``STORE_FAST`` instruction.
586
587
588.. opcode:: JUMP_FORWARD (delta)
589
Georg Brandl9afde1c2007-11-01 20:32:30 +0000590 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000591
592
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000593.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000594
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000595 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000596
597
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000598.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000599
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000600 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
601
602
603.. opcode:: JUMP_IF_TRUE_OR_POP (target)
604
605 If TOS is true, sets the bytecode counter to *target* and leaves TOS
606 on the stack. Otherwise (TOS is false), TOS is popped.
607
608
609.. opcode:: JUMP_IF_FALSE_OR_POP (target)
610
611 If TOS is false, sets the bytecode counter to *target* and leaves
612 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000613
614
615.. opcode:: JUMP_ABSOLUTE (target)
616
Georg Brandl9afde1c2007-11-01 20:32:30 +0000617 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000618
619
620.. opcode:: FOR_ITER (delta)
621
Georg Brandl9afde1c2007-11-01 20:32:30 +0000622 ``TOS`` is an :term:`iterator`. Call its :meth:`__next__` method. If this
623 yields a new value, push it on the stack (leaving the iterator below it). If
624 the iterator indicates it is exhausted ``TOS`` is popped, and the byte code
625 counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000626
Georg Brandl116aa622007-08-15 14:28:22 +0000627
628.. opcode:: LOAD_GLOBAL (namei)
629
630 Loads the global named ``co_names[namei]`` onto the stack.
631
Georg Brandl116aa622007-08-15 14:28:22 +0000632
633.. opcode:: SETUP_LOOP (delta)
634
635 Pushes a block for a loop onto the block stack. The block spans from the
636 current instruction with a size of *delta* bytes.
637
638
639.. opcode:: SETUP_EXCEPT (delta)
640
641 Pushes a try block from a try-except clause onto the block stack. *delta* points
642 to the first except block.
643
644
645.. opcode:: SETUP_FINALLY (delta)
646
647 Pushes a try block from a try-except clause onto the block stack. *delta* points
648 to the finally block.
649
Christian Heimesa62da1d2008-01-12 19:39:10 +0000650.. opcode:: STORE_MAP ()
651
652 Store a key and value pair in a dictionary. Pops the key and value while leaving
653 the dictionary on the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000654
655.. opcode:: LOAD_FAST (var_num)
656
657 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
658
659
660.. opcode:: STORE_FAST (var_num)
661
662 Stores TOS into the local ``co_varnames[var_num]``.
663
664
665.. opcode:: DELETE_FAST (var_num)
666
667 Deletes local ``co_varnames[var_num]``.
668
669
670.. opcode:: LOAD_CLOSURE (i)
671
672 Pushes a reference to the cell contained in slot *i* of the cell and free
673 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
674 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
675 len(co_cellvars)]``.
676
677
678.. opcode:: LOAD_DEREF (i)
679
680 Loads the cell contained in slot *i* of the cell and free variable storage.
681 Pushes a reference to the object the cell contains on the stack.
682
683
684.. opcode:: STORE_DEREF (i)
685
686 Stores TOS into the cell contained in slot *i* of the cell and free variable
687 storage.
688
689
690.. opcode:: SET_LINENO (lineno)
691
692 This opcode is obsolete.
693
694
695.. opcode:: RAISE_VARARGS (argc)
696
697 Raises an exception. *argc* indicates the number of parameters to the raise
698 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
699 the parameter as TOS1, and the exception as TOS.
700
701
702.. opcode:: CALL_FUNCTION (argc)
703
704 Calls a function. The low byte of *argc* indicates the number of positional
705 parameters, the high byte the number of keyword parameters. On the stack, the
706 opcode finds the keyword parameters first. For each keyword argument, the value
707 is on top of the key. Below the keyword parameters, the positional parameters
708 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl48310cd2009-01-03 21:18:54 +0000709 the function object to call is on the stack. Pops all function arguments, and
Benjamin Peterson206e3072008-10-19 14:07:49 +0000710 the function itself off the stack, and pushes the return value.
Georg Brandl116aa622007-08-15 14:28:22 +0000711
712
713.. opcode:: MAKE_FUNCTION (argc)
714
715 Pushes a new function object on the stack. TOS is the code associated with the
716 function. The function object is defined to have *argc* default parameters,
717 which are found below TOS.
718
719
720.. opcode:: MAKE_CLOSURE (argc)
721
Guido van Rossum04110fb2007-08-24 16:32:05 +0000722 Creates a new function object, sets its *__closure__* slot, and pushes it on
723 the stack. TOS is the code associated with the function, TOS1 the tuple
724 containing cells for the closure's free variables. The function also has
725 *argc* default parameters, which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000726
727
728.. opcode:: BUILD_SLICE (argc)
729
730 .. index:: builtin: slice
731
732 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
733 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000734 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000735
736
737.. opcode:: EXTENDED_ARG (ext)
738
739 Prefixes any opcode which has an argument too big to fit into the default two
740 bytes. *ext* holds two additional bytes which, taken together with the
741 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
742 most-significant bytes.
743
744
745.. opcode:: CALL_FUNCTION_VAR (argc)
746
747 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
748 on the stack contains the variable argument list, followed by keyword and
749 positional arguments.
750
751
752.. opcode:: CALL_FUNCTION_KW (argc)
753
754 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
755 on the stack contains the keyword arguments dictionary, followed by explicit
756 keyword and positional arguments.
757
758
759.. opcode:: CALL_FUNCTION_VAR_KW (argc)
760
761 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
762 element on the stack contains the keyword arguments dictionary, followed by the
763 variable-arguments tuple, followed by explicit keyword and positional arguments.
764
765
766.. opcode:: HAVE_ARGUMENT ()
767
768 This is not really an opcode. It identifies the dividing line between opcodes
769 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
770 HAVE_ARGUMENT``.
771