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