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