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