The numbers below are from Python 3.10’s own opcode module, and meanings
from the 3.10 dis documentation.
Encoding
co_code is a sequence of 2-byte instructions: one byte of opcode and one
byte of argument. An opcode below 90 takes no argument; its argument byte is zero and
ignored.
EXTENDED_ARG widens an argument. It goes before the instruction, and its argument becomes the higher byte: up to three prefixes give the following instruction a 32-bit argument.
Jump arguments
From magic number 3435, a jump’s argument counts instructions, and every instruction is 2 bytes. In bytes:
| Kind | Instructions | Target in bytes |
|---|---|---|
| Relative | FOR_ITER, JUMP_FORWARD, SETUP_FINALLY, SETUP_WITH, SETUP_ASYNC_WITH | offset + 2 + 2 × argument |
| Absolute | JUMP_ABSOLUTE, JUMP_IF_FALSE_OR_POP, JUMP_IF_TRUE_OR_POP, POP_JUMP_IF_FALSE, POP_JUMP_IF_TRUE, JUMP_IF_NOT_EXC_MATCH | 2 × argument |
Pre-release files with magic numbers 3430 to 3434 count bytes.
This format has no inline caches and no exception table: exception handling is done
by SETUP_FINALLY, POP_BLOCK and the block stack at run time.
Every instruction
The argument, where one is used, is called arg. A local slot number indexes
co_varnames; a cell number counts co_cellvars first and then
co_freevars; a name index goes into co_names and a constant
index into co_consts.
| Name | 3.10 | Meaning |
|---|---|---|
POP_TOP | 1 | Removes the top of the stack |
ROT_TWO | 2 | Swaps the top two items |
ROT_THREE | 3 | Moves the top item down to position 3 |
DUP_TOP | 4 | Pushes a copy of the top item |
DUP_TOP_TWO | 5 | Duplicates the top two items, in order |
ROT_FOUR | 6 | Moves the top item down to position 4 |
NOP | 9 | Does nothing |
UNARY_POSITIVE | 10 | Replaces the top with + of it |
UNARY_NEGATIVE | 11 | Replaces the top with - of it |
UNARY_NOT | 12 | Replaces the top with not of it |
UNARY_INVERT | 15 | Replaces the top with ~ of it |
BINARY_MATRIX_MULTIPLY | 16 | Pops two values, pushes a @ b |
INPLACE_MATRIX_MULTIPLY | 17 | Pops two values, pushes a @= b |
BINARY_POWER | 19 | Pops two values, pushes a ** b |
BINARY_MULTIPLY | 20 | Pops two values, pushes a * b |
BINARY_MODULO | 22 | Pops two values, pushes a % b |
BINARY_ADD | 23 | Pops two values, pushes a + b |
BINARY_SUBTRACT | 24 | Pops two values, pushes a - b |
BINARY_SUBSCR | 25 | Pops key and container; pushes container[key] |
BINARY_FLOOR_DIVIDE | 26 | Pops two values, pushes a // b |
BINARY_TRUE_DIVIDE | 27 | Pops two values, pushes a / b |
INPLACE_FLOOR_DIVIDE | 28 | Pops two values, pushes a //= b |
INPLACE_TRUE_DIVIDE | 29 | Pops two values, pushes a /= b |
GET_LEN | 30 | Pushes len() of the top |
MATCH_MAPPING | 31 | Pushes whether the top is a mapping |
MATCH_SEQUENCE | 32 | Pushes whether the top is a sequence and not text or bytes |
MATCH_KEYS | 33 | The top is a tuple of keys and the item below a subject. Pushes the values and True, or None and False |
COPY_DICT_WITHOUT_KEYS | 34 | Replaces the tuple of keys on top with a dictionary of the subject’s other items |
WITH_EXCEPT_START | 49 | Calls __exit__, at stack position 7, with the exception on the top three |
GET_AITER | 50 | Replaces the top with __aiter__() of it |
GET_ANEXT | 51 | Pushes an awaitable of __anext__() of the top |
BEFORE_ASYNC_WITH | 52 | Takes __aenter__ and __aexit__ from the top; pushes __aexit__ and the result of __aenter__() |
END_ASYNC_FOR | 54 | Ends an async for. If the exception is StopAsyncIteration, restores the exception state; otherwise re-raises |
INPLACE_ADD | 55 | Pops two values, pushes a += b |
INPLACE_SUBTRACT | 56 | Pops two values, pushes a -= b |
INPLACE_MULTIPLY | 57 | Pops two values, pushes a *= b |
INPLACE_MODULO | 59 | Pops two values, pushes a %= b |
STORE_SUBSCR | 60 | Pops key, container and value; container[key] = value |
DELETE_SUBSCR | 61 | Pops key and container; del container[key] |
BINARY_LSHIFT | 62 | Pops two values, pushes a << b |
BINARY_RSHIFT | 63 | Pops two values, pushes a >> b |
BINARY_AND | 64 | Pops two values, pushes a & b |
BINARY_XOR | 65 | Pops two values, pushes a ^ b |
BINARY_OR | 66 | Pops two values, pushes a | b |
INPLACE_POWER | 67 | Pops two values, pushes a **= b |
GET_ITER | 68 | Replaces the top with iter() of it |
GET_YIELD_FROM_ITER | 69 | Leaves a generator or coroutine on top unchanged; replaces anything else with iter() of it |
PRINT_EXPR | 70 | Pops the top and prints it, in interactive mode |
LOAD_BUILD_CLASS | 71 | Pushes builtins.__build_class__() |
YIELD_FROM | 72 | Pops the top and delegates to it as a sub-iterator |
GET_AWAITABLE | 73 | Replaces the top with an awaitable |
LOAD_ASSERTION_ERROR | 74 | Pushes AssertionError |
INPLACE_LSHIFT | 75 | Pops two values, pushes a <<= b |
INPLACE_RSHIFT | 76 | Pops two values, pushes a >>= b |
INPLACE_AND | 77 | Pops two values, pushes a &= b |
INPLACE_XOR | 78 | Pops two values, pushes a ^= b |
INPLACE_OR | 79 | Pops two values, pushes a |= b |
LIST_TO_TUPLE | 82 | Pops a list, pushes a tuple of the same items |
RETURN_VALUE | 83 | Returns the top of the stack |
IMPORT_STAR | 84 | Pops a module and loads its public names into the local namespace |
SETUP_ANNOTATIONS | 85 | Makes __annotations__ an empty dictionary in the locals, if it is missing |
YIELD_VALUE | 86 | Pops the top and yields it |
POP_BLOCK | 87 | Removes one block from the block stack |
POP_EXCEPT | 89 | Removes one exception-handler block and restores the exception state from the last three values popped |
STORE_NAME | 90 | Pops a value into a name. arg is a name index |
DELETE_NAME | 91 | Deletes a name. arg is a name index |
UNPACK_SEQUENCE | 92 | Replaces the top with its arg items, pushed right to left |
FOR_ITER | 93 | Calls __next__() on the iterator on top; pushes the value, or pops the iterator and jumps forward by arg |
UNPACK_EX | 94 | Unpacks the top with a starred target. The low byte of arg is the count before the list, and the high byte the count after it |
STORE_ATTR | 95 | Pops an object and a value; sets the attribute. arg is a name index |
DELETE_ATTR | 96 | Pops an object; del obj.name. arg is a name index |
STORE_GLOBAL | 97 | Pops a value into a global. arg is a name index |
DELETE_GLOBAL | 98 | Deletes a global. arg is a name index |
ROT_N | 99 | Moves the top item down to position arg |
LOAD_CONST | 100 | Pushes a constant. arg is a constant index |
LOAD_NAME | 101 | Pushes a name. arg is a name index |
BUILD_TUPLE | 102 | Pops arg items, pushes a tuple |
BUILD_LIST | 103 | Pops arg items, pushes a list |
BUILD_SET | 104 | Pops arg items, pushes a set |
BUILD_MAP | 105 | Pops 2 × arg items, pushes a dictionary of arg entries |
LOAD_ATTR | 106 | Replaces the top with an attribute of it. arg is a name index |
COMPARE_OP | 107 | Pops two values, pushes the comparison. arg is the index |
IMPORT_NAME | 108 | Pops fromlist and level; pushes the imported module. arg is a name index |
IMPORT_FROM | 109 | Pushes an attribute of the module on top. arg is a name index |
JUMP_FORWARD | 110 | Jumps forward by arg |
JUMP_IF_FALSE_OR_POP | 111 | If the top is false, jumps to arg and keeps it; otherwise pops it |
JUMP_IF_TRUE_OR_POP | 112 | If the top is true, jumps to arg and keeps it; otherwise pops it |
JUMP_ABSOLUTE | 113 | Jumps to arg |
POP_JUMP_IF_FALSE | 114 | Pops the top; if false, jumps to arg |
POP_JUMP_IF_TRUE | 115 | Pops the top; if true, jumps to arg |
LOAD_GLOBAL | 116 | Pushes a global. arg is a name index |
IS_OP | 117 | Pops two values, pushes is, or is not if arg is 1 |
CONTAINS_OP | 118 | Pops two values, pushes in, or not in if arg is 1 |
RERAISE | 119 | Re-raises the exception on top. If arg is nonzero, restores f_lasti |
JUMP_IF_NOT_EXC_MATCH | 121 | Pops two values; jumps to arg if the second does not match the exception type on top |
SETUP_FINALLY | 122 | Pushes a try block; arg points to the first handler |
LOAD_FAST | 124 | Pushes the local in slot arg |
STORE_FAST | 125 | Pops a value into the local in slot arg |
DELETE_FAST | 126 | Deletes the local in slot arg |
GEN_START | 129 | Pops the top. arg is 0 for a generator, 1 for a coroutine, 2 for an async generator |
RAISE_VARARGS | 130 | Raises: arg 0 re-raises, 1 raises the top, 2 raises the item below the top with the top as its cause |
CALL_FUNCTION | 131 | Calls with arg positional arguments; pops the arguments and callable, pushes the result |
MAKE_FUNCTION | 132 | Pushes a function made from the code and qualified name on top. arg holds flags for extra items below them |
BUILD_SLICE | 133 | arg is 2 or 3; pops the bounds, and a step if 3, and pushes a slice |
LOAD_CLOSURE | 135 | Pushes the cell in cell number arg |
LOAD_DEREF | 136 | Pushes the object in the cell in cell number arg |
STORE_DEREF | 137 | Pops a value into the cell in cell number arg |
DELETE_DEREF | 138 | Empties the cell in cell number arg |
CALL_FUNCTION_KW | 141 | Calls with arg arguments; a tuple of keyword names is on top |
CALL_FUNCTION_EX | 142 | Calls with an iterable of positional arguments; arg bit 0 means a keyword mapping is on top |
SETUP_WITH | 143 | Pushes __exit__, calls __enter__(), pushes a finally block targeting arg, then pushes the result |
EXTENDED_ARG | 144 | Gives the next instruction’s argument more high bits |
LIST_APPEND | 145 | Pops an item and appends it to the list arg places down |
SET_ADD | 146 | Pops an item and adds it to the set arg places down |
MAP_ADD | 147 | Pops a value and key, stores them in the dictionary arg places down |
LOAD_CLASSDEREF | 148 | Like LOAD_DEREF, but checks the locals dictionary first. Used in class bodies |
MATCH_CLASS | 152 | Matches a class pattern; arg is the number of positional sub-patterns. Sets the top to whether it matched |
SETUP_ASYNC_WITH | 154 | Part of async with; arg is a jump target |
FORMAT_VALUE | 155 | f-string formatting. arg bits 0–1 pick the conversion (0 none, 1 str, 2 repr, 3 ascii); bit 2 means a format spec is on the stack |
BUILD_CONST_KEY_MAP | 156 | Pops a tuple of keys, then arg values; pushes a dictionary |
BUILD_STRING | 157 | Pops arg strings and pushes them joined |
LOAD_METHOD | 160 | Pops an object; pushes the unbound method and the object, or NULL and the attribute. arg is a name index |
CALL_METHOD | 161 | Calls a method with arg positional arguments, using what LOAD_METHOD pushed |
LIST_EXTEND | 162 | Pops an iterable and extends the list arg places down |
SET_UPDATE | 163 | Pops an iterable and adds its items to the set arg places down |
DICT_MERGE | 164 | Like DICT_UPDATE, but raises an error on a duplicate key |
DICT_UPDATE | 165 | Pops a mapping and adds it to the dictionary arg places down |
Comparison operators
COMPARE_OP’s argument indexes this list.
| arg | Operator |
|---|---|
| 0 | < |
| 1 | <= |
| 2 | == |
| 3 | != |
| 4 | > |
| 5 | >= |
MAKE_FUNCTION flags
Each set bit means one extra item is on the stack below the code object and the qualified name, in this order from the bottom.
| Bit | Item |
|---|---|
| 0x01 | Tuple of defaults for positional parameters |
| 0x02 | Dictionary of keyword-only defaults |
| 0x04 | Tuple of annotation strings |
| 0x08 | Tuple of cells for a closure |