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 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 400 | .. opcode:: POP_EXCEPT () |
| 401 | |
| 402 | Removes one block from the block stack. The popped block must be an exception |
| 403 | handler block, as implicitly created when entering an except handler. |
| 404 | In addition to popping extraneous values from the frame stack, the |
| 405 | last three popped values are used to restore the exception state. |
| 406 | |
| 407 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 408 | .. opcode:: END_FINALLY () |
| 409 | |
| 410 | Terminates a :keyword:`finally` clause. The interpreter recalls whether the |
| 411 | exception has to be re-raised, or whether the function returns, and continues |
| 412 | with the outer-next block. |
| 413 | |
| 414 | |
Benjamin Peterson | 69164c7 | 2008-07-03 14:45:20 +0000 | [diff] [blame] | 415 | .. opcode:: LOAD_BUILD_CLASS () |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 416 | |
Georg Brandl | 5ac2230 | 2008-07-20 21:39:03 +0000 | [diff] [blame] | 417 | Pushes :func:`builtins.__build_class__` onto the stack. It is later called |
Benjamin Peterson | aac8fd3 | 2008-07-20 22:02:26 +0000 | [diff] [blame] | 418 | by ``CALL_FUNCTION`` to construct a class. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 420 | |
| 421 | .. opcode:: WITH_CLEANUP () |
| 422 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 423 | Cleans up the stack when a :keyword:`with` statement block exits. TOS is |
| 424 | the context manager's :meth:`__exit__` bound method. Below TOS are 1--3 |
| 425 | values indicating how/why the finally clause was entered: |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 426 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 427 | * SECOND = ``None`` |
| 428 | * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval |
| 429 | * SECOND = ``WHY_*``; no retval below it |
| 430 | * (SECOND, THIRD, FOURTH) = exc_info() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 431 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 432 | In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise |
| 433 | ``TOS(None, None, None)``. In addition, TOS is removed from the stack. |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 434 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 435 | If the stack represents an exception, *and* the function call returns |
| 436 | a 'true' value, this information is "zapped" and replaced with a single |
| 437 | ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception. |
| 438 | (But non-local gotos will still be resumed.) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 439 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 440 | .. XXX explain the WHY stuff! |
| 441 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 442 | |
Georg Brandl | 5ac2230 | 2008-07-20 21:39:03 +0000 | [diff] [blame] | 443 | .. opcode:: STORE_LOCALS |
| 444 | |
| 445 | Pops TOS from the stack and stores it as the current frame's ``f_locals``. |
| 446 | This is used in class construction. |
| 447 | |
| 448 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 449 | All of the following opcodes expect arguments. An argument is two bytes, with |
| 450 | the more significant byte last. |
| 451 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 452 | .. opcode:: STORE_NAME (namei) |
| 453 | |
| 454 | Implements ``name = TOS``. *namei* is the index of *name* in the attribute |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 455 | :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST`` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 456 | or ``STORE_GLOBAL`` if possible. |
| 457 | |
| 458 | |
| 459 | .. opcode:: DELETE_NAME (namei) |
| 460 | |
| 461 | Implements ``del name``, where *namei* is the index into :attr:`co_names` |
| 462 | attribute of the code object. |
| 463 | |
| 464 | |
| 465 | .. opcode:: UNPACK_SEQUENCE (count) |
| 466 | |
| 467 | Unpacks TOS into *count* individual values, which are put onto the stack |
| 468 | right-to-left. |
| 469 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 470 | |
Georg Brandl | 5ac2230 | 2008-07-20 21:39:03 +0000 | [diff] [blame] | 471 | .. opcode:: UNPACK_EX (counts) |
| 472 | |
| 473 | Implements assignment with a starred target: Unpacks an iterable in TOS into |
| 474 | individual values, where the total number of values can be smaller than the |
| 475 | number of items in the iterable: one the new values will be a list of all |
| 476 | leftover items. |
| 477 | |
| 478 | The low byte of *counts* is the number of values before the list value, the |
| 479 | high byte of *counts* the number of values after it. The resulting values |
| 480 | are put onto the stack right-to-left. |
| 481 | |
| 482 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 483 | .. opcode:: DUP_TOPX (count) |
| 484 | |
| 485 | Duplicate *count* items, keeping them in the same order. Due to implementation |
| 486 | limits, *count* should be between 1 and 5 inclusive. |
| 487 | |
| 488 | |
| 489 | .. opcode:: STORE_ATTR (namei) |
| 490 | |
| 491 | Implements ``TOS.name = TOS1``, where *namei* is the index of name in |
| 492 | :attr:`co_names`. |
| 493 | |
| 494 | |
| 495 | .. opcode:: DELETE_ATTR (namei) |
| 496 | |
| 497 | Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`. |
| 498 | |
| 499 | |
| 500 | .. opcode:: STORE_GLOBAL (namei) |
| 501 | |
| 502 | Works as ``STORE_NAME``, but stores the name as a global. |
| 503 | |
| 504 | |
| 505 | .. opcode:: DELETE_GLOBAL (namei) |
| 506 | |
| 507 | Works as ``DELETE_NAME``, but deletes a global name. |
| 508 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 509 | |
| 510 | .. opcode:: LOAD_CONST (consti) |
| 511 | |
| 512 | Pushes ``co_consts[consti]`` onto the stack. |
| 513 | |
| 514 | |
| 515 | .. opcode:: LOAD_NAME (namei) |
| 516 | |
| 517 | Pushes the value associated with ``co_names[namei]`` onto the stack. |
| 518 | |
| 519 | |
| 520 | .. opcode:: BUILD_TUPLE (count) |
| 521 | |
| 522 | Creates a tuple consuming *count* items from the stack, and pushes the resulting |
| 523 | tuple onto the stack. |
| 524 | |
| 525 | |
| 526 | .. opcode:: BUILD_LIST (count) |
| 527 | |
| 528 | Works as ``BUILD_TUPLE``, but creates a list. |
| 529 | |
| 530 | |
| 531 | .. opcode:: BUILD_SET (count) |
| 532 | |
| 533 | Works as ``BUILD_TUPLE``, but creates a set. |
| 534 | |
| 535 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 536 | .. opcode:: BUILD_MAP (count) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 537 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 538 | Pushes a new dictionary object onto the stack. The dictionary is pre-sized |
| 539 | to hold *count* entries. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 540 | |
| 541 | |
| 542 | .. opcode:: LOAD_ATTR (namei) |
| 543 | |
| 544 | Replaces TOS with ``getattr(TOS, co_names[namei])``. |
| 545 | |
| 546 | |
| 547 | .. opcode:: COMPARE_OP (opname) |
| 548 | |
| 549 | Performs a Boolean operation. The operation name can be found in |
| 550 | ``cmp_op[opname]``. |
| 551 | |
| 552 | |
| 553 | .. opcode:: IMPORT_NAME (namei) |
| 554 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 555 | Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide |
| 556 | the *fromlist* and *level* arguments of :func:`__import__`. The module |
| 557 | object is pushed onto the stack. The current namespace is not affected: |
| 558 | for a proper import statement, a subsequent ``STORE_FAST`` instruction |
| 559 | modifies the namespace. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 560 | |
| 561 | |
| 562 | .. opcode:: IMPORT_FROM (namei) |
| 563 | |
| 564 | Loads the attribute ``co_names[namei]`` from the module found in TOS. The |
| 565 | resulting object is pushed onto the stack, to be subsequently stored by a |
| 566 | ``STORE_FAST`` instruction. |
| 567 | |
| 568 | |
| 569 | .. opcode:: JUMP_FORWARD (delta) |
| 570 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 571 | Increments bytecode counter by *delta*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 572 | |
| 573 | |
| 574 | .. opcode:: JUMP_IF_TRUE (delta) |
| 575 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 576 | 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] | 577 | stack. |
| 578 | |
| 579 | |
| 580 | .. opcode:: JUMP_IF_FALSE (delta) |
| 581 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 582 | 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] | 583 | changed. |
| 584 | |
| 585 | |
| 586 | .. opcode:: JUMP_ABSOLUTE (target) |
| 587 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 588 | Set bytecode counter to *target*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 589 | |
| 590 | |
| 591 | .. opcode:: FOR_ITER (delta) |
| 592 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 593 | ``TOS`` is an :term:`iterator`. Call its :meth:`__next__` method. If this |
| 594 | yields a new value, push it on the stack (leaving the iterator below it). If |
| 595 | the iterator indicates it is exhausted ``TOS`` is popped, and the byte code |
| 596 | counter is incremented by *delta*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 597 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 598 | |
| 599 | .. opcode:: LOAD_GLOBAL (namei) |
| 600 | |
| 601 | Loads the global named ``co_names[namei]`` onto the stack. |
| 602 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 603 | |
| 604 | .. opcode:: SETUP_LOOP (delta) |
| 605 | |
| 606 | Pushes a block for a loop onto the block stack. The block spans from the |
| 607 | current instruction with a size of *delta* bytes. |
| 608 | |
| 609 | |
| 610 | .. opcode:: SETUP_EXCEPT (delta) |
| 611 | |
| 612 | Pushes a try block from a try-except clause onto the block stack. *delta* points |
| 613 | to the first except block. |
| 614 | |
| 615 | |
| 616 | .. opcode:: SETUP_FINALLY (delta) |
| 617 | |
| 618 | Pushes a try block from a try-except clause onto the block stack. *delta* points |
| 619 | to the finally block. |
| 620 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 621 | .. opcode:: STORE_MAP () |
| 622 | |
| 623 | Store a key and value pair in a dictionary. Pops the key and value while leaving |
| 624 | the dictionary on the stack. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 625 | |
| 626 | .. opcode:: LOAD_FAST (var_num) |
| 627 | |
| 628 | Pushes a reference to the local ``co_varnames[var_num]`` onto the stack. |
| 629 | |
| 630 | |
| 631 | .. opcode:: STORE_FAST (var_num) |
| 632 | |
| 633 | Stores TOS into the local ``co_varnames[var_num]``. |
| 634 | |
| 635 | |
| 636 | .. opcode:: DELETE_FAST (var_num) |
| 637 | |
| 638 | Deletes local ``co_varnames[var_num]``. |
| 639 | |
| 640 | |
| 641 | .. opcode:: LOAD_CLOSURE (i) |
| 642 | |
| 643 | Pushes a reference to the cell contained in slot *i* of the cell and free |
| 644 | variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is |
| 645 | less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i - |
| 646 | len(co_cellvars)]``. |
| 647 | |
| 648 | |
| 649 | .. opcode:: LOAD_DEREF (i) |
| 650 | |
| 651 | Loads the cell contained in slot *i* of the cell and free variable storage. |
| 652 | Pushes a reference to the object the cell contains on the stack. |
| 653 | |
| 654 | |
| 655 | .. opcode:: STORE_DEREF (i) |
| 656 | |
| 657 | Stores TOS into the cell contained in slot *i* of the cell and free variable |
| 658 | storage. |
| 659 | |
| 660 | |
| 661 | .. opcode:: SET_LINENO (lineno) |
| 662 | |
| 663 | This opcode is obsolete. |
| 664 | |
| 665 | |
| 666 | .. opcode:: RAISE_VARARGS (argc) |
| 667 | |
| 668 | Raises an exception. *argc* indicates the number of parameters to the raise |
| 669 | statement, ranging from 0 to 3. The handler will find the traceback as TOS2, |
| 670 | the parameter as TOS1, and the exception as TOS. |
| 671 | |
| 672 | |
| 673 | .. opcode:: CALL_FUNCTION (argc) |
| 674 | |
| 675 | Calls a function. The low byte of *argc* indicates the number of positional |
| 676 | parameters, the high byte the number of keyword parameters. On the stack, the |
| 677 | opcode finds the keyword parameters first. For each keyword argument, the value |
| 678 | is on top of the key. Below the keyword parameters, the positional parameters |
| 679 | are on the stack, with the right-most parameter on top. Below the parameters, |
| 680 | the function object to call is on the stack. |
| 681 | |
| 682 | |
| 683 | .. opcode:: MAKE_FUNCTION (argc) |
| 684 | |
| 685 | Pushes a new function object on the stack. TOS is the code associated with the |
| 686 | function. The function object is defined to have *argc* default parameters, |
| 687 | which are found below TOS. |
| 688 | |
| 689 | |
| 690 | .. opcode:: MAKE_CLOSURE (argc) |
| 691 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 692 | Creates a new function object, sets its *__closure__* slot, and pushes it on |
| 693 | the stack. TOS is the code associated with the function, TOS1 the tuple |
| 694 | containing cells for the closure's free variables. The function also has |
| 695 | *argc* default parameters, which are found below the cells. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 696 | |
| 697 | |
| 698 | .. opcode:: BUILD_SLICE (argc) |
| 699 | |
| 700 | .. index:: builtin: slice |
| 701 | |
| 702 | Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2, |
| 703 | ``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] | 704 | pushed. See the :func:`slice` built-in function for more information. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 705 | |
| 706 | |
| 707 | .. opcode:: EXTENDED_ARG (ext) |
| 708 | |
| 709 | Prefixes any opcode which has an argument too big to fit into the default two |
| 710 | bytes. *ext* holds two additional bytes which, taken together with the |
| 711 | subsequent opcode's argument, comprise a four-byte argument, *ext* being the two |
| 712 | most-significant bytes. |
| 713 | |
| 714 | |
| 715 | .. opcode:: CALL_FUNCTION_VAR (argc) |
| 716 | |
| 717 | Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element |
| 718 | on the stack contains the variable argument list, followed by keyword and |
| 719 | positional arguments. |
| 720 | |
| 721 | |
| 722 | .. opcode:: CALL_FUNCTION_KW (argc) |
| 723 | |
| 724 | Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element |
| 725 | on the stack contains the keyword arguments dictionary, followed by explicit |
| 726 | keyword and positional arguments. |
| 727 | |
| 728 | |
| 729 | .. opcode:: CALL_FUNCTION_VAR_KW (argc) |
| 730 | |
| 731 | Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top |
| 732 | element on the stack contains the keyword arguments dictionary, followed by the |
| 733 | variable-arguments tuple, followed by explicit keyword and positional arguments. |
| 734 | |
| 735 | |
| 736 | .. opcode:: HAVE_ARGUMENT () |
| 737 | |
| 738 | This is not really an opcode. It identifies the dividing line between opcodes |
| 739 | which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>= |
| 740 | HAVE_ARGUMENT``. |
| 741 | |