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