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