| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 1 | :mod:`dis` --- Disassembler for Python bytecode | 
 | 2 | =============================================== | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 |  | 
 | 4 | .. module:: dis | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 5 |    :synopsis: Disassembler for Python bytecode. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 6 |  | 
 | 7 |  | 
| Brett Cannon | 8315fd1 | 2010-07-02 22:03:00 +0000 | [diff] [blame] | 8 | The :mod:`dis` module supports the analysis of CPython :term:`bytecode` by | 
 | 9 | disassembling it. The CPython bytecode which this module takes as an | 
| Georg Brandl | 71515ca | 2009-05-17 12:29:12 +0000 | [diff] [blame] | 10 | input is defined in the file :file:`Include/opcode.h` and used by the compiler | 
 | 11 | and the interpreter. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 |  | 
| Georg Brandl | 19b7a87 | 2010-07-03 10:21:50 +0000 | [diff] [blame] | 13 | .. 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 Cannon | 8315fd1 | 2010-07-02 22:03:00 +0000 | [diff] [blame] | 20 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | Example: Given the function :func:`myfunc`:: | 
 | 22 |  | 
 | 23 |    def myfunc(alist): | 
 | 24 |        return len(alist) | 
 | 25 |  | 
 | 26 | the 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 |  | 
 | 36 | The :mod:`dis` module defines the following functions and constants: | 
 | 37 |  | 
 | 38 |  | 
| Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame^] | 39 | .. function:: code_info(x=None) | 
 | 40 |  | 
 | 41 |    Return a formatted multi-line string with detailed code object | 
 | 42 |    information for the supplied function, method, source code string | 
 | 43 |    or code object. | 
 | 44 |  | 
 | 45 |    Note that the exact contents of code info strings are highly | 
 | 46 |    implementation dependent and they may change arbitrarily across | 
 | 47 |    Python VMs or Python releases. | 
 | 48 |  | 
 | 49 |    .. versionadded:: 3.2 | 
 | 50 |  | 
| Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 51 | .. function:: dis(x=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 53 |    Disassemble the *x* object.  *x* can denote either a module, a | 
| Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 54 |    class, a method, a function, a code object, a string of source code or a | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 55 |    byte sequence of raw bytecode.  For a module, it disassembles all | 
| Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 56 |    functions.  For a class, it disassembles all methods.  For a code object | 
 | 57 |    or sequence of raw bytecode, it prints one line per bytecode instruction. | 
 | 58 |    Strings are first compiled to code objects with the :func:`compile` | 
 | 59 |    built-in function before being disassembled.  If no object is provided, | 
 | 60 |    this function disassembles the last traceback. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 |  | 
 | 62 |  | 
| Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 63 | .. function:: distb(tb=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 65 |    Disassemble the top-of-stack function of a traceback, using the last | 
 | 66 |    traceback if none was passed.  The instruction causing the exception is | 
 | 67 |    indicated. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 |  | 
 | 69 |  | 
| Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 70 | .. function:: disassemble(code, lasti=-1) | 
 | 71 |               disco(code, lasti=-1) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 73 |    Disassemble a code object, indicating the last instruction if *lasti* was | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 |    provided.  The output is divided in the following columns: | 
 | 75 |  | 
 | 76 |    #. the line number, for the first instruction of each line | 
 | 77 |    #. the current instruction, indicated as ``-->``, | 
 | 78 |    #. a labelled instruction, indicated with ``>>``, | 
 | 79 |    #. the address of the instruction, | 
 | 80 |    #. the operation code name, | 
 | 81 |    #. operation parameters, and | 
 | 82 |    #. interpretation of the parameters in parentheses. | 
 | 83 |  | 
 | 84 |    The parameter interpretation recognizes local and global variable names, | 
 | 85 |    constant values, branch targets, and compare operators. | 
 | 86 |  | 
 | 87 |  | 
| Benjamin Peterson | 75edad0 | 2009-01-01 15:05:06 +0000 | [diff] [blame] | 88 | .. function:: findlinestarts(code) | 
 | 89 |  | 
 | 90 |    This generator function uses the ``co_firstlineno`` and ``co_lnotab`` | 
 | 91 |    attributes of the code object *code* to find the offsets which are starts of | 
 | 92 |    lines in the source code.  They are generated as ``(offset, lineno)`` pairs. | 
 | 93 |  | 
 | 94 |  | 
 | 95 | .. function:: findlabels(code) | 
 | 96 |  | 
 | 97 |    Detect all offsets in the code object *code* which are jump targets, and | 
 | 98 |    return a list of these offsets. | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 99 |  | 
 | 100 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 101 | .. data:: opname | 
 | 102 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 103 |    Sequence of operation names, indexable using the bytecode. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 |  | 
 | 105 |  | 
 | 106 | .. data:: opmap | 
 | 107 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 108 |    Dictionary mapping bytecodes to operation names. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 |  | 
 | 110 |  | 
 | 111 | .. data:: cmp_op | 
 | 112 |  | 
 | 113 |    Sequence of all compare operation names. | 
 | 114 |  | 
 | 115 |  | 
 | 116 | .. data:: hasconst | 
 | 117 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 118 |    Sequence of bytecodes that have a constant parameter. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 |  | 
 | 120 |  | 
 | 121 | .. data:: hasfree | 
 | 122 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 123 |    Sequence of bytecodes that access a free variable. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 |  | 
 | 125 |  | 
 | 126 | .. data:: hasname | 
 | 127 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 128 |    Sequence of bytecodes that access an attribute by name. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 |  | 
 | 130 |  | 
 | 131 | .. data:: hasjrel | 
 | 132 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 133 |    Sequence of bytecodes that have a relative jump target. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 134 |  | 
 | 135 |  | 
 | 136 | .. data:: hasjabs | 
 | 137 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 138 |    Sequence of bytecodes that have an absolute jump target. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 139 |  | 
 | 140 |  | 
 | 141 | .. data:: haslocal | 
 | 142 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 143 |    Sequence of bytecodes that access a local variable. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 144 |  | 
 | 145 |  | 
 | 146 | .. data:: hascompare | 
 | 147 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 148 |    Sequence of bytecodes of Boolean operations. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 149 |  | 
 | 150 |  | 
 | 151 | .. _bytecodes: | 
 | 152 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 153 | Python Bytecode Instructions | 
 | 154 | ---------------------------- | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 156 | The Python compiler currently generates the following bytecode instructions. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 |  | 
 | 158 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 159 | **General instructions** | 
 | 160 |  | 
 | 161 | .. opcode:: STOP_CODE | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 |  | 
 | 163 |    Indicates end-of-code to the compiler, not used by the interpreter. | 
 | 164 |  | 
 | 165 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 166 | .. opcode:: NOP | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 167 |  | 
 | 168 |    Do nothing code.  Used as a placeholder by the bytecode optimizer. | 
 | 169 |  | 
 | 170 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 171 | .. opcode:: POP_TOP | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 172 |  | 
 | 173 |    Removes the top-of-stack (TOS) item. | 
 | 174 |  | 
 | 175 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 176 | .. opcode:: ROT_TWO | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 177 |  | 
 | 178 |    Swaps the two top-most stack items. | 
 | 179 |  | 
 | 180 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 181 | .. opcode:: ROT_THREE | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 |  | 
 | 183 |    Lifts second and third stack item one position up, moves top down to position | 
 | 184 |    three. | 
 | 185 |  | 
 | 186 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 187 | .. opcode:: ROT_FOUR | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 188 |  | 
 | 189 |    Lifts second, third and forth stack item one position up, moves top down to | 
 | 190 |    position four. | 
 | 191 |  | 
 | 192 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 193 | .. opcode:: DUP_TOP | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 194 |  | 
 | 195 |    Duplicates the reference on top of the stack. | 
 | 196 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 197 |  | 
 | 198 | **Unary operations** | 
 | 199 |  | 
 | 200 | Unary operations take the top of the stack, apply the operation, and push the | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 201 | result back on the stack. | 
 | 202 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 203 | .. opcode:: UNARY_POSITIVE | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 204 |  | 
 | 205 |    Implements ``TOS = +TOS``. | 
 | 206 |  | 
 | 207 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 208 | .. opcode:: UNARY_NEGATIVE | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 209 |  | 
 | 210 |    Implements ``TOS = -TOS``. | 
 | 211 |  | 
 | 212 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 213 | .. opcode:: UNARY_NOT | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 214 |  | 
 | 215 |    Implements ``TOS = not TOS``. | 
 | 216 |  | 
 | 217 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 218 | .. opcode:: UNARY_INVERT | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 219 |  | 
 | 220 |    Implements ``TOS = ~TOS``. | 
 | 221 |  | 
 | 222 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 223 | .. opcode:: GET_ITER | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 224 |  | 
 | 225 |    Implements ``TOS = iter(TOS)``. | 
 | 226 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 227 |  | 
 | 228 | **Binary operations** | 
 | 229 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 230 | Binary operations remove the top of the stack (TOS) and the second top-most | 
 | 231 | stack item (TOS1) from the stack.  They perform the operation, and put the | 
 | 232 | result back on the stack. | 
 | 233 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 234 | .. opcode:: BINARY_POWER | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 235 |  | 
 | 236 |    Implements ``TOS = TOS1 ** TOS``. | 
 | 237 |  | 
 | 238 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 239 | .. opcode:: BINARY_MULTIPLY | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 240 |  | 
 | 241 |    Implements ``TOS = TOS1 * TOS``. | 
 | 242 |  | 
 | 243 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 244 | .. opcode:: BINARY_FLOOR_DIVIDE | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 245 |  | 
 | 246 |    Implements ``TOS = TOS1 // TOS``. | 
 | 247 |  | 
 | 248 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 249 | .. opcode:: BINARY_TRUE_DIVIDE | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 250 |  | 
| Ezio Melotti | 7de0a6e | 2010-01-05 08:37:27 +0000 | [diff] [blame] | 251 |    Implements ``TOS = TOS1 / TOS``. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 252 |  | 
 | 253 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 254 | .. opcode:: BINARY_MODULO | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 255 |  | 
 | 256 |    Implements ``TOS = TOS1 % TOS``. | 
 | 257 |  | 
 | 258 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 259 | .. opcode:: BINARY_ADD | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 260 |  | 
 | 261 |    Implements ``TOS = TOS1 + TOS``. | 
 | 262 |  | 
 | 263 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 264 | .. opcode:: BINARY_SUBTRACT | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 265 |  | 
 | 266 |    Implements ``TOS = TOS1 - TOS``. | 
 | 267 |  | 
 | 268 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 269 | .. opcode:: BINARY_SUBSCR | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 270 |  | 
 | 271 |    Implements ``TOS = TOS1[TOS]``. | 
 | 272 |  | 
 | 273 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 274 | .. opcode:: BINARY_LSHIFT | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 275 |  | 
 | 276 |    Implements ``TOS = TOS1 << TOS``. | 
 | 277 |  | 
 | 278 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 279 | .. opcode:: BINARY_RSHIFT | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 280 |  | 
 | 281 |    Implements ``TOS = TOS1 >> TOS``. | 
 | 282 |  | 
 | 283 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 284 | .. opcode:: BINARY_AND | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 285 |  | 
 | 286 |    Implements ``TOS = TOS1 & TOS``. | 
 | 287 |  | 
 | 288 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 289 | .. opcode:: BINARY_XOR | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 290 |  | 
 | 291 |    Implements ``TOS = TOS1 ^ TOS``. | 
 | 292 |  | 
 | 293 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 294 | .. opcode:: BINARY_OR | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 295 |  | 
 | 296 |    Implements ``TOS = TOS1 | TOS``. | 
 | 297 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 298 |  | 
 | 299 | **In-place operations** | 
 | 300 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 301 | In-place operations are like binary operations, in that they remove TOS and | 
 | 302 | TOS1, and push the result back on the stack, but the operation is done in-place | 
 | 303 | when TOS1 supports it, and the resulting TOS may be (but does not have to be) | 
 | 304 | the original TOS1. | 
 | 305 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 306 | .. opcode:: INPLACE_POWER | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 307 |  | 
 | 308 |    Implements in-place ``TOS = TOS1 ** TOS``. | 
 | 309 |  | 
 | 310 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 311 | .. opcode:: INPLACE_MULTIPLY | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 312 |  | 
 | 313 |    Implements in-place ``TOS = TOS1 * TOS``. | 
 | 314 |  | 
 | 315 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 316 | .. opcode:: INPLACE_FLOOR_DIVIDE | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 317 |  | 
 | 318 |    Implements in-place ``TOS = TOS1 // TOS``. | 
 | 319 |  | 
 | 320 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 321 | .. opcode:: INPLACE_TRUE_DIVIDE | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 322 |  | 
| Ezio Melotti | 7de0a6e | 2010-01-05 08:37:27 +0000 | [diff] [blame] | 323 |    Implements in-place ``TOS = TOS1 / TOS``. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 324 |  | 
 | 325 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 326 | .. opcode:: INPLACE_MODULO | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 327 |  | 
 | 328 |    Implements in-place ``TOS = TOS1 % TOS``. | 
 | 329 |  | 
 | 330 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 331 | .. opcode:: INPLACE_ADD | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 332 |  | 
 | 333 |    Implements in-place ``TOS = TOS1 + TOS``. | 
 | 334 |  | 
 | 335 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 336 | .. opcode:: INPLACE_SUBTRACT | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 337 |  | 
 | 338 |    Implements in-place ``TOS = TOS1 - TOS``. | 
 | 339 |  | 
 | 340 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 341 | .. opcode:: INPLACE_LSHIFT | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 342 |  | 
 | 343 |    Implements in-place ``TOS = TOS1 << TOS``. | 
 | 344 |  | 
 | 345 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 346 | .. opcode:: INPLACE_RSHIFT | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 347 |  | 
 | 348 |    Implements in-place ``TOS = TOS1 >> TOS``. | 
 | 349 |  | 
 | 350 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 351 | .. opcode:: INPLACE_AND | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 352 |  | 
 | 353 |    Implements in-place ``TOS = TOS1 & TOS``. | 
 | 354 |  | 
 | 355 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 356 | .. opcode:: INPLACE_XOR | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 357 |  | 
 | 358 |    Implements in-place ``TOS = TOS1 ^ TOS``. | 
 | 359 |  | 
 | 360 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 361 | .. opcode:: INPLACE_OR | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 362 |  | 
 | 363 |    Implements in-place ``TOS = TOS1 | TOS``. | 
 | 364 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 365 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 366 | .. opcode:: STORE_SUBSCR | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 367 |  | 
 | 368 |    Implements ``TOS1[TOS] = TOS2``. | 
 | 369 |  | 
 | 370 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 371 | .. opcode:: DELETE_SUBSCR | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 372 |  | 
 | 373 |    Implements ``del TOS1[TOS]``. | 
 | 374 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 375 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 376 | **Miscellaneous opcodes** | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 377 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 378 | .. opcode:: PRINT_EXPR | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 379 |  | 
 | 380 |    Implements the expression statement for the interactive mode.  TOS is removed | 
 | 381 |    from the stack and printed.  In non-interactive mode, an expression statement is | 
 | 382 |    terminated with ``POP_STACK``. | 
 | 383 |  | 
 | 384 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 385 | .. opcode:: BREAK_LOOP | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 386 |  | 
 | 387 |    Terminates a loop due to a :keyword:`break` statement. | 
 | 388 |  | 
 | 389 |  | 
 | 390 | .. opcode:: CONTINUE_LOOP (target) | 
 | 391 |  | 
 | 392 |    Continues a loop due to a :keyword:`continue` statement.  *target* is the | 
 | 393 |    address to jump to (which should be a ``FOR_ITER`` instruction). | 
 | 394 |  | 
 | 395 |  | 
| Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 396 | .. opcode:: SET_ADD (i) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 397 |  | 
| Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 398 |    Calls ``set.add(TOS1[-i], TOS)``.  Used to implement set comprehensions. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 399 |  | 
 | 400 |  | 
| Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 401 | .. opcode:: LIST_APPEND (i) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 402 |  | 
| Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 403 |    Calls ``list.append(TOS[-i], TOS)``.  Used to implement list comprehensions. | 
 | 404 |  | 
 | 405 |  | 
 | 406 | .. opcode:: MAP_ADD (i) | 
 | 407 |  | 
 | 408 |    Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``.  Used to implement dict | 
 | 409 |    comprehensions. | 
 | 410 |  | 
| Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 411 | For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the | 
 | 412 | added value or key/value pair is popped off, the container object remains on | 
 | 413 | the stack so that it is available for further iterations of the loop. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 414 |  | 
 | 415 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 416 | .. opcode:: RETURN_VALUE | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 417 |  | 
 | 418 |    Returns with TOS to the caller of the function. | 
 | 419 |  | 
 | 420 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 421 | .. opcode:: YIELD_VALUE | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 422 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 423 |    Pops ``TOS`` and yields it from a :term:`generator`. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 424 |  | 
 | 425 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 426 | .. opcode:: IMPORT_STAR | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 427 |  | 
 | 428 |    Loads all symbols not starting with ``'_'`` directly from the module TOS to the | 
 | 429 |    local namespace. The module is popped after loading all names. This opcode | 
 | 430 |    implements ``from module import *``. | 
 | 431 |  | 
 | 432 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 433 | .. opcode:: POP_BLOCK | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 434 |  | 
 | 435 |    Removes one block from the block stack.  Per frame, there is a  stack of blocks, | 
 | 436 |    denoting nested loops, try statements, and such. | 
 | 437 |  | 
 | 438 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 439 | .. opcode:: POP_EXCEPT | 
| Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 440 |  | 
 | 441 |    Removes one block from the block stack. The popped block must be an exception | 
 | 442 |    handler block, as implicitly created when entering an except handler. | 
 | 443 |    In addition to popping extraneous values from the frame stack, the | 
 | 444 |    last three popped values are used to restore the exception state. | 
 | 445 |  | 
 | 446 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 447 | .. opcode:: END_FINALLY | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 448 |  | 
 | 449 |    Terminates a :keyword:`finally` clause.  The interpreter recalls whether the | 
 | 450 |    exception has to be re-raised, or whether the function returns, and continues | 
 | 451 |    with the outer-next block. | 
 | 452 |  | 
 | 453 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 454 | .. opcode:: LOAD_BUILD_CLASS | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 455 |  | 
| Georg Brandl | 5ac2230 | 2008-07-20 21:39:03 +0000 | [diff] [blame] | 456 |    Pushes :func:`builtins.__build_class__` onto the stack.  It is later called | 
| Benjamin Peterson | aac8fd3 | 2008-07-20 22:02:26 +0000 | [diff] [blame] | 457 |    by ``CALL_FUNCTION`` to construct a class. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 458 |  | 
| Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 459 |  | 
| Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 460 | .. opcode:: SETUP_WITH (delta) | 
 | 461 |  | 
 | 462 |    This opcode performs several operations before a with block starts.  First, | 
 | 463 |    it loads :meth:`~object.__exit__` from the context manager and pushes it onto | 
 | 464 |    the stack for later use by :opcode:`WITH_CLEANUP`.  Then, | 
 | 465 |    :meth:`~object.__enter__` is called, and a finally block pointing to *delta* | 
 | 466 |    is pushed.  Finally, the result of calling the enter method is pushed onto | 
 | 467 |    the stack.  The next opcode will either ignore it (:opcode:`POP_TOP`), or | 
 | 468 |    store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or | 
 | 469 |    :opcode:`UNPACK_SEQUENCE`). | 
 | 470 |  | 
 | 471 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 472 | .. opcode:: WITH_CLEANUP | 
| Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 473 |  | 
| Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 474 |    Cleans up the stack when a :keyword:`with` statement block exits.  TOS is | 
 | 475 |    the context manager's :meth:`__exit__` bound method. Below TOS are 1--3 | 
 | 476 |    values indicating how/why the finally clause was entered: | 
| Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 477 |  | 
| Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 478 |    * SECOND = ``None`` | 
 | 479 |    * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval | 
 | 480 |    * SECOND = ``WHY_*``; no retval below it | 
 | 481 |    * (SECOND, THIRD, FOURTH) = exc_info() | 
| Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 482 |  | 
| Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 483 |    In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise | 
 | 484 |    ``TOS(None, None, None)``.  In addition, TOS is removed from the stack. | 
| Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 485 |  | 
| Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 486 |    If the stack represents an exception, *and* the function call returns | 
 | 487 |    a 'true' value, this information is "zapped" and replaced with a single | 
 | 488 |    ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception. | 
 | 489 |    (But non-local gotos will still be resumed.) | 
| Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 490 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 491 |    .. XXX explain the WHY stuff! | 
 | 492 |  | 
| Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 493 |  | 
| Georg Brandl | 5ac2230 | 2008-07-20 21:39:03 +0000 | [diff] [blame] | 494 | .. opcode:: STORE_LOCALS | 
 | 495 |  | 
 | 496 |    Pops TOS from the stack and stores it as the current frame's ``f_locals``. | 
 | 497 |    This is used in class construction. | 
 | 498 |  | 
 | 499 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 500 | All of the following opcodes expect arguments.  An argument is two bytes, with | 
 | 501 | the more significant byte last. | 
 | 502 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 503 | .. opcode:: STORE_NAME (namei) | 
 | 504 |  | 
 | 505 |    Implements ``name = TOS``. *namei* is the index of *name* in the attribute | 
| Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 506 |    :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST`` | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 507 |    or ``STORE_GLOBAL`` if possible. | 
 | 508 |  | 
 | 509 |  | 
 | 510 | .. opcode:: DELETE_NAME (namei) | 
 | 511 |  | 
 | 512 |    Implements ``del name``, where *namei* is the index into :attr:`co_names` | 
 | 513 |    attribute of the code object. | 
 | 514 |  | 
 | 515 |  | 
 | 516 | .. opcode:: UNPACK_SEQUENCE (count) | 
 | 517 |  | 
 | 518 |    Unpacks TOS into *count* individual values, which are put onto the stack | 
 | 519 |    right-to-left. | 
 | 520 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 521 |  | 
| Georg Brandl | 5ac2230 | 2008-07-20 21:39:03 +0000 | [diff] [blame] | 522 | .. opcode:: UNPACK_EX (counts) | 
 | 523 |  | 
 | 524 |    Implements assignment with a starred target: Unpacks an iterable in TOS into | 
 | 525 |    individual values, where the total number of values can be smaller than the | 
 | 526 |    number of items in the iterable: one the new values will be a list of all | 
 | 527 |    leftover items. | 
 | 528 |  | 
 | 529 |    The low byte of *counts* is the number of values before the list value, the | 
 | 530 |    high byte of *counts* the number of values after it.  The resulting values | 
 | 531 |    are put onto the stack right-to-left. | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 532 |  | 
| Georg Brandl | 5ac2230 | 2008-07-20 21:39:03 +0000 | [diff] [blame] | 533 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 534 | .. opcode:: DUP_TOPX (count) | 
 | 535 |  | 
 | 536 |    Duplicate *count* items, keeping them in the same order. Due to implementation | 
 | 537 |    limits, *count* should be between 1 and 5 inclusive. | 
 | 538 |  | 
 | 539 |  | 
 | 540 | .. opcode:: STORE_ATTR (namei) | 
 | 541 |  | 
 | 542 |    Implements ``TOS.name = TOS1``, where *namei* is the index of name in | 
 | 543 |    :attr:`co_names`. | 
 | 544 |  | 
 | 545 |  | 
 | 546 | .. opcode:: DELETE_ATTR (namei) | 
 | 547 |  | 
 | 548 |    Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`. | 
 | 549 |  | 
 | 550 |  | 
 | 551 | .. opcode:: STORE_GLOBAL (namei) | 
 | 552 |  | 
 | 553 |    Works as ``STORE_NAME``, but stores the name as a global. | 
 | 554 |  | 
 | 555 |  | 
 | 556 | .. opcode:: DELETE_GLOBAL (namei) | 
 | 557 |  | 
 | 558 |    Works as ``DELETE_NAME``, but deletes a global name. | 
 | 559 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 560 |  | 
 | 561 | .. opcode:: LOAD_CONST (consti) | 
 | 562 |  | 
 | 563 |    Pushes ``co_consts[consti]`` onto the stack. | 
 | 564 |  | 
 | 565 |  | 
 | 566 | .. opcode:: LOAD_NAME (namei) | 
 | 567 |  | 
 | 568 |    Pushes the value associated with ``co_names[namei]`` onto the stack. | 
 | 569 |  | 
 | 570 |  | 
 | 571 | .. opcode:: BUILD_TUPLE (count) | 
 | 572 |  | 
 | 573 |    Creates a tuple consuming *count* items from the stack, and pushes the resulting | 
 | 574 |    tuple onto the stack. | 
 | 575 |  | 
 | 576 |  | 
 | 577 | .. opcode:: BUILD_LIST (count) | 
 | 578 |  | 
 | 579 |    Works as ``BUILD_TUPLE``, but creates a list. | 
 | 580 |  | 
 | 581 |  | 
 | 582 | .. opcode:: BUILD_SET (count) | 
 | 583 |  | 
 | 584 |    Works as ``BUILD_TUPLE``, but creates a set. | 
 | 585 |  | 
 | 586 |  | 
| Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 587 | .. opcode:: BUILD_MAP (count) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 588 |  | 
| Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 589 |    Pushes a new dictionary object onto the stack.  The dictionary is pre-sized | 
 | 590 |    to hold *count* entries. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 591 |  | 
 | 592 |  | 
 | 593 | .. opcode:: LOAD_ATTR (namei) | 
 | 594 |  | 
 | 595 |    Replaces TOS with ``getattr(TOS, co_names[namei])``. | 
 | 596 |  | 
 | 597 |  | 
 | 598 | .. opcode:: COMPARE_OP (opname) | 
 | 599 |  | 
 | 600 |    Performs a Boolean operation.  The operation name can be found in | 
 | 601 |    ``cmp_op[opname]``. | 
 | 602 |  | 
 | 603 |  | 
 | 604 | .. opcode:: IMPORT_NAME (namei) | 
 | 605 |  | 
| Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 606 |    Imports the module ``co_names[namei]``.  TOS and TOS1 are popped and provide | 
 | 607 |    the *fromlist* and *level* arguments of :func:`__import__`.  The module | 
 | 608 |    object is pushed onto the stack.  The current namespace is not affected: | 
 | 609 |    for a proper import statement, a subsequent ``STORE_FAST`` instruction | 
 | 610 |    modifies the namespace. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 611 |  | 
 | 612 |  | 
 | 613 | .. opcode:: IMPORT_FROM (namei) | 
 | 614 |  | 
 | 615 |    Loads the attribute ``co_names[namei]`` from the module found in TOS. The | 
 | 616 |    resulting object is pushed onto the stack, to be subsequently stored by a | 
 | 617 |    ``STORE_FAST`` instruction. | 
 | 618 |  | 
 | 619 |  | 
 | 620 | .. opcode:: JUMP_FORWARD (delta) | 
 | 621 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 622 |    Increments bytecode counter by *delta*. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 623 |  | 
 | 624 |  | 
| Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 625 | .. opcode:: POP_JUMP_IF_TRUE (target) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 626 |  | 
| Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 627 |    If TOS is true, sets the bytecode counter to *target*.  TOS is popped. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 628 |  | 
 | 629 |  | 
| Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 630 | .. opcode:: POP_JUMP_IF_FALSE (target) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 631 |  | 
| Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 632 |    If TOS is false, sets the bytecode counter to *target*.  TOS is popped. | 
 | 633 |  | 
 | 634 |  | 
 | 635 | .. opcode:: JUMP_IF_TRUE_OR_POP (target) | 
 | 636 |  | 
 | 637 |    If TOS is true, sets the bytecode counter to *target* and leaves TOS | 
 | 638 |    on the stack.  Otherwise (TOS is false), TOS is popped. | 
 | 639 |  | 
 | 640 |  | 
 | 641 | .. opcode:: JUMP_IF_FALSE_OR_POP (target) | 
 | 642 |  | 
 | 643 |    If TOS is false, sets the bytecode counter to *target* and leaves | 
 | 644 |    TOS on the stack.  Otherwise (TOS is true), TOS is popped. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 645 |  | 
 | 646 |  | 
 | 647 | .. opcode:: JUMP_ABSOLUTE (target) | 
 | 648 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 649 |    Set bytecode counter to *target*. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 650 |  | 
 | 651 |  | 
 | 652 | .. opcode:: FOR_ITER (delta) | 
 | 653 |  | 
| Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 654 |    ``TOS`` is an :term:`iterator`.  Call its :meth:`__next__` method.  If this | 
 | 655 |    yields a new value, push it on the stack (leaving the iterator below it).  If | 
 | 656 |    the iterator indicates it is exhausted ``TOS`` is popped, and the byte code | 
 | 657 |    counter is incremented by *delta*. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 658 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 659 |  | 
 | 660 | .. opcode:: LOAD_GLOBAL (namei) | 
 | 661 |  | 
 | 662 |    Loads the global named ``co_names[namei]`` onto the stack. | 
 | 663 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 664 |  | 
 | 665 | .. opcode:: SETUP_LOOP (delta) | 
 | 666 |  | 
 | 667 |    Pushes a block for a loop onto the block stack.  The block spans from the | 
 | 668 |    current instruction with a size of *delta* bytes. | 
 | 669 |  | 
 | 670 |  | 
 | 671 | .. opcode:: SETUP_EXCEPT (delta) | 
 | 672 |  | 
 | 673 |    Pushes a try block from a try-except clause onto the block stack. *delta* points | 
 | 674 |    to the first except block. | 
 | 675 |  | 
 | 676 |  | 
 | 677 | .. opcode:: SETUP_FINALLY (delta) | 
 | 678 |  | 
 | 679 |    Pushes a try block from a try-except clause onto the block stack. *delta* points | 
 | 680 |    to the finally block. | 
 | 681 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 682 | .. opcode:: STORE_MAP | 
| Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 683 |  | 
 | 684 |    Store a key and value pair in a dictionary.  Pops the key and value while leaving | 
 | 685 |    the dictionary on the stack. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 686 |  | 
 | 687 | .. opcode:: LOAD_FAST (var_num) | 
 | 688 |  | 
 | 689 |    Pushes a reference to the local ``co_varnames[var_num]`` onto the stack. | 
 | 690 |  | 
 | 691 |  | 
 | 692 | .. opcode:: STORE_FAST (var_num) | 
 | 693 |  | 
 | 694 |    Stores TOS into the local ``co_varnames[var_num]``. | 
 | 695 |  | 
 | 696 |  | 
 | 697 | .. opcode:: DELETE_FAST (var_num) | 
 | 698 |  | 
 | 699 |    Deletes local ``co_varnames[var_num]``. | 
 | 700 |  | 
 | 701 |  | 
 | 702 | .. opcode:: LOAD_CLOSURE (i) | 
 | 703 |  | 
 | 704 |    Pushes a reference to the cell contained in slot *i* of the cell and free | 
 | 705 |    variable storage.  The name of the variable is  ``co_cellvars[i]`` if *i* is | 
 | 706 |    less than the length of *co_cellvars*.  Otherwise it is  ``co_freevars[i - | 
 | 707 |    len(co_cellvars)]``. | 
 | 708 |  | 
 | 709 |  | 
 | 710 | .. opcode:: LOAD_DEREF (i) | 
 | 711 |  | 
 | 712 |    Loads the cell contained in slot *i* of the cell and free variable storage. | 
 | 713 |    Pushes a reference to the object the cell contains on the stack. | 
 | 714 |  | 
 | 715 |  | 
 | 716 | .. opcode:: STORE_DEREF (i) | 
 | 717 |  | 
 | 718 |    Stores TOS into the cell contained in slot *i* of the cell and free variable | 
 | 719 |    storage. | 
 | 720 |  | 
 | 721 |  | 
 | 722 | .. opcode:: SET_LINENO (lineno) | 
 | 723 |  | 
 | 724 |    This opcode is obsolete. | 
 | 725 |  | 
 | 726 |  | 
 | 727 | .. opcode:: RAISE_VARARGS (argc) | 
 | 728 |  | 
 | 729 |    Raises an exception. *argc* indicates the number of parameters to the raise | 
 | 730 |    statement, ranging from 0 to 3.  The handler will find the traceback as TOS2, | 
 | 731 |    the parameter as TOS1, and the exception as TOS. | 
 | 732 |  | 
 | 733 |  | 
 | 734 | .. opcode:: CALL_FUNCTION (argc) | 
 | 735 |  | 
 | 736 |    Calls a function.  The low byte of *argc* indicates the number of positional | 
 | 737 |    parameters, the high byte the number of keyword parameters. On the stack, the | 
 | 738 |    opcode finds the keyword parameters first.  For each keyword argument, the value | 
 | 739 |    is on top of the key.  Below the keyword parameters, the positional parameters | 
 | 740 |    are on the stack, with the right-most parameter on top.  Below the parameters, | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 741 |    the function object to call is on the stack.  Pops all function arguments, and | 
| Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 742 |    the function itself off the stack, and pushes the return value. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 743 |  | 
 | 744 |  | 
 | 745 | .. opcode:: MAKE_FUNCTION (argc) | 
 | 746 |  | 
 | 747 |    Pushes a new function object on the stack.  TOS is the code associated with the | 
 | 748 |    function.  The function object is defined to have *argc* default parameters, | 
 | 749 |    which are found below TOS. | 
 | 750 |  | 
 | 751 |  | 
 | 752 | .. opcode:: MAKE_CLOSURE (argc) | 
 | 753 |  | 
| Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 754 |    Creates a new function object, sets its *__closure__* slot, and pushes it on | 
 | 755 |    the stack.  TOS is the code associated with the function, TOS1 the tuple | 
 | 756 |    containing cells for the closure's free variables.  The function also has | 
 | 757 |    *argc* default parameters, which are found below the cells. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 758 |  | 
 | 759 |  | 
 | 760 | .. opcode:: BUILD_SLICE (argc) | 
 | 761 |  | 
 | 762 |    .. index:: builtin: slice | 
 | 763 |  | 
 | 764 |    Pushes a slice object on the stack.  *argc* must be 2 or 3.  If it is 2, | 
 | 765 |    ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is | 
| Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 766 |    pushed. See the :func:`slice` built-in function for more information. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 767 |  | 
 | 768 |  | 
 | 769 | .. opcode:: EXTENDED_ARG (ext) | 
 | 770 |  | 
 | 771 |    Prefixes any opcode which has an argument too big to fit into the default two | 
 | 772 |    bytes.  *ext* holds two additional bytes which, taken together with the | 
 | 773 |    subsequent opcode's argument, comprise a four-byte argument, *ext* being the two | 
 | 774 |    most-significant bytes. | 
 | 775 |  | 
 | 776 |  | 
 | 777 | .. opcode:: CALL_FUNCTION_VAR (argc) | 
 | 778 |  | 
 | 779 |    Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element | 
 | 780 |    on the stack contains the variable argument list, followed by keyword and | 
 | 781 |    positional arguments. | 
 | 782 |  | 
 | 783 |  | 
 | 784 | .. opcode:: CALL_FUNCTION_KW (argc) | 
 | 785 |  | 
 | 786 |    Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element | 
 | 787 |    on the stack contains the keyword arguments dictionary,  followed by explicit | 
 | 788 |    keyword and positional arguments. | 
 | 789 |  | 
 | 790 |  | 
 | 791 | .. opcode:: CALL_FUNCTION_VAR_KW (argc) | 
 | 792 |  | 
 | 793 |    Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``.  The top | 
 | 794 |    element on the stack contains the keyword arguments dictionary, followed by the | 
 | 795 |    variable-arguments tuple, followed by explicit keyword and positional arguments. | 
 | 796 |  | 
 | 797 |  | 
| Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 798 | .. opcode:: HAVE_ARGUMENT | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 799 |  | 
 | 800 |    This is not really an opcode.  It identifies the dividing line between opcodes | 
 | 801 |    which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>= | 
 | 802 |    HAVE_ARGUMENT``. | 
 | 803 |  |