Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 1 | :mod:`dis` --- Disassembler for Python bytecode |
| 2 | =============================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: dis |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 5 | :synopsis: Disassembler for Python bytecode. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 6 | |
Raymond Hettinger | 1048094 | 2011-01-10 03:26:08 +0000 | [diff] [blame] | 7 | **Source code:** :source:`Lib/dis.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | |
Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 9 | -------------- |
| 10 | |
Brett Cannon | 8315fd1 | 2010-07-02 22:03:00 +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 |
Georg Brandl | 71515ca | 2009-05-17 12:29:12 +0000 | [diff] [blame] | 13 | input is defined in the file :file:`Include/opcode.h` and used by the compiler |
| 14 | and the interpreter. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | |
Georg Brandl | 19b7a87 | 2010-07-03 10:21:50 +0000 | [diff] [blame] | 16 | .. impl-detail:: |
| 17 | |
Raymond Hettinger | 1048094 | 2011-01-10 03:26:08 +0000 | [diff] [blame] | 18 | Bytecode is an implementation detail of the CPython interpreter. No |
Georg Brandl | 19b7a87 | 2010-07-03 10:21:50 +0000 | [diff] [blame] | 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 | |
Brett Cannon | 8315fd1 | 2010-07-02 22:03:00 +0000 | [diff] [blame] | 23 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | Example: Given the function :func:`myfunc`:: |
| 25 | |
| 26 | def myfunc(alist): |
| 27 | return len(alist) |
| 28 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 29 | the following command can be used to display the disassembly of |
| 30 | :func:`myfunc`:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | |
| 32 | >>> dis.dis(myfunc) |
| 33 | 2 0 LOAD_GLOBAL 0 (len) |
| 34 | 3 LOAD_FAST 0 (alist) |
| 35 | 6 CALL_FUNCTION 1 |
| 36 | 9 RETURN_VALUE |
| 37 | |
| 38 | (The "2" is a line number). |
| 39 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 40 | Bytecode analysis |
| 41 | ----------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 42 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 43 | The bytecode analysis API allows pieces of Python code to be wrapped in a |
| 44 | :class:`Bytecode` object that provides easy access to details of the |
| 45 | compiled code. |
| 46 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame^] | 47 | .. class:: Bytecode(x, *, first_line=None) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 48 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame^] | 49 | Analyse the bytecode corresponding to a function, method, string of |
| 50 | source code, or a code object (as returned by :func:`compile`). |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 51 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame^] | 52 | This is a convenience wrapper around many of the functions listed below, |
| 53 | most notably :func:`get_instructions`, as iterating over a |
| 54 | :class:`ByteCode` instance yields the bytecode operations as |
| 55 | :class:`Instruction` instances. |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 56 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame^] | 57 | If *first_line* is not None, it indicates the line number that should |
| 58 | be reported for the first source line in the disassembled code. |
| 59 | Otherwise, the source line information (if any) is taken directly from |
| 60 | the disassembled code object. |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 61 | |
| 62 | .. data:: codeobj |
| 63 | |
| 64 | The compiled code object. |
| 65 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame^] | 66 | .. data:: first_line |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 67 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame^] | 68 | The first source line of the code object (if available) |
| 69 | |
| 70 | .. method:: dis() |
| 71 | |
| 72 | Return a formatted view of the bytecode operations (the same as |
| 73 | printed by :func:`dis`, but returned as a multi-line string). |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 74 | |
| 75 | .. method:: info() |
| 76 | |
| 77 | Return a formatted multi-line string with detailed information about the |
| 78 | code object, like :func:`code_info`. |
| 79 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 80 | Example:: |
| 81 | |
| 82 | >>> bytecode = dis.Bytecode(myfunc) |
| 83 | >>> for instr in bytecode: |
| 84 | ... print(instr.opname) |
| 85 | ... |
| 86 | LOAD_GLOBAL |
| 87 | LOAD_FAST |
| 88 | CALL_FUNCTION |
| 89 | RETURN_VALUE |
| 90 | |
| 91 | |
| 92 | Analysis functions |
| 93 | ------------------ |
| 94 | |
| 95 | The :mod:`dis` module also defines the following analysis functions that |
| 96 | convert the input directly to the desired output. They can be useful if |
| 97 | only a single operation is being performed, so the intermediate analysis |
| 98 | object isn't useful: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | |
Nick Coghlan | e8814fb | 2010-09-10 14:08:04 +0000 | [diff] [blame] | 100 | .. function:: code_info(x) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 101 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 102 | Return a formatted multi-line string with detailed code object information |
| 103 | for the supplied function, method, source code string or code object. |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 104 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 105 | Note that the exact contents of code info strings are highly implementation |
| 106 | dependent and they may change arbitrarily across Python VMs or Python |
| 107 | releases. |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 108 | |
| 109 | .. versionadded:: 3.2 |
| 110 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 111 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 112 | .. function:: show_code(x, *, file=None) |
Nick Coghlan | e8814fb | 2010-09-10 14:08:04 +0000 | [diff] [blame] | 113 | |
| 114 | Print detailed code object information for the supplied function, method, |
Ezio Melotti | 6e6c6ac | 2013-08-23 22:41:39 +0300 | [diff] [blame] | 115 | source code string or code object to *file* (or ``sys.stdout`` if *file* |
| 116 | is not specified). |
Nick Coghlan | e8814fb | 2010-09-10 14:08:04 +0000 | [diff] [blame] | 117 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 118 | This is a convenient shorthand for ``print(code_info(x), file=file)``, |
| 119 | intended for interactive exploration at the interpreter prompt. |
Nick Coghlan | e8814fb | 2010-09-10 14:08:04 +0000 | [diff] [blame] | 120 | |
| 121 | .. versionadded:: 3.2 |
| 122 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 123 | .. versionchanged:: 3.4 |
| 124 | Added ``file`` parameter |
| 125 | |
| 126 | |
| 127 | .. function:: dis(x=None, *, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 129 | Disassemble the *x* object. *x* can denote either a module, a class, a |
| 130 | method, a function, a code object, a string of source code or a byte sequence |
| 131 | of raw bytecode. For a module, it disassembles all functions. For a class, |
| 132 | it disassembles all methods. For a code object or sequence of raw bytecode, |
| 133 | it prints one line per bytecode instruction. Strings are first compiled to |
| 134 | code objects with the :func:`compile` built-in function before being |
| 135 | disassembled. If no object is provided, this function disassembles the last |
| 136 | traceback. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 137 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 138 | The disassembly is written as text to the supplied ``file`` argument if |
| 139 | provided and to ``sys.stdout`` otherwise. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 140 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 141 | .. versionchanged:: 3.4 |
| 142 | Added ``file`` parameter |
| 143 | |
| 144 | |
| 145 | .. function:: distb(tb=None, *, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 147 | Disassemble the top-of-stack function of a traceback, using the last |
| 148 | traceback if none was passed. The instruction causing the exception is |
| 149 | indicated. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 151 | The disassembly is written as text to the supplied ``file`` argument if |
| 152 | provided and to ``sys.stdout`` otherwise. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 153 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 154 | .. versionchanged:: 3.4 |
| 155 | Added ``file`` parameter |
| 156 | |
| 157 | |
| 158 | .. function:: disassemble(code, lasti=-1, *, file=None) |
| 159 | disco(code, lasti=-1, *, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 160 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 161 | Disassemble a code object, indicating the last instruction if *lasti* was |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | provided. The output is divided in the following columns: |
| 163 | |
| 164 | #. the line number, for the first instruction of each line |
| 165 | #. the current instruction, indicated as ``-->``, |
| 166 | #. a labelled instruction, indicated with ``>>``, |
| 167 | #. the address of the instruction, |
| 168 | #. the operation code name, |
| 169 | #. operation parameters, and |
| 170 | #. interpretation of the parameters in parentheses. |
| 171 | |
| 172 | The parameter interpretation recognizes local and global variable names, |
| 173 | constant values, branch targets, and compare operators. |
| 174 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 175 | The disassembly is written as text to the supplied ``file`` argument if |
| 176 | provided and to ``sys.stdout`` otherwise. |
| 177 | |
| 178 | .. versionchanged:: 3.4 |
| 179 | Added ``file`` parameter |
| 180 | |
| 181 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame^] | 182 | .. function:: get_instructions(x, *, first_line=None) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 183 | |
| 184 | Return an iterator over the instructions in the supplied function, method, |
| 185 | source code string or code object. |
| 186 | |
| 187 | The iterator generates a series of :class:`Instruction` named tuples |
| 188 | giving the details of each operation in the supplied code. |
| 189 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame^] | 190 | If *first_line* is not None, it indicates the line number that should |
| 191 | be reported for the first source line in the disassembled code. |
| 192 | Otherwise, the source line information (if any) is taken directly from |
| 193 | the disassembled code object. |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 194 | |
| 195 | .. versionadded:: 3.4 |
| 196 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 197 | |
Benjamin Peterson | 75edad0 | 2009-01-01 15:05:06 +0000 | [diff] [blame] | 198 | .. function:: findlinestarts(code) |
| 199 | |
| 200 | This generator function uses the ``co_firstlineno`` and ``co_lnotab`` |
| 201 | attributes of the code object *code* to find the offsets which are starts of |
| 202 | lines in the source code. They are generated as ``(offset, lineno)`` pairs. |
| 203 | |
| 204 | |
| 205 | .. function:: findlabels(code) |
| 206 | |
| 207 | Detect all offsets in the code object *code* which are jump targets, and |
| 208 | return a list of these offsets. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 209 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 210 | .. _bytecodes: |
| 211 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 212 | Python Bytecode Instructions |
| 213 | ---------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 214 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 215 | The :func:`get_instructions` function and :class:`Bytecode` class provide |
| 216 | details of bytecode instructions as :class:`Instruction` instances: |
| 217 | |
| 218 | .. class:: Instruction |
| 219 | |
| 220 | Details for a bytecode operation |
| 221 | |
| 222 | .. data:: opcode |
| 223 | |
| 224 | numeric code for operation, corresponding to the opcode values listed |
| 225 | below and the bytecode values in the :ref:`opcode_collections`. |
| 226 | |
| 227 | |
| 228 | .. data:: opname |
| 229 | |
| 230 | human readable name for operation |
| 231 | |
| 232 | |
| 233 | .. data:: arg |
| 234 | |
| 235 | numeric argument to operation (if any), otherwise None |
| 236 | |
| 237 | |
| 238 | .. data:: argval |
| 239 | |
| 240 | resolved arg value (if known), otherwise same as arg |
| 241 | |
| 242 | |
| 243 | .. data:: argrepr |
| 244 | |
| 245 | human readable description of operation argument |
| 246 | |
| 247 | |
| 248 | .. data:: offset |
| 249 | |
| 250 | start index of operation within bytecode sequence |
| 251 | |
| 252 | |
| 253 | .. data:: starts_line |
| 254 | |
| 255 | line started by this opcode (if any), otherwise None |
| 256 | |
| 257 | |
| 258 | .. data:: is_jump_target |
| 259 | |
| 260 | True if other code jumps to here, otherwise False |
| 261 | |
| 262 | .. versionadded:: 3.4 |
| 263 | |
| 264 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 265 | The Python compiler currently generates the following bytecode instructions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 266 | |
| 267 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 268 | **General instructions** |
| 269 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 270 | .. opcode:: NOP |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 271 | |
| 272 | Do nothing code. Used as a placeholder by the bytecode optimizer. |
| 273 | |
| 274 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 275 | .. opcode:: POP_TOP |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 276 | |
| 277 | Removes the top-of-stack (TOS) item. |
| 278 | |
| 279 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 280 | .. opcode:: ROT_TWO |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 281 | |
| 282 | Swaps the two top-most stack items. |
| 283 | |
| 284 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 285 | .. opcode:: ROT_THREE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 286 | |
| 287 | Lifts second and third stack item one position up, moves top down to position |
| 288 | three. |
| 289 | |
| 290 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 291 | .. opcode:: DUP_TOP |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 292 | |
| 293 | Duplicates the reference on top of the stack. |
| 294 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 295 | |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 296 | .. opcode:: DUP_TOP_TWO |
| 297 | |
| 298 | Duplicates the two references on top of the stack, leaving them in the |
| 299 | same order. |
| 300 | |
| 301 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 302 | **Unary operations** |
| 303 | |
| 304 | Unary operations take the top of the stack, apply the operation, and push the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 305 | result back on the stack. |
| 306 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 307 | .. opcode:: UNARY_POSITIVE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 308 | |
| 309 | Implements ``TOS = +TOS``. |
| 310 | |
| 311 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 312 | .. opcode:: UNARY_NEGATIVE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 313 | |
| 314 | Implements ``TOS = -TOS``. |
| 315 | |
| 316 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 317 | .. opcode:: UNARY_NOT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 318 | |
| 319 | Implements ``TOS = not TOS``. |
| 320 | |
| 321 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 322 | .. opcode:: UNARY_INVERT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 323 | |
| 324 | Implements ``TOS = ~TOS``. |
| 325 | |
| 326 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 327 | .. opcode:: GET_ITER |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 328 | |
| 329 | Implements ``TOS = iter(TOS)``. |
| 330 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 331 | |
| 332 | **Binary operations** |
| 333 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 334 | Binary operations remove the top of the stack (TOS) and the second top-most |
| 335 | stack item (TOS1) from the stack. They perform the operation, and put the |
| 336 | result back on the stack. |
| 337 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 338 | .. opcode:: BINARY_POWER |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 339 | |
| 340 | Implements ``TOS = TOS1 ** TOS``. |
| 341 | |
| 342 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 343 | .. opcode:: BINARY_MULTIPLY |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 344 | |
| 345 | Implements ``TOS = TOS1 * TOS``. |
| 346 | |
| 347 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 348 | .. opcode:: BINARY_FLOOR_DIVIDE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 349 | |
| 350 | Implements ``TOS = TOS1 // TOS``. |
| 351 | |
| 352 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 353 | .. opcode:: BINARY_TRUE_DIVIDE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 354 | |
Ezio Melotti | 7de0a6e | 2010-01-05 08:37:27 +0000 | [diff] [blame] | 355 | Implements ``TOS = TOS1 / TOS``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 356 | |
| 357 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 358 | .. opcode:: BINARY_MODULO |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 359 | |
| 360 | Implements ``TOS = TOS1 % TOS``. |
| 361 | |
| 362 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 363 | .. opcode:: BINARY_ADD |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 364 | |
| 365 | Implements ``TOS = TOS1 + TOS``. |
| 366 | |
| 367 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 368 | .. opcode:: BINARY_SUBTRACT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 369 | |
| 370 | Implements ``TOS = TOS1 - TOS``. |
| 371 | |
| 372 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 373 | .. opcode:: BINARY_SUBSCR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 374 | |
| 375 | Implements ``TOS = TOS1[TOS]``. |
| 376 | |
| 377 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 378 | .. opcode:: BINARY_LSHIFT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 379 | |
| 380 | Implements ``TOS = TOS1 << TOS``. |
| 381 | |
| 382 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 383 | .. opcode:: BINARY_RSHIFT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 384 | |
| 385 | Implements ``TOS = TOS1 >> TOS``. |
| 386 | |
| 387 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 388 | .. opcode:: BINARY_AND |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 389 | |
| 390 | Implements ``TOS = TOS1 & TOS``. |
| 391 | |
| 392 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 393 | .. opcode:: BINARY_XOR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 394 | |
| 395 | Implements ``TOS = TOS1 ^ TOS``. |
| 396 | |
| 397 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 398 | .. opcode:: BINARY_OR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 399 | |
| 400 | Implements ``TOS = TOS1 | TOS``. |
| 401 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 402 | |
| 403 | **In-place operations** |
| 404 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 405 | In-place operations are like binary operations, in that they remove TOS and |
| 406 | TOS1, and push the result back on the stack, but the operation is done in-place |
| 407 | when TOS1 supports it, and the resulting TOS may be (but does not have to be) |
| 408 | the original TOS1. |
| 409 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 410 | .. opcode:: INPLACE_POWER |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 411 | |
| 412 | Implements in-place ``TOS = TOS1 ** TOS``. |
| 413 | |
| 414 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 415 | .. opcode:: INPLACE_MULTIPLY |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 416 | |
| 417 | Implements in-place ``TOS = TOS1 * TOS``. |
| 418 | |
| 419 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 420 | .. opcode:: INPLACE_FLOOR_DIVIDE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 421 | |
| 422 | Implements in-place ``TOS = TOS1 // TOS``. |
| 423 | |
| 424 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 425 | .. opcode:: INPLACE_TRUE_DIVIDE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 426 | |
Ezio Melotti | 7de0a6e | 2010-01-05 08:37:27 +0000 | [diff] [blame] | 427 | Implements in-place ``TOS = TOS1 / TOS``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 428 | |
| 429 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 430 | .. opcode:: INPLACE_MODULO |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 431 | |
| 432 | Implements in-place ``TOS = TOS1 % TOS``. |
| 433 | |
| 434 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 435 | .. opcode:: INPLACE_ADD |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 436 | |
| 437 | Implements in-place ``TOS = TOS1 + TOS``. |
| 438 | |
| 439 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 440 | .. opcode:: INPLACE_SUBTRACT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 441 | |
| 442 | Implements in-place ``TOS = TOS1 - TOS``. |
| 443 | |
| 444 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 445 | .. opcode:: INPLACE_LSHIFT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 446 | |
| 447 | Implements in-place ``TOS = TOS1 << TOS``. |
| 448 | |
| 449 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 450 | .. opcode:: INPLACE_RSHIFT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 451 | |
| 452 | Implements in-place ``TOS = TOS1 >> TOS``. |
| 453 | |
| 454 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 455 | .. opcode:: INPLACE_AND |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 456 | |
| 457 | Implements in-place ``TOS = TOS1 & TOS``. |
| 458 | |
| 459 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 460 | .. opcode:: INPLACE_XOR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 461 | |
| 462 | Implements in-place ``TOS = TOS1 ^ TOS``. |
| 463 | |
| 464 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 465 | .. opcode:: INPLACE_OR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 466 | |
| 467 | Implements in-place ``TOS = TOS1 | TOS``. |
| 468 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 469 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 470 | .. opcode:: STORE_SUBSCR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 471 | |
| 472 | Implements ``TOS1[TOS] = TOS2``. |
| 473 | |
| 474 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 475 | .. opcode:: DELETE_SUBSCR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 476 | |
| 477 | Implements ``del TOS1[TOS]``. |
| 478 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 479 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 480 | **Miscellaneous opcodes** |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 481 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 482 | .. opcode:: PRINT_EXPR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 483 | |
| 484 | Implements the expression statement for the interactive mode. TOS is removed |
| 485 | from the stack and printed. In non-interactive mode, an expression statement is |
| 486 | terminated with ``POP_STACK``. |
| 487 | |
| 488 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 489 | .. opcode:: BREAK_LOOP |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 490 | |
| 491 | Terminates a loop due to a :keyword:`break` statement. |
| 492 | |
| 493 | |
| 494 | .. opcode:: CONTINUE_LOOP (target) |
| 495 | |
| 496 | Continues a loop due to a :keyword:`continue` statement. *target* is the |
| 497 | address to jump to (which should be a ``FOR_ITER`` instruction). |
| 498 | |
| 499 | |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 500 | .. opcode:: SET_ADD (i) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 501 | |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 502 | Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 503 | |
| 504 | |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 505 | .. opcode:: LIST_APPEND (i) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 506 | |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 507 | Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions. |
| 508 | |
| 509 | |
| 510 | .. opcode:: MAP_ADD (i) |
| 511 | |
| 512 | Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict |
| 513 | comprehensions. |
| 514 | |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 515 | For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the |
| 516 | added value or key/value pair is popped off, the container object remains on |
| 517 | the stack so that it is available for further iterations of the loop. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 518 | |
| 519 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 520 | .. opcode:: RETURN_VALUE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 521 | |
| 522 | Returns with TOS to the caller of the function. |
| 523 | |
| 524 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 525 | .. opcode:: YIELD_VALUE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 526 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 527 | Pops ``TOS`` and yields it from a :term:`generator`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 528 | |
| 529 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 530 | .. opcode:: YIELD_FROM |
| 531 | |
| 532 | Pops ``TOS`` and delegates to it as a subiterator from a :term:`generator`. |
| 533 | |
| 534 | .. versionadded:: 3.3 |
| 535 | |
| 536 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 537 | .. opcode:: IMPORT_STAR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 538 | |
| 539 | Loads all symbols not starting with ``'_'`` directly from the module TOS to the |
| 540 | local namespace. The module is popped after loading all names. This opcode |
| 541 | implements ``from module import *``. |
| 542 | |
| 543 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 544 | .. opcode:: POP_BLOCK |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 545 | |
| 546 | Removes one block from the block stack. Per frame, there is a stack of blocks, |
| 547 | denoting nested loops, try statements, and such. |
| 548 | |
| 549 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 550 | .. opcode:: POP_EXCEPT |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 551 | |
| 552 | Removes one block from the block stack. The popped block must be an exception |
| 553 | handler block, as implicitly created when entering an except handler. |
| 554 | In addition to popping extraneous values from the frame stack, the |
| 555 | last three popped values are used to restore the exception state. |
| 556 | |
| 557 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 558 | .. opcode:: END_FINALLY |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 559 | |
| 560 | Terminates a :keyword:`finally` clause. The interpreter recalls whether the |
| 561 | exception has to be re-raised, or whether the function returns, and continues |
| 562 | with the outer-next block. |
| 563 | |
| 564 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 565 | .. opcode:: LOAD_BUILD_CLASS |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 566 | |
Georg Brandl | 5ac2230 | 2008-07-20 21:39:03 +0000 | [diff] [blame] | 567 | Pushes :func:`builtins.__build_class__` onto the stack. It is later called |
Benjamin Peterson | aac8fd3 | 2008-07-20 22:02:26 +0000 | [diff] [blame] | 568 | by ``CALL_FUNCTION`` to construct a class. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 569 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 570 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 571 | .. opcode:: SETUP_WITH (delta) |
| 572 | |
| 573 | This opcode performs several operations before a with block starts. First, |
| 574 | it loads :meth:`~object.__exit__` from the context manager and pushes it onto |
| 575 | the stack for later use by :opcode:`WITH_CLEANUP`. Then, |
| 576 | :meth:`~object.__enter__` is called, and a finally block pointing to *delta* |
| 577 | is pushed. Finally, the result of calling the enter method is pushed onto |
| 578 | the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or |
| 579 | store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or |
| 580 | :opcode:`UNPACK_SEQUENCE`). |
| 581 | |
| 582 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 583 | .. opcode:: WITH_CLEANUP |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 584 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 585 | Cleans up the stack when a :keyword:`with` statement block exits. TOS is |
| 586 | the context manager's :meth:`__exit__` bound method. Below TOS are 1--3 |
| 587 | values indicating how/why the finally clause was entered: |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 588 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 589 | * SECOND = ``None`` |
| 590 | * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval |
| 591 | * SECOND = ``WHY_*``; no retval below it |
| 592 | * (SECOND, THIRD, FOURTH) = exc_info() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 593 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 594 | In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise |
| 595 | ``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] | 596 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 597 | If the stack represents an exception, *and* the function call returns |
| 598 | a 'true' value, this information is "zapped" and replaced with a single |
| 599 | ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception. |
| 600 | (But non-local gotos will still be resumed.) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 601 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 602 | .. XXX explain the WHY stuff! |
| 603 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 604 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 605 | All of the following opcodes expect arguments. An argument is two bytes, with |
| 606 | the more significant byte last. |
| 607 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 608 | .. opcode:: STORE_NAME (namei) |
| 609 | |
| 610 | Implements ``name = TOS``. *namei* is the index of *name* in the attribute |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 611 | :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] | 612 | or ``STORE_GLOBAL`` if possible. |
| 613 | |
| 614 | |
| 615 | .. opcode:: DELETE_NAME (namei) |
| 616 | |
| 617 | Implements ``del name``, where *namei* is the index into :attr:`co_names` |
| 618 | attribute of the code object. |
| 619 | |
| 620 | |
| 621 | .. opcode:: UNPACK_SEQUENCE (count) |
| 622 | |
| 623 | Unpacks TOS into *count* individual values, which are put onto the stack |
| 624 | right-to-left. |
| 625 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 626 | |
Georg Brandl | 5ac2230 | 2008-07-20 21:39:03 +0000 | [diff] [blame] | 627 | .. opcode:: UNPACK_EX (counts) |
| 628 | |
| 629 | Implements assignment with a starred target: Unpacks an iterable in TOS into |
| 630 | individual values, where the total number of values can be smaller than the |
| 631 | number of items in the iterable: one the new values will be a list of all |
| 632 | leftover items. |
| 633 | |
| 634 | The low byte of *counts* is the number of values before the list value, the |
| 635 | high byte of *counts* the number of values after it. The resulting values |
| 636 | are put onto the stack right-to-left. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 637 | |
Georg Brandl | 5ac2230 | 2008-07-20 21:39:03 +0000 | [diff] [blame] | 638 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 639 | .. opcode:: STORE_ATTR (namei) |
| 640 | |
| 641 | Implements ``TOS.name = TOS1``, where *namei* is the index of name in |
| 642 | :attr:`co_names`. |
| 643 | |
| 644 | |
| 645 | .. opcode:: DELETE_ATTR (namei) |
| 646 | |
| 647 | Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`. |
| 648 | |
| 649 | |
| 650 | .. opcode:: STORE_GLOBAL (namei) |
| 651 | |
| 652 | Works as ``STORE_NAME``, but stores the name as a global. |
| 653 | |
| 654 | |
| 655 | .. opcode:: DELETE_GLOBAL (namei) |
| 656 | |
| 657 | Works as ``DELETE_NAME``, but deletes a global name. |
| 658 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 659 | |
| 660 | .. opcode:: LOAD_CONST (consti) |
| 661 | |
| 662 | Pushes ``co_consts[consti]`` onto the stack. |
| 663 | |
| 664 | |
| 665 | .. opcode:: LOAD_NAME (namei) |
| 666 | |
| 667 | Pushes the value associated with ``co_names[namei]`` onto the stack. |
| 668 | |
| 669 | |
| 670 | .. opcode:: BUILD_TUPLE (count) |
| 671 | |
| 672 | Creates a tuple consuming *count* items from the stack, and pushes the resulting |
| 673 | tuple onto the stack. |
| 674 | |
| 675 | |
| 676 | .. opcode:: BUILD_LIST (count) |
| 677 | |
| 678 | Works as ``BUILD_TUPLE``, but creates a list. |
| 679 | |
| 680 | |
| 681 | .. opcode:: BUILD_SET (count) |
| 682 | |
| 683 | Works as ``BUILD_TUPLE``, but creates a set. |
| 684 | |
| 685 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 686 | .. opcode:: BUILD_MAP (count) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 687 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 688 | Pushes a new dictionary object onto the stack. The dictionary is pre-sized |
| 689 | to hold *count* entries. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 690 | |
| 691 | |
| 692 | .. opcode:: LOAD_ATTR (namei) |
| 693 | |
| 694 | Replaces TOS with ``getattr(TOS, co_names[namei])``. |
| 695 | |
| 696 | |
| 697 | .. opcode:: COMPARE_OP (opname) |
| 698 | |
| 699 | Performs a Boolean operation. The operation name can be found in |
| 700 | ``cmp_op[opname]``. |
| 701 | |
| 702 | |
| 703 | .. opcode:: IMPORT_NAME (namei) |
| 704 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 705 | Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide |
| 706 | the *fromlist* and *level* arguments of :func:`__import__`. The module |
| 707 | object is pushed onto the stack. The current namespace is not affected: |
| 708 | for a proper import statement, a subsequent ``STORE_FAST`` instruction |
| 709 | modifies the namespace. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 710 | |
| 711 | |
| 712 | .. opcode:: IMPORT_FROM (namei) |
| 713 | |
| 714 | Loads the attribute ``co_names[namei]`` from the module found in TOS. The |
| 715 | resulting object is pushed onto the stack, to be subsequently stored by a |
| 716 | ``STORE_FAST`` instruction. |
| 717 | |
| 718 | |
| 719 | .. opcode:: JUMP_FORWARD (delta) |
| 720 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 721 | Increments bytecode counter by *delta*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 722 | |
| 723 | |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 724 | .. opcode:: POP_JUMP_IF_TRUE (target) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 725 | |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 726 | If TOS is true, sets the bytecode counter to *target*. TOS is popped. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 727 | |
| 728 | |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 729 | .. opcode:: POP_JUMP_IF_FALSE (target) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 730 | |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 731 | If TOS is false, sets the bytecode counter to *target*. TOS is popped. |
| 732 | |
| 733 | |
| 734 | .. opcode:: JUMP_IF_TRUE_OR_POP (target) |
| 735 | |
| 736 | If TOS is true, sets the bytecode counter to *target* and leaves TOS |
| 737 | on the stack. Otherwise (TOS is false), TOS is popped. |
| 738 | |
| 739 | |
| 740 | .. opcode:: JUMP_IF_FALSE_OR_POP (target) |
| 741 | |
| 742 | If TOS is false, sets the bytecode counter to *target* and leaves |
| 743 | TOS on the stack. Otherwise (TOS is true), TOS is popped. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 744 | |
| 745 | |
| 746 | .. opcode:: JUMP_ABSOLUTE (target) |
| 747 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 748 | Set bytecode counter to *target*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 749 | |
| 750 | |
| 751 | .. opcode:: FOR_ITER (delta) |
| 752 | |
Ezio Melotti | 7fa8222 | 2012-10-12 13:42:08 +0300 | [diff] [blame] | 753 | ``TOS`` is an :term:`iterator`. Call its :meth:`~iterator.__next__` method. |
| 754 | If this yields a new value, push it on the stack (leaving the iterator below |
| 755 | it). If the iterator indicates it is exhausted ``TOS`` is popped, and the |
| 756 | byte code counter is incremented by *delta*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 757 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 758 | |
| 759 | .. opcode:: LOAD_GLOBAL (namei) |
| 760 | |
| 761 | Loads the global named ``co_names[namei]`` onto the stack. |
| 762 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 763 | |
| 764 | .. opcode:: SETUP_LOOP (delta) |
| 765 | |
| 766 | Pushes a block for a loop onto the block stack. The block spans from the |
| 767 | current instruction with a size of *delta* bytes. |
| 768 | |
| 769 | |
| 770 | .. opcode:: SETUP_EXCEPT (delta) |
| 771 | |
| 772 | Pushes a try block from a try-except clause onto the block stack. *delta* points |
| 773 | to the first except block. |
| 774 | |
| 775 | |
| 776 | .. opcode:: SETUP_FINALLY (delta) |
| 777 | |
| 778 | Pushes a try block from a try-except clause onto the block stack. *delta* points |
| 779 | to the finally block. |
| 780 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 781 | .. opcode:: STORE_MAP |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 782 | |
| 783 | Store a key and value pair in a dictionary. Pops the key and value while leaving |
| 784 | the dictionary on the stack. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 785 | |
| 786 | .. opcode:: LOAD_FAST (var_num) |
| 787 | |
| 788 | Pushes a reference to the local ``co_varnames[var_num]`` onto the stack. |
| 789 | |
| 790 | |
| 791 | .. opcode:: STORE_FAST (var_num) |
| 792 | |
| 793 | Stores TOS into the local ``co_varnames[var_num]``. |
| 794 | |
| 795 | |
| 796 | .. opcode:: DELETE_FAST (var_num) |
| 797 | |
| 798 | Deletes local ``co_varnames[var_num]``. |
| 799 | |
| 800 | |
| 801 | .. opcode:: LOAD_CLOSURE (i) |
| 802 | |
| 803 | Pushes a reference to the cell contained in slot *i* of the cell and free |
| 804 | variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is |
| 805 | less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i - |
| 806 | len(co_cellvars)]``. |
| 807 | |
| 808 | |
| 809 | .. opcode:: LOAD_DEREF (i) |
| 810 | |
| 811 | Loads the cell contained in slot *i* of the cell and free variable storage. |
| 812 | Pushes a reference to the object the cell contains on the stack. |
| 813 | |
| 814 | |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 815 | .. opcode:: LOAD_CLASSDEREF (i) |
| 816 | |
| 817 | Much like :opcode:`LOAD_DEREF` but first checks the locals dictionary before |
| 818 | consulting the cell. This is used for loading free variables in class |
| 819 | bodies. |
| 820 | |
| 821 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 822 | .. opcode:: STORE_DEREF (i) |
| 823 | |
| 824 | Stores TOS into the cell contained in slot *i* of the cell and free variable |
| 825 | storage. |
| 826 | |
| 827 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 828 | .. opcode:: DELETE_DEREF (i) |
| 829 | |
| 830 | Empties the cell contained in slot *i* of the cell and free variable storage. |
| 831 | Used by the :keyword:`del` statement. |
| 832 | |
| 833 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 834 | .. opcode:: RAISE_VARARGS (argc) |
| 835 | |
| 836 | Raises an exception. *argc* indicates the number of parameters to the raise |
| 837 | statement, ranging from 0 to 3. The handler will find the traceback as TOS2, |
| 838 | the parameter as TOS1, and the exception as TOS. |
| 839 | |
| 840 | |
| 841 | .. opcode:: CALL_FUNCTION (argc) |
| 842 | |
| 843 | Calls a function. The low byte of *argc* indicates the number of positional |
| 844 | parameters, the high byte the number of keyword parameters. On the stack, the |
| 845 | opcode finds the keyword parameters first. For each keyword argument, the value |
| 846 | is on top of the key. Below the keyword parameters, the positional parameters |
| 847 | are on the stack, with the right-most parameter on top. Below the parameters, |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 848 | the function object to call is on the stack. Pops all function arguments, and |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 849 | the function itself off the stack, and pushes the return value. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 850 | |
| 851 | |
| 852 | .. opcode:: MAKE_FUNCTION (argc) |
| 853 | |
Georg Brandl | c96ef1f | 2013-10-12 18:41:18 +0200 | [diff] [blame] | 854 | Pushes a new function object on the stack. From bottom to top, the consumed |
| 855 | stack must consist of |
| 856 | |
| 857 | * ``argc & 0xFF`` default argument objects in positional order |
| 858 | * ``(argc >> 8) & 0xFF`` pairs of name and default argument, with the name |
| 859 | just below the object on the stack, for keyword-only parameters |
| 860 | * ``(argc >> 16) & 0x7FFF`` parameter annotation objects |
| 861 | * a tuple listing the parameter names for the annotations (only if there are |
| 862 | ony annotation objects) |
| 863 | * the code associated with the function (at TOS1) |
| 864 | * the :term:`qualified name` of the function (at TOS) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 865 | |
| 866 | |
| 867 | .. opcode:: MAKE_CLOSURE (argc) |
| 868 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 869 | Creates a new function object, sets its *__closure__* slot, and pushes it on |
Andrew Svetlov | a5c4309 | 2012-11-23 15:28:34 +0200 | [diff] [blame] | 870 | the stack. TOS is the :term:`qualified name` of the function, TOS1 is the |
| 871 | code associated with the function, and TOS2 is the tuple containing cells for |
| 872 | the closure's free variables. The function also has *argc* default parameters, |
| 873 | which are found below the cells. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 874 | |
| 875 | |
| 876 | .. opcode:: BUILD_SLICE (argc) |
| 877 | |
| 878 | .. index:: builtin: slice |
| 879 | |
| 880 | Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2, |
| 881 | ``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] | 882 | pushed. See the :func:`slice` built-in function for more information. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 883 | |
| 884 | |
| 885 | .. opcode:: EXTENDED_ARG (ext) |
| 886 | |
| 887 | Prefixes any opcode which has an argument too big to fit into the default two |
| 888 | bytes. *ext* holds two additional bytes which, taken together with the |
| 889 | subsequent opcode's argument, comprise a four-byte argument, *ext* being the two |
| 890 | most-significant bytes. |
| 891 | |
| 892 | |
| 893 | .. opcode:: CALL_FUNCTION_VAR (argc) |
| 894 | |
| 895 | Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element |
| 896 | on the stack contains the variable argument list, followed by keyword and |
| 897 | positional arguments. |
| 898 | |
| 899 | |
| 900 | .. opcode:: CALL_FUNCTION_KW (argc) |
| 901 | |
| 902 | Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element |
| 903 | on the stack contains the keyword arguments dictionary, followed by explicit |
| 904 | keyword and positional arguments. |
| 905 | |
| 906 | |
| 907 | .. opcode:: CALL_FUNCTION_VAR_KW (argc) |
| 908 | |
| 909 | Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top |
| 910 | element on the stack contains the keyword arguments dictionary, followed by the |
| 911 | variable-arguments tuple, followed by explicit keyword and positional arguments. |
| 912 | |
| 913 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 914 | .. opcode:: HAVE_ARGUMENT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 915 | |
| 916 | This is not really an opcode. It identifies the dividing line between opcodes |
| 917 | which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>= |
| 918 | HAVE_ARGUMENT``. |
| 919 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 920 | .. _opcode_collections: |
| 921 | |
| 922 | Opcode collections |
| 923 | ------------------ |
| 924 | |
| 925 | These collections are provided for automatic introspection of bytecode |
| 926 | instructions: |
| 927 | |
| 928 | .. data:: opname |
| 929 | |
| 930 | Sequence of operation names, indexable using the bytecode. |
| 931 | |
| 932 | |
| 933 | .. data:: opmap |
| 934 | |
| 935 | Dictionary mapping operation names to bytecodes. |
| 936 | |
| 937 | |
| 938 | .. data:: cmp_op |
| 939 | |
| 940 | Sequence of all compare operation names. |
| 941 | |
| 942 | |
| 943 | .. data:: hasconst |
| 944 | |
| 945 | Sequence of bytecodes that have a constant parameter. |
| 946 | |
| 947 | |
| 948 | .. data:: hasfree |
| 949 | |
| 950 | Sequence of bytecodes that access a free variable (note that 'free' in |
| 951 | this context refers to names in the current scope that are referenced by |
| 952 | inner scopes or names in outer scopes that are referenced from this scope. |
| 953 | It does *not* include references to global or builtin scopes). |
| 954 | |
| 955 | |
| 956 | .. data:: hasname |
| 957 | |
| 958 | Sequence of bytecodes that access an attribute by name. |
| 959 | |
| 960 | |
| 961 | .. data:: hasjrel |
| 962 | |
| 963 | Sequence of bytecodes that have a relative jump target. |
| 964 | |
| 965 | |
| 966 | .. data:: hasjabs |
| 967 | |
| 968 | Sequence of bytecodes that have an absolute jump target. |
| 969 | |
| 970 | |
| 971 | .. data:: haslocal |
| 972 | |
| 973 | Sequence of bytecodes that access a local variable. |
| 974 | |
| 975 | |
| 976 | .. data:: hascompare |
| 977 | |
| 978 | Sequence of bytecodes of Boolean operations. |