howto dis
且任容枯 Lv4

howtouse dis

代码方式

1
2
3
4
5
6
import dis

def main():
print("Hello, World!")

dis.dis(main)

命令行方式

1
python -m dis main.py

dis 字节码指令

  • NOP: 无操作
  • POP_TOP: 弹出栈顶元素
  • END_FOR: 结束循环
  • END_SEND: del stack[-2] 用于gengerator退出
  • COPY(i): 复制栈顶元素到栈顶i个元素的位置
    1
    2
    assert i > 0
    STACK.append(STACK[-1])
  • SWAP(i): 交换栈顶元素和栈顶i个元素的位置
    1
    STACK[-i], STACK[-1] = STACK[-1], STACK[-i]
  • CACHE: 缓存 show_caches=True 才会显示
  • UNARY_NEGATIVE: STACK[-1] = -STACK[-1]
  • UNARY_NOT: STACK[-1] = not STACK[-1]
  • UNARY_INVERT: STACK[-1] = ~STACK[-1]
  • GET_ITER: 从栈顶元素获取迭代器 STACK[-1] = iter(STACK[-1])
  • GET_YIELD_FROM_ITER: 从栈顶元素获取迭代器 如果stack[-1]是迭代器 则返回stack[-1] 否则返回iter(stack[-1])
  • BINARY_OP(OP):
    1
    2
    3
    rhs = STACK.pop()
    lhs = STACK.pop()
    STACK.append(lhs OP rhs)
  • BINARY_SUBSCR:
    1
    2
    3
    key = STACK.pop()
    container = STACK.pop()
    STACK.append(container[key])
  • STORE_SUBSCR:
    1
    2
    3
    4
    key = STACK.pop()
    container = STACK.pop()
    value = STACK.pop()
    container[key] = value
  • DELETE_SUBSCR:
    1
    2
    3
    key = STACK.pop()
    container = STACK.pop()
    del container[key]
  • BINARY_SLICE:
    1
    2
    3
    4
    end = STACK.pop()
    start = STACK.pop()
    container = STACK.pop()
    STACK.append(container[start:end])
  • STORE_SLICE:
    1
    2
    3
    4
    5
    end = STACK.pop()
    start = STACK.pop()
    container = STACK.pop()
    value = STACK.pop()
    container[start:end] = value
  • GET_AWAITABLE: STACK[-1] = get_awaitable(STACK[-1])
  • GET_AITER: STACK[-1] = STACK[-1].aiter()
  • GET_ANEXT: STACK.append(STACK[-1].anext())
  • END_ASYNC_FOR: 结束一个async for循环 stack[-2] 放async iterable stack[-1] 放exception 然后pop 俩者
  • CLEANUP_THROW: Handles an exception raised during a throw() or close() call through the current frame. If STACK[-1] is an instance of StopIteration, pop three values from the stack and push its value member. Otherwise, re-raise STACK[-1].
  • BEFORE_ASYNC_WITH: STACK.extend((aexit, aenter()))
  • SET_ADD(i):
    1
    2
    item = STACK.pop()
    set.add(STACK[-i], item)
  • LIST_APPEND(i):
    1
    2
    item = STACK.pop()
    list.append(STACK[-i], item)
  • MAP_ADD(i):
    1
    2
    3
    item = STACK.pop()
    key = STACK.pop()
    dict.__setitem__(STACK[-i], key, item)
  • RETURN_VALUE: return STACK[-1] to the caller of function
  • RETURN_CONST: return co_consts[consti] to the caller of function
  • YIELD_VALUE: yield STACK.pop() from a generator
  • SETUP_ANNOTATIONS: Checks whether annotations is defined in locals(), if not it is set up to an empty dict. This opcode is only emitted if a class or module body contains variable annotations statically.
  • POP_EXCEPT: Pops a value from the stack, which is used to restore the exception state.
  • RERAISE: 重新抛出异常
  • PUSH_EXC_INFO: 把异常信息压入栈
  • CHECK_EXC_MATCH: Tests whether the STACK[-2] is an exception matching STACK[-1]. Pops STACK[-1] and pushes the boolean result of the test.
  • CHECK_EG_MATCH: Applies split(STACK[-1]) on the exception group representing STACK[-2]
  • WITH_EXCEPT_START: Calls the function in position 4 on the stack with arguments (type, val, tb) representing the exception at the top of the stack. Used to implement the call context_manager.exit(*exc_info()) when an exception has occurred in a with statement
  • LOAD_ASSERTION_ERROR: Pushes AssertionError onto the stack
  • LOAD_BUILD_CLASS: Pushes builtins.build_class() onto the stack. It is later called to construct a class.
  • BEFORE_WITH: This opcode performs several operations before a with block starts. First, it loads exit() from the context manager and pushes it onto the stack for later use by WITH_EXCEPT_START. Then, enter() is called. Finally, the result of calling the enter() method is pushed onto the stack
  • GET_LEN: STACK.append(len(STACK[-1]))
  • MATCH_MAPPING: push isinstand(STACK[-1],collections.abc.Mapping)
  • MATCH_SEQUENCE: push isinstand(STACK[-1],collections.abc.Sequence) and not isinstand(STACK[-1], str/bytes/bytearray)
  • MATCH_KEYS: STACK[-1] is a tuple of mapping keys, and STACK[-2] is the match subject. If STACK[-2] contains all of the keys in STACK[-1], push a tuple containing the corresponding values. Otherwise, push None.
  • STORE_NAME(namei): name = STACK.pop() namei 是named的index
  • DELETE_NAME(namei): del name
  • UNPACK_SEQUENCE(count):
    1
    2
    assert(len(STACK[-1]) == count)
    STACK.extend(STACK.pop()[:-count-1:-1])
  • UNPACK_EX(counts): ???
  • STORE_ATTR(namei):
    1
    2
    3
    obj = STACK.pop()
    value = STACK.pop()
    obj.name = value
  • DELETE_ATTR(namei):
    1
    2
    obj = STACK.pop()
    del obj.name
  • STORE_GLOBAL(namei): like STORE_NAME but global
  • DELETE_GLOBAL(namei): like DELETE_NAME but global
  • LOAD_CONST(consti): STACK.append(co_consts[consti])
  • LOAD_NAME(namei): STACK.append(name)
  • LOAD_LOCALS: Pushes a reference to the locals dictionary onto the stack
  • LOAD_FROM_DICT_OR_GLOBAL(i): ???
  • BUILD_TUPLE(count):
    1
    2
    3
    4
    5
    6
    if count == 0:
    value = ()
    else:
    value = tuple(STACK[-count:])
    STACK = STACK[:-count]
    STACK.append(value)
  • BUILD_LIST(count): like BUILD_TUPLE but list
  • BUILD_SET(count): like BUILD_TUPLE but set
  • BUILD_MAP(count): Pushes a new dictionary object onto the stack. Pops 2 * count items so that the dictionary holds count entries: {…, STACK[-4]: STACK[-3], STACK[-2]: STACK[-1]}
  • BUILD_CONST_KEY_MAP(count): like BUILD_TUPLE but dict
  • BUILD_STRING(count): Concatenates count strings from the stack and pushes the resulting string onto the stack.
  • LIST_EXTEND(i):
    1
    2
    seq = STACK.pop()
    list.extend(STACK[-i], seq)
  • SET_UPDATE(i)
    1
    2
    seq = STACK.pop()
    set.update(STACK[-i], seq)
  • SET_UPDATE(i):
    1
    2
    map = STACK.pop()
    dict.update(STACK[-i], map)
  • DICT_MERGE(i): Like DICT_UPDATE but raises an exception for duplicate keys.
  • LOAD_ATTR(namei): If the low bit of namei is not set, this replaces STACK[-1] with getattr(STACK[-1], co_names[namei>>1]).

If the low bit of namei is set, this will attempt to load a method named co_names[namei>>1] from the STACK[-1] object. STACK[-1] is popped. This bytecode distinguishes two cases: if STACK[-1] has a method with the correct name, the bytecode pushes the unbound method and STACK[-1]. STACK[-1] will be used as the first argument (self) by CALL when calling the unbound method. Otherwise, NULL and the object returned by the attribute lookup are pushed.

  • LOAD_SUPER_ATTR(namei): ??
  • COMPARE_OP(opname): Performs a Boolean operation. The operation name can be found in cmp_op[opname >> 4].
  • IS_OP(invert): Performs is comparison, or is not if invert is 1.
  • CONTAINS_OP(invert): Performs in comparison, or not in if invert is 1.