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