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