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