Python 位元組碼反彙編器


Python 標準庫中的 dis 模組提供了許多有用的函式,用於透過反彙編將 Python 位元組碼分析成人類可讀的形式。這有助於執行最佳化。位元組碼是直譯器的特定版本實現細節。

dis() 函式

dis() 函式生成任何 Python 程式碼源(例如模組、類、方法、函式或程式碼物件)的反彙編表示形式。

>>> def hello():
print ("hello world")
>>> import dis
>>> dis.dis(hello)
2    0 LOAD_GLOBAL 0 (print)
     3 LOAD_CONST 1 ('hello world')
     6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
     9 POP_TOP
     10 LOAD_CONST 0 (None)
     13 RETURN_VALUE

位元組碼分析 API 在 Bytecode 類中定義。它的建構函式返回 Bytecode 物件,該物件具有以下方法來分析位元組碼

Bytecode()

這是建構函式。分析對應於函式、生成器、方法、原始碼字串或程式碼物件的位元組碼。這是許多函式的便捷包裝器

>>> string=dis.Bytecode(hello)
>>> for x in string:
      print (x)
Instruction(opname = 'LOAD_GLOBAL', opcode = 116, arg = 0, argval = 'print', argrepr = 'print', offset = 0, starts_line = 2, is_jump_target = False)
Instruction(opname = 'LOAD_CONST', opcode = 100, arg = 1, argval = 'hello world', argrepr = "'hello world'", offset = 3, starts_line = None, is_jump_target = False)
Instruction(opname = 'CALL_FUNCTION', opcode = 131, arg = 1, argval = 1, argrepr = '1 positional, 0 keyword pair', offset = 6, starts_line = None, is_jump_target = False)
Instruction(opname = 'POP_TOP', opcode = 1, arg = None, argval = None, argrepr = '', offset = 9, starts_line = None, is_jump_target = False)
Instruction(opname = 'LOAD_CONST', opcode = 100, arg = 0, argval = None, argrepr = 'None', offset = 10, starts_line = None, is_jump_target = False)
Instruction(opname = 'RETURN_VALUE', opcode = 83, arg = None, argval = None, argrepr = '', offset = 13, starts_line = None, is_jump_target = False)

code_info()

此函式返回 Python 程式碼物件的資訊。

>>> dis.code_info(hello)
"Name: hello\nFilename: <pyshell#2>\nArgument count: 0\nKw-only arguments: 0\nNumber of locals: 0\nStack size: 2\nFlags: OPTIMIZED, NEWLOCALS, NOFREE\nConstants:\n 0: None\n 1: 'hello world'\nNames:\n 0: print"

show_code()

此函式列印 Python 模組、函式或類的詳細程式碼資訊。

>>> dis.show_code(hello)
Name: hello
Filename: <pyshell#2>
Argument count: 0
Kw-only arguments: 0
Number of locals: 0
Stack size: 2
Flags: OPTIMIZED, NEWLOCALS, NOFREE
Constants:
   0: None
   1: 'hello world'
Names:
   0: print

disassemble()

此函式反彙編程式碼物件,並將輸出分成以下幾列:

  • 每行的第一條指令的行號

  • 當前指令,用 --> 表示

  • 帶標籤的指令,用 >> 表示

  • 指令的地址

  • 操作碼名稱

  • 操作引數,以及

  • 括號中對引數的解釋。

>>> codeInString = 'a = 5\nb = 6\nsum = a + b \ nprint("sum = ",sum)'
>>> codeObejct = compile(codeInString, 'sumstring', 'exec')
>>> dis.disassemble(codeObejct)

輸出

1    0 LOAD_CONST 0 (5)
     3 STORE_NAME 0 (a)
2    6 LOAD_CONST 1 (6)
     9 STORE_NAME 1 (b)
3    12 LOAD_NAME 0 (a)
     15 LOAD_NAME 1 (b)
     18 BINARY_ADD
     19 STORE_NAME 2 (sum)
4    22 LOAD_NAME 3 (print)
     25 LOAD_CONST 2 ('sum =')
     28 LOAD_NAME 2 (sum)
     31 CALL_FUNCTION 2 (2 positional, 0 keyword pair)
     34 POP_TOP
     35 LOAD_CONST 3 (None)
     38 RETURN_VALUE

get_instructions()

此函式返回提供的函式、方法、原始碼字串或程式碼物件中指令的迭代器。迭代器生成一系列 Instruction 命名元組,提供提供的程式碼中每個操作的詳細資訊。

>>> it=dis.get_instructions(code)
>>> for i in it:
      print (i)
Instruction(opname = 'LOAD_CONST', opcode = 100, arg = 0, argval = <code object hello at 0x02A9BA70,
   file "<disassembly>", line 2>, argrepr = '<code object hello at 0x02A9BA70, file "<disassembly>",
   line 2>', offset = 0, starts_line = 2, is_jump_target = False)
Instruction(opname = 'LOAD_CONST', opcode = 100, arg = 1, argval = 'hello', argrepr = "'hello'", offset = 3, starts_line = None, is_jump_target = False)
Instruction(opname = 'MAKE_FUNCTION', opcode = 132, arg = 0, argval = 0, argrepr = '', offset = 6, starts_line = None, is_jump_target = False)
Instruction(opname = 'STORE_NAME', opcode = 90, arg = 0, argval = 'hello', argrepr = 'hello', offset = 9, starts_line = None, is_jump_target = False)
Instruction(opname = 'LOAD_CONST', opcode = 100, arg = 2, argval = None, argrepr = 'None', offset = 12, starts_line = None, is_jump_target = False)
Instruction(opname = 'RETURN_VALUE', opcode = 83, arg = None, argval = None, argrepr = '', offset = 15, starts_line = None, is_jump_target = False)

函式資訊採用元組狀物件的格式,其引數如下所示:

Instruction
位元組碼操作的詳細資訊
opcode
操作的數字程式碼,對應於下面列出的操作碼值和 Opcode 集合中的位元組碼值。
opname
操作的人類可讀名稱
arg
操作的數字引數(如果有),否則為 None
argval
已解析的 arg 值(如果已知),否則與 arg 相同
argrepr
操作引數的人類可讀描述
offset
操作在位元組碼序列中的起始索引
starts_line
此操作碼開始的行(如果有),否則為 None
is_jump_target
如果其他程式碼跳轉到此處,則為 True,否則為 False

更新於: 2020年6月27日

2K+ 閱讀量

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.