blob: e3f30cb0d4a5fdf1a1691ce35edd5517de5233a9 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
Georg Brandl63fa1682007-10-21 10:24:20 +00002:mod:`dis` --- Disassembler for Python bytecode
3===============================================
Georg Brandl8ec7f652007-08-15 14:28:01 +00004
5.. module:: dis
Georg Brandl63fa1682007-10-21 10:24:20 +00006 :synopsis: Disassembler for Python bytecode.
Georg Brandl8ec7f652007-08-15 14:28:01 +00007
8
Georg Brandl5c367462010-08-01 22:26:02 +00009The :mod:`dis` module supports the analysis of CPython :term:`bytecode` by
10disassembling it. The CPython bytecode which this module takes as an
11input is defined in the file :file:`Include/opcode.h` and used by the compiler
12and the interpreter.
13
14.. impl-detail::
15
16 Bytecode is an implementation detail of the CPython interpreter! No
17 guarantees are made that bytecode will not be added, removed, or changed
18 between versions of Python. Use of this module should not be considered to
19 work across Python VMs or Python releases.
Georg Brandl8ec7f652007-08-15 14:28:01 +000020
21Example: Given the function :func:`myfunc`::
22
23 def myfunc(alist):
24 return len(alist)
25
26the following command can be used to get the disassembly of :func:`myfunc`::
27
28 >>> dis.dis(myfunc)
29 2 0 LOAD_GLOBAL 0 (len)
30 3 LOAD_FAST 0 (alist)
31 6 CALL_FUNCTION 1
32 9 RETURN_VALUE
33
34(The "2" is a line number).
35
36The :mod:`dis` module defines the following functions and constants:
37
38
39.. function:: dis([bytesource])
40
41 Disassemble the *bytesource* object. *bytesource* can denote either a module, a
42 class, a method, a function, or a code object. For a module, it disassembles
43 all functions. For a class, it disassembles all methods. For a single code
Georg Brandl63fa1682007-10-21 10:24:20 +000044 sequence, it prints one line per bytecode instruction. If no object is
Georg Brandl8ec7f652007-08-15 14:28:01 +000045 provided, it disassembles the last traceback.
46
47
48.. function:: distb([tb])
49
50 Disassembles the top-of-stack function of a traceback, using the last traceback
51 if none was passed. The instruction causing the exception is indicated.
52
53
54.. function:: disassemble(code[, lasti])
55
56 Disassembles a code object, indicating the last instruction if *lasti* was
57 provided. The output is divided in the following columns:
58
59 #. the line number, for the first instruction of each line
60 #. the current instruction, indicated as ``-->``,
61 #. a labelled instruction, indicated with ``>>``,
62 #. the address of the instruction,
63 #. the operation code name,
64 #. operation parameters, and
65 #. interpretation of the parameters in parentheses.
66
67 The parameter interpretation recognizes local and global variable names,
68 constant values, branch targets, and compare operators.
69
70
71.. function:: disco(code[, lasti])
72
73 A synonym for disassemble. It is more convenient to type, and kept for
74 compatibility with earlier Python releases.
75
76
77.. data:: opname
78
Georg Brandl63fa1682007-10-21 10:24:20 +000079 Sequence of operation names, indexable using the bytecode.
Georg Brandl8ec7f652007-08-15 14:28:01 +000080
81
82.. data:: opmap
83
Georg Brandl63fa1682007-10-21 10:24:20 +000084 Dictionary mapping bytecodes to operation names.
Georg Brandl8ec7f652007-08-15 14:28:01 +000085
86
87.. data:: cmp_op
88
89 Sequence of all compare operation names.
90
91
92.. data:: hasconst
93
Georg Brandl63fa1682007-10-21 10:24:20 +000094 Sequence of bytecodes that have a constant parameter.
Georg Brandl8ec7f652007-08-15 14:28:01 +000095
96
97.. data:: hasfree
98
Georg Brandl63fa1682007-10-21 10:24:20 +000099 Sequence of bytecodes that access a free variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000100
101
102.. data:: hasname
103
Georg Brandl63fa1682007-10-21 10:24:20 +0000104 Sequence of bytecodes that access an attribute by name.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000105
106
107.. data:: hasjrel
108
Georg Brandl63fa1682007-10-21 10:24:20 +0000109 Sequence of bytecodes that have a relative jump target.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000110
111
112.. data:: hasjabs
113
Georg Brandl63fa1682007-10-21 10:24:20 +0000114 Sequence of bytecodes that have an absolute jump target.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000115
116
117.. data:: haslocal
118
Georg Brandl63fa1682007-10-21 10:24:20 +0000119 Sequence of bytecodes that access a local variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000120
121
122.. data:: hascompare
123
Georg Brandl63fa1682007-10-21 10:24:20 +0000124 Sequence of bytecodes of Boolean operations.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000125
126
127.. _bytecodes:
128
Georg Brandl63fa1682007-10-21 10:24:20 +0000129Python Bytecode Instructions
130----------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000131
Georg Brandl63fa1682007-10-21 10:24:20 +0000132The Python compiler currently generates the following bytecode instructions.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000133
134
135.. opcode:: STOP_CODE ()
136
137 Indicates end-of-code to the compiler, not used by the interpreter.
138
139
140.. opcode:: NOP ()
141
142 Do nothing code. Used as a placeholder by the bytecode optimizer.
143
144
145.. opcode:: POP_TOP ()
146
147 Removes the top-of-stack (TOS) item.
148
149
150.. opcode:: ROT_TWO ()
151
152 Swaps the two top-most stack items.
153
154
155.. opcode:: ROT_THREE ()
156
157 Lifts second and third stack item one position up, moves top down to position
158 three.
159
160
161.. opcode:: ROT_FOUR ()
162
163 Lifts second, third and forth stack item one position up, moves top down to
164 position four.
165
166
167.. opcode:: DUP_TOP ()
168
169 Duplicates the reference on top of the stack.
170
171Unary Operations take the top of the stack, apply the operation, and push the
172result back on the stack.
173
174
175.. opcode:: UNARY_POSITIVE ()
176
177 Implements ``TOS = +TOS``.
178
179
180.. opcode:: UNARY_NEGATIVE ()
181
182 Implements ``TOS = -TOS``.
183
184
185.. opcode:: UNARY_NOT ()
186
187 Implements ``TOS = not TOS``.
188
189
190.. opcode:: UNARY_CONVERT ()
191
192 Implements ``TOS = `TOS```.
193
194
195.. opcode:: UNARY_INVERT ()
196
197 Implements ``TOS = ~TOS``.
198
199
200.. opcode:: GET_ITER ()
201
202 Implements ``TOS = iter(TOS)``.
203
204Binary operations remove the top of the stack (TOS) and the second top-most
205stack item (TOS1) from the stack. They perform the operation, and put the
206result back on the stack.
207
208
209.. opcode:: BINARY_POWER ()
210
211 Implements ``TOS = TOS1 ** TOS``.
212
213
214.. opcode:: BINARY_MULTIPLY ()
215
216 Implements ``TOS = TOS1 * TOS``.
217
218
219.. opcode:: BINARY_DIVIDE ()
220
221 Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is not
222 in effect.
223
224
225.. opcode:: BINARY_FLOOR_DIVIDE ()
226
227 Implements ``TOS = TOS1 // TOS``.
228
229
230.. opcode:: BINARY_TRUE_DIVIDE ()
231
232 Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is in
233 effect.
234
235
236.. opcode:: BINARY_MODULO ()
237
238 Implements ``TOS = TOS1 % TOS``.
239
240
241.. opcode:: BINARY_ADD ()
242
243 Implements ``TOS = TOS1 + TOS``.
244
245
246.. opcode:: BINARY_SUBTRACT ()
247
248 Implements ``TOS = TOS1 - TOS``.
249
250
251.. opcode:: BINARY_SUBSCR ()
252
253 Implements ``TOS = TOS1[TOS]``.
254
255
256.. opcode:: BINARY_LSHIFT ()
257
258 Implements ``TOS = TOS1 << TOS``.
259
260
261.. opcode:: BINARY_RSHIFT ()
262
263 Implements ``TOS = TOS1 >> TOS``.
264
265
266.. opcode:: BINARY_AND ()
267
268 Implements ``TOS = TOS1 & TOS``.
269
270
271.. opcode:: BINARY_XOR ()
272
273 Implements ``TOS = TOS1 ^ TOS``.
274
275
276.. opcode:: BINARY_OR ()
277
278 Implements ``TOS = TOS1 | TOS``.
279
280In-place operations are like binary operations, in that they remove TOS and
281TOS1, and push the result back on the stack, but the operation is done in-place
282when TOS1 supports it, and the resulting TOS may be (but does not have to be)
283the original TOS1.
284
285
286.. opcode:: INPLACE_POWER ()
287
288 Implements in-place ``TOS = TOS1 ** TOS``.
289
290
291.. opcode:: INPLACE_MULTIPLY ()
292
293 Implements in-place ``TOS = TOS1 * TOS``.
294
295
296.. opcode:: INPLACE_DIVIDE ()
297
298 Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
299 division`` is not in effect.
300
301
302.. opcode:: INPLACE_FLOOR_DIVIDE ()
303
304 Implements in-place ``TOS = TOS1 // TOS``.
305
306
307.. opcode:: INPLACE_TRUE_DIVIDE ()
308
309 Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
310 division`` is in effect.
311
312
313.. opcode:: INPLACE_MODULO ()
314
315 Implements in-place ``TOS = TOS1 % TOS``.
316
317
318.. opcode:: INPLACE_ADD ()
319
320 Implements in-place ``TOS = TOS1 + TOS``.
321
322
323.. opcode:: INPLACE_SUBTRACT ()
324
325 Implements in-place ``TOS = TOS1 - TOS``.
326
327
328.. opcode:: INPLACE_LSHIFT ()
329
330 Implements in-place ``TOS = TOS1 << TOS``.
331
332
333.. opcode:: INPLACE_RSHIFT ()
334
335 Implements in-place ``TOS = TOS1 >> TOS``.
336
337
338.. opcode:: INPLACE_AND ()
339
340 Implements in-place ``TOS = TOS1 & TOS``.
341
342
343.. opcode:: INPLACE_XOR ()
344
345 Implements in-place ``TOS = TOS1 ^ TOS``.
346
347
348.. opcode:: INPLACE_OR ()
349
350 Implements in-place ``TOS = TOS1 | TOS``.
351
352The slice opcodes take up to three parameters.
353
354
355.. opcode:: SLICE+0 ()
356
357 Implements ``TOS = TOS[:]``.
358
359
360.. opcode:: SLICE+1 ()
361
362 Implements ``TOS = TOS1[TOS:]``.
363
364
365.. opcode:: SLICE+2 ()
366
367 Implements ``TOS = TOS1[:TOS]``.
368
369
370.. opcode:: SLICE+3 ()
371
372 Implements ``TOS = TOS2[TOS1:TOS]``.
373
374Slice assignment needs even an additional parameter. As any statement, they put
375nothing on the stack.
376
377
378.. opcode:: STORE_SLICE+0 ()
379
380 Implements ``TOS[:] = TOS1``.
381
382
383.. opcode:: STORE_SLICE+1 ()
384
385 Implements ``TOS1[TOS:] = TOS2``.
386
387
388.. opcode:: STORE_SLICE+2 ()
389
390 Implements ``TOS1[:TOS] = TOS2``.
391
392
393.. opcode:: STORE_SLICE+3 ()
394
395 Implements ``TOS2[TOS1:TOS] = TOS3``.
396
397
398.. opcode:: DELETE_SLICE+0 ()
399
400 Implements ``del TOS[:]``.
401
402
403.. opcode:: DELETE_SLICE+1 ()
404
405 Implements ``del TOS1[TOS:]``.
406
407
408.. opcode:: DELETE_SLICE+2 ()
409
410 Implements ``del TOS1[:TOS]``.
411
412
413.. opcode:: DELETE_SLICE+3 ()
414
415 Implements ``del TOS2[TOS1:TOS]``.
416
417
418.. opcode:: STORE_SUBSCR ()
419
420 Implements ``TOS1[TOS] = TOS2``.
421
422
423.. opcode:: DELETE_SUBSCR ()
424
425 Implements ``del TOS1[TOS]``.
426
427Miscellaneous opcodes.
428
429
430.. opcode:: PRINT_EXPR ()
431
432 Implements the expression statement for the interactive mode. TOS is removed
433 from the stack and printed. In non-interactive mode, an expression statement is
434 terminated with ``POP_STACK``.
435
436
437.. opcode:: PRINT_ITEM ()
438
439 Prints TOS to the file-like object bound to ``sys.stdout``. There is one such
440 instruction for each item in the :keyword:`print` statement.
441
442
443.. opcode:: PRINT_ITEM_TO ()
444
445 Like ``PRINT_ITEM``, but prints the item second from TOS to the file-like object
446 at TOS. This is used by the extended print statement.
447
448
449.. opcode:: PRINT_NEWLINE ()
450
451 Prints a new line on ``sys.stdout``. This is generated as the last operation of
452 a :keyword:`print` statement, unless the statement ends with a comma.
453
454
455.. opcode:: PRINT_NEWLINE_TO ()
456
457 Like ``PRINT_NEWLINE``, but prints the new line on the file-like object on the
458 TOS. This is used by the extended print statement.
459
460
461.. opcode:: BREAK_LOOP ()
462
463 Terminates a loop due to a :keyword:`break` statement.
464
465
466.. opcode:: CONTINUE_LOOP (target)
467
468 Continues a loop due to a :keyword:`continue` statement. *target* is the
469 address to jump to (which should be a ``FOR_ITER`` instruction).
470
471
472.. opcode:: LIST_APPEND ()
473
474 Calls ``list.append(TOS1, TOS)``. Used to implement list comprehensions.
475
476
477.. opcode:: LOAD_LOCALS ()
478
479 Pushes a reference to the locals of the current scope on the stack. This is used
480 in the code for a class definition: After the class body is evaluated, the
481 locals are passed to the class definition.
482
483
484.. opcode:: RETURN_VALUE ()
485
486 Returns with TOS to the caller of the function.
487
488
489.. opcode:: YIELD_VALUE ()
490
Georg Brandlcf3fb252007-10-21 10:52:38 +0000491 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000492
493
494.. opcode:: IMPORT_STAR ()
495
496 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
497 local namespace. The module is popped after loading all names. This opcode
498 implements ``from module import *``.
499
500
501.. opcode:: EXEC_STMT ()
502
503 Implements ``exec TOS2,TOS1,TOS``. The compiler fills missing optional
504 parameters with ``None``.
505
506
507.. opcode:: POP_BLOCK ()
508
509 Removes one block from the block stack. Per frame, there is a stack of blocks,
510 denoting nested loops, try statements, and such.
511
512
513.. opcode:: END_FINALLY ()
514
515 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
516 exception has to be re-raised, or whether the function returns, and continues
517 with the outer-next block.
518
519
520.. opcode:: BUILD_CLASS ()
521
522 Creates a new class object. TOS is the methods dictionary, TOS1 the tuple of
523 the names of the base classes, and TOS2 the class name.
524
Georg Brandl4debd552007-08-23 17:54:11 +0000525
526.. opcode:: WITH_CLEANUP ()
527
Nick Coghlan7af53be2008-03-07 14:13:28 +0000528 Cleans up the stack when a :keyword:`with` statement block exits. On top of
529 the stack are 1--3 values indicating how/why the finally clause was entered:
Georg Brandl4debd552007-08-23 17:54:11 +0000530
Nick Coghlan7af53be2008-03-07 14:13:28 +0000531 * TOP = ``None``
532 * (TOP, SECOND) = (``WHY_{RETURN,CONTINUE}``), retval
533 * TOP = ``WHY_*``; no retval below it
534 * (TOP, SECOND, THIRD) = exc_info()
Georg Brandl4debd552007-08-23 17:54:11 +0000535
Nick Coghlan7af53be2008-03-07 14:13:28 +0000536 Under them is EXIT, the context manager's :meth:`__exit__` bound method.
Georg Brandl4debd552007-08-23 17:54:11 +0000537
Nick Coghlan7af53be2008-03-07 14:13:28 +0000538 In the last case, ``EXIT(TOP, SECOND, THIRD)`` is called, otherwise
539 ``EXIT(None, None, None)``.
540
541 EXIT is removed from the stack, leaving the values above it in the same
542 order. In addition, if the stack represents an exception, *and* the function
543 call returns a 'true' value, this information is "zapped", to prevent
544 ``END_FINALLY`` from re-raising the exception. (But non-local gotos should
545 still be resumed.)
Georg Brandl4debd552007-08-23 17:54:11 +0000546
Georg Brandlff27e0c2007-10-20 13:22:53 +0000547 .. XXX explain the WHY stuff!
548
Georg Brandl4debd552007-08-23 17:54:11 +0000549
Georg Brandl8ec7f652007-08-15 14:28:01 +0000550All of the following opcodes expect arguments. An argument is two bytes, with
551the more significant byte last.
552
Georg Brandl8ec7f652007-08-15 14:28:01 +0000553.. opcode:: STORE_NAME (namei)
554
555 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Georg Brandl59724932008-02-23 15:43:48 +0000556 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl8ec7f652007-08-15 14:28:01 +0000557 or ``STORE_GLOBAL`` if possible.
558
559
560.. opcode:: DELETE_NAME (namei)
561
562 Implements ``del name``, where *namei* is the index into :attr:`co_names`
563 attribute of the code object.
564
565
566.. opcode:: UNPACK_SEQUENCE (count)
567
568 Unpacks TOS into *count* individual values, which are put onto the stack
569 right-to-left.
570
Georg Brandl8ec7f652007-08-15 14:28:01 +0000571
572.. opcode:: DUP_TOPX (count)
573
574 Duplicate *count* items, keeping them in the same order. Due to implementation
575 limits, *count* should be between 1 and 5 inclusive.
576
577
578.. opcode:: STORE_ATTR (namei)
579
580 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
581 :attr:`co_names`.
582
583
584.. opcode:: DELETE_ATTR (namei)
585
586 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
587
588
589.. opcode:: STORE_GLOBAL (namei)
590
591 Works as ``STORE_NAME``, but stores the name as a global.
592
593
594.. opcode:: DELETE_GLOBAL (namei)
595
596 Works as ``DELETE_NAME``, but deletes a global name.
597
Georg Brandl8ec7f652007-08-15 14:28:01 +0000598
599.. opcode:: LOAD_CONST (consti)
600
601 Pushes ``co_consts[consti]`` onto the stack.
602
603
604.. opcode:: LOAD_NAME (namei)
605
606 Pushes the value associated with ``co_names[namei]`` onto the stack.
607
608
609.. opcode:: BUILD_TUPLE (count)
610
611 Creates a tuple consuming *count* items from the stack, and pushes the resulting
612 tuple onto the stack.
613
614
615.. opcode:: BUILD_LIST (count)
616
617 Works as ``BUILD_TUPLE``, but creates a list.
618
619
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000620.. opcode:: BUILD_MAP (count)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000621
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000622 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
623 to hold *count* entries.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000624
625
626.. opcode:: LOAD_ATTR (namei)
627
628 Replaces TOS with ``getattr(TOS, co_names[namei])``.
629
630
631.. opcode:: COMPARE_OP (opname)
632
633 Performs a Boolean operation. The operation name can be found in
634 ``cmp_op[opname]``.
635
636
637.. opcode:: IMPORT_NAME (namei)
638
Georg Brandl2fb8a532008-04-19 16:59:16 +0000639 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
640 the *fromlist* and *level* arguments of :func:`__import__`. The module
641 object is pushed onto the stack. The current namespace is not affected:
642 for a proper import statement, a subsequent ``STORE_FAST`` instruction
643 modifies the namespace.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000644
645
646.. opcode:: IMPORT_FROM (namei)
647
648 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
649 resulting object is pushed onto the stack, to be subsequently stored by a
650 ``STORE_FAST`` instruction.
651
652
653.. opcode:: JUMP_FORWARD (delta)
654
Georg Brandl63fa1682007-10-21 10:24:20 +0000655 Increments bytecode counter by *delta*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000656
657
658.. opcode:: JUMP_IF_TRUE (delta)
659
Georg Brandl63fa1682007-10-21 10:24:20 +0000660 If TOS is true, increment the bytecode counter by *delta*. TOS is left on the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000661 stack.
662
663
664.. opcode:: JUMP_IF_FALSE (delta)
665
Georg Brandl63fa1682007-10-21 10:24:20 +0000666 If TOS is false, increment the bytecode counter by *delta*. TOS is not
Georg Brandl8ec7f652007-08-15 14:28:01 +0000667 changed.
668
669
670.. opcode:: JUMP_ABSOLUTE (target)
671
Georg Brandl63fa1682007-10-21 10:24:20 +0000672 Set bytecode counter to *target*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000673
674
675.. opcode:: FOR_ITER (delta)
676
Georg Brandl0dfdf002009-10-27 14:36:50 +0000677 ``TOS`` is an :term:`iterator`. Call its :meth:`!next` method. If this
Georg Brandle7a09902007-10-21 12:10:28 +0000678 yields a new value, push it on the stack (leaving the iterator below it). If
679 the iterator indicates it is exhausted ``TOS`` is popped, and the bytecode
680 counter is incremented by *delta*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000681
Georg Brandl8ec7f652007-08-15 14:28:01 +0000682
683.. opcode:: LOAD_GLOBAL (namei)
684
685 Loads the global named ``co_names[namei]`` onto the stack.
686
Georg Brandl8ec7f652007-08-15 14:28:01 +0000687
688.. opcode:: SETUP_LOOP (delta)
689
690 Pushes a block for a loop onto the block stack. The block spans from the
691 current instruction with a size of *delta* bytes.
692
693
694.. opcode:: SETUP_EXCEPT (delta)
695
696 Pushes a try block from a try-except clause onto the block stack. *delta* points
697 to the first except block.
698
699
700.. opcode:: SETUP_FINALLY (delta)
701
702 Pushes a try block from a try-except clause onto the block stack. *delta* points
703 to the finally block.
704
Raymond Hettingerbed4dd42008-01-11 23:25:18 +0000705.. opcode:: STORE_MAP ()
706
707 Store a key and value pair in a dictionary. Pops the key and value while leaving
708 the dictionary on the stack.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000709
710.. opcode:: LOAD_FAST (var_num)
711
712 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
713
714
715.. opcode:: STORE_FAST (var_num)
716
717 Stores TOS into the local ``co_varnames[var_num]``.
718
719
720.. opcode:: DELETE_FAST (var_num)
721
722 Deletes local ``co_varnames[var_num]``.
723
724
725.. opcode:: LOAD_CLOSURE (i)
726
727 Pushes a reference to the cell contained in slot *i* of the cell and free
728 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
729 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
730 len(co_cellvars)]``.
731
732
733.. opcode:: LOAD_DEREF (i)
734
735 Loads the cell contained in slot *i* of the cell and free variable storage.
736 Pushes a reference to the object the cell contains on the stack.
737
738
739.. opcode:: STORE_DEREF (i)
740
741 Stores TOS into the cell contained in slot *i* of the cell and free variable
742 storage.
743
744
745.. opcode:: SET_LINENO (lineno)
746
747 This opcode is obsolete.
748
749
750.. opcode:: RAISE_VARARGS (argc)
751
752 Raises an exception. *argc* indicates the number of parameters to the raise
753 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
754 the parameter as TOS1, and the exception as TOS.
755
756
757.. opcode:: CALL_FUNCTION (argc)
758
759 Calls a function. The low byte of *argc* indicates the number of positional
760 parameters, the high byte the number of keyword parameters. On the stack, the
761 opcode finds the keyword parameters first. For each keyword argument, the value
762 is on top of the key. Below the keyword parameters, the positional parameters
763 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl734373c2009-01-03 21:55:17 +0000764 the function object to call is on the stack. Pops all function arguments, and
Georg Brandl4aef7032008-11-07 08:56:27 +0000765 the function itself off the stack, and pushes the return value.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000766
767
768.. opcode:: MAKE_FUNCTION (argc)
769
770 Pushes a new function object on the stack. TOS is the code associated with the
771 function. The function object is defined to have *argc* default parameters,
772 which are found below TOS.
773
774
775.. opcode:: MAKE_CLOSURE (argc)
776
777 Creates a new function object, sets its *func_closure* slot, and pushes it on
Georg Brandl4debd552007-08-23 17:54:11 +0000778 the stack. TOS is the code associated with the function, TOS1 the tuple
779 containing cells for the closure's free variables. The function also has
780 *argc* default parameters, which are found below the cells.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000781
782
783.. opcode:: BUILD_SLICE (argc)
784
785 .. index:: builtin: slice
786
787 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
788 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Georg Brandlb19be572007-12-29 10:57:00 +0000789 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000790
791
792.. opcode:: EXTENDED_ARG (ext)
793
794 Prefixes any opcode which has an argument too big to fit into the default two
795 bytes. *ext* holds two additional bytes which, taken together with the
796 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
797 most-significant bytes.
798
799
800.. opcode:: CALL_FUNCTION_VAR (argc)
801
802 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
803 on the stack contains the variable argument list, followed by keyword and
804 positional arguments.
805
806
807.. opcode:: CALL_FUNCTION_KW (argc)
808
809 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
810 on the stack contains the keyword arguments dictionary, followed by explicit
811 keyword and positional arguments.
812
813
814.. opcode:: CALL_FUNCTION_VAR_KW (argc)
815
816 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
817 element on the stack contains the keyword arguments dictionary, followed by the
818 variable-arguments tuple, followed by explicit keyword and positional arguments.
819
820
821.. opcode:: HAVE_ARGUMENT ()
822
823 This is not really an opcode. It identifies the dividing line between opcodes
824 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
825 HAVE_ARGUMENT``.
826