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