dis — Python 字节码的反汇编程序

源代码: Lib/dis.py


The dis 模块支持分析 CPython bytecode 通过反汇编它。此模块接受作为输入的 CPython 字节码的定义在文件 Include/opcode.h 并用于编译器和解释器。

CPython 实现细节: 字节码是 CPython 解释器的实现细节。不保证字节码不会在 Python 版本之间被添加、移除或改变。不应考虑使用此模块来跨 Python VM 或 Python 发行。

3.6 版改变: 每条指令使用 2 字节。先前,字节数因指令而异。

3.10 版改变: The argument of jump, exception handling and loop instructions is now the instruction offset rather than the byte offset.

3.11 版改变: Some instructions are accompanied by one or more inline cache entries, which take the form of CACHE instructions. These instructions are hidden by default, but can be shown by passing show_caches=True to any dis utility. Furthermore, the interpreter now adapts the bytecode to specialize it for different runtime conditions. The adaptive bytecode can be shown by passing adaptive=True .

3.12 版改变: The argument of a jump is the offset of the target instruction relative to the instruction that appears immediately after the jump instruction’s CACHE 条目。

As a consequence, the presence of the CACHE instructions is transparent for forward jumps but needs to be taken into account when reasoning about backward jumps.

Changed in version 3.13: The output shows logical labels rather than instruction offsets for jump targets and exception handlers. The -O 命令行选项和 show_offsets argument were added.