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