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