我正在创建一个工具来分析用户定义函数定义中使用的代码,例如:
def help_fun(a, b): return a * b
def test1(numbers):
r1, r2 = np.multiply.reduce(numbers), sum(numbers)/len(numbers)
r = math.sin(help_fun(r1, r2))
return math.sqrt(r)
但是,我理解如何解释
dis.dis(test1)
的结果:
3 0 LOAD_GLOBAL 0 (np)
2 LOAD_ATTR 1 (multiply)
4 LOAD_METHOD 2 (reduce)
6 LOAD_FAST 0 (numbers)
8 CALL_METHOD 1
10 LOAD_GLOBAL 3 (sum)
...
5 46 LOAD_GLOBAL 5 (math)
48 LOAD_METHOD 8 (sqrt)
50 LOAD_FAST 3 (r)
52 CALL_METHOD 1
54 RETURN_VALUE
我的预期输出是:
{0: {'functions': ['sum', 'len', 'help_fun'],
'methods': ['np.multiply.reduce', 'math.sin', 'math.sqrt']}} #frame 0: scope of test1
为了收集函数和方法名称,我为反汇编器堆栈的内容实现了一个包装器,并使用特定的方式从堆栈存档中提取这些名称。
import types
import dis
def get_frames(code):
'''given <function>.__code__ instance, iterate each code frame
>>> [type(x) for x in get_frames(test1.__code__)]
[code]
'''
yield code
for c in code.co_consts:
if isinstance(c, types.CodeType):
yield from get_frames(c)
break
def get_calls(instructions, stack):
'''get called functions and methods in CALL_FUNCTION and CALL_METHOD opnames'''
functions, methods = [], []
for idx, instr in enumerate(instructions):
if instr.opname == 'CALL_FUNCTION':
functions.append(stack[idx - 1][- 1 - instr.arg])
elif instr.opname == 'CALL_METHOD':
methods.append(stack[idx - 1][- 1 - instr.arg])
return {'functions': functions, 'methods': methods}
def get_stack(instructions):
'''Wrapper for stack contents'''
stack = []
for n in instructions:
if n.opname in ('LOAD_FAST', 'LOAD_GLOBAL', 'LOAD_CONST'):
stack.append(n.argrepr) #global var
elif n.opname in ('LOAD_METHOD', 'LOAD_ATTR'):
stack[-1] = f'{stack[-1]}.{n.argrepr}'
elif n.opname in ('CALL_FUNCTION', 'CALL_METHOD'):
args = stack[-n.arg:]
del stack[-n.arg:]
stack[-1] = f'{stack[-1]}({", ".join(args)})'
elif n.opname == 'BINARY_TRUE_DIVIDE':
stack[-2:] = [' / '.join(stack[-2:])]
elif n.opname == 'STORE_FAST':
del stack[-1]
elif n.opname == 'ROT_TWO':
stack[-1], stack[-2] = stack[-2], stack[-1]
elif n.opname == 'GET_ITER':
stack[-1] = f'iter({stack[-1]})'
yield stack.copy()
code = list(get_frames(test1.__code__))
out = dict()
for i, c in enumerate(code):
instructions = dis.Bytecode(c)
stack = list(get_stack(instructions))
out[i] = get_calls(instructions, stack)
out
>>> {0: {'functions': ['sum', 'len', 'help_fun'], 'methods': ['np.multiply.reduce', 'math.sin', 'math.sqrt']}}
在我的方法中,函数和方法的名称是从表的
stack
列中提取的:
| line | opname | arg | argrepr | stack |
|--------|--------------------|-------|-----------|----------------------------------------------------------|
| 3 | LOAD_GLOBAL | 0 | np | np |
| | LOAD_ATTR | 1 | multiply | np.multiply |
| | LOAD_METHOD | 2 | reduce | np.multiply.reduce |
| | LOAD_FAST | 0 | numbers | np.multiply.reduce, numbers |
| | CALL_METHOD | 1 | | np.multiply.reduce(numbers) |
| | LOAD_GLOBAL | 3 | sum | np.multiply.reduce(numbers), sum |
| | LOAD_FAST | 0 | numbers | np.multiply.reduce(numbers), sum, numbers |
| | CALL_FUNCTION | 1 | | np.multiply.reduce(numbers), sum(numbers) |
| | LOAD_GLOBAL | 4 | len | np.multiply.reduce(numbers), sum(numbers), len |
| | LOAD_FAST | 0 | numbers | np.multiply.reduce(numbers), sum(numbers), len, numbers |
| | CALL_FUNCTION | 1 | | np.multiply.reduce(numbers), sum(numbers), len(numbers) |
| | BINARY_TRUE_DIVIDE | | | np.multiply.reduce(numbers), sum(numbers) / len(numbers) |
| | ROT_TWO | | | sum(numbers) / len(numbers), np.multiply.reduce(numbers) |
| | STORE_FAST | 1 | r1 | sum(numbers) / len(numbers) |
| | STORE_FAST | 2 | r2 | |
| 4 | LOAD_GLOBAL | 5 | math | math |
| | LOAD_METHOD | 6 | sin | math.sin |
| | LOAD_GLOBAL | 7 | help_fun | math.sin, help_fun |
| | LOAD_FAST | 1 | r1 | math.sin, help_fun, r1 |
| | LOAD_FAST | 2 | r2 | math.sin, help_fun, r1, r2 |
| | CALL_FUNCTION | 2 | | math.sin, help_fun(r1, r2) |
| | CALL_METHOD | 1 | | math.sin(help_fun(r1, r2)) |
| | STORE_FAST | 3 | r | |
| 5 | LOAD_GLOBAL | 5 | math | math |
| | LOAD_METHOD | 8 | sqrt | math.sqrt |
| | LOAD_FAST | 3 | r | math.sqrt, r |
| | CALL_METHOD | 1 | | math.sqrt(r) |
| | RETURN_VALUE | | | math.sqrt(r) |
但是,如果我的说明中包含其他类型的操作名称,事情会变得更加复杂。例如,如果使用任何列表理解,我不确定堆栈的行为。获取方法和函数的名称可能会显示不正确的结果:
(
<listcomp>
不是一个函数,而是我的包装器)
def test2(x):
return [[math.sqrt(m) for m in list(n)] for n in x]
预期输出:
{0: {'functions': [], 'methods': []}, #frame 0: scope of test2
1: {'functions': ['list'], 'methods': [], #frame 1: scope of <listcomp>
2: {'functions': [], 'methods': ['math.sqrt']}} #frame 2: scope of <listcomp>
当前代码的输出:
{0: {'functions': ["'test6.<locals>.<listcomp>'"], 'methods': []},
1: {'functions': ['list', "'test6.<locals>.<listcomp>.<listcomp>'"], 'methods': []},
2: {'functions': [], 'methods': ['math.sqrt']}}
有时它也可能崩溃,因为我不确定堆栈发生了什么:
def test3(x,y):
return [pow(i,j) for i,j in zip(x,y)]
预期输出:
{0: {'functions': ['zip'], 'methods': []},
1: {'functions': ['pow'], 'methods': []}}
第二个
STORE_FAST
命令尝试从空堆栈中弹出项目后崩溃。第二个范围的指令如下所示:
| line | opname | arg | argrepr | stack |
|--------|-----------------|-------|-----------|---------|
| 104 | BUILD_LIST | 0 | | |
| | LOAD_FAST | 0 | .0 | .0 |
| | FOR_ITER | 18 | to 24 | .0 |
| | UNPACK_SEQUENCE | 2 | | .0 |
| | STORE_FAST | 1 | i | |
| | STORE_FAST | 2 | j | ??? | ### stuck here
| | LOAD_GLOBAL | 0 | pow | ??? |
| | LOAD_FAST | 1 | i | ??? |
| | LOAD_FAST | 2 | j | ??? |
| | CALL_FUNCTION | 2 | | ??? |
| | LIST_APPEND | 2 | | ??? |
| | JUMP_ABSOLUTE | 4 | | ??? |
| | RETURN_VALUE | | | ??? |
有没有更简单的方法来获取调用者内部使用的方法和函数的名称?有没有更好的方法来获取堆栈存档?我知道,我目前对
get_stack
的实现很差,我正在寻找不同的方法或更好的堆栈控制文档。
备注
ls.append
如果您希望代码在 Python 版本之间更具可移植性,您应该考虑使用 ast。尽管 AST 确实会在不同版本之间发生变化,但它通常会以导致易于理解的错误的方式发生变化。
要修复您的代码,您将需要实现更多操作。我通过忽略大多数事情并仅应用它们的堆栈效应(从堆栈中放置或删除的元素数量)来做到这一点。
import dis
import inspect
def iterate_stack(instructions):
stack = []
called = []
for instruction in instructions:
old_stack_len = len(stack)
if instruction.opname == "ROT_TWO":
stack[-1], stack[-2] = stack[-2], stack[-1]
elif instruction.opname == "ROT_THREE":
stack[-1], stack[-2], stack[-3] = stack[-2], stack[-3], stack[-1]
elif instruction.opname == "ROT_FOUR":
stack[-1], stack[-2], stack[-3], stack[-4] = stack[-2], stack[-3], stack[-4], stack[-1]
elif instruction.opname == "DUP_TOP":
stack.append(stack[-1])
elif instruction.opname == "DUP_TOP_TWO":
stack.extend(stack[-2:])
elif instruction.opname == "LOAD_ASSERTION_ERROR":
stack.append("AssertionError")
elif instruction.opname == "LOAD_NAME":
stack.append(instruction.argrepr)
elif instruction.opname == "LOAD_ATTR" or instruction.opname == "LOAD_METHOD":
if stack[-1]:
stack[-1] = stack[-1] + "." + instruction.argrepr
elif instruction.opname == "LOAD_GLOBAL":
stack.append(instruction.argrepr)
elif instruction.opname == "LOAD_FAST" or instruction.opname == "LOAD_CLOSURE" or instruction.opname == "LOAD_DEREF" or instruction.opname == "LOAD_CLASSDEREF":
if inspect.iscode(instruction.argval):
stack.append(None)
else:
stack.append(instruction.argrepr)
elif instruction.opname == "CALL_FUNCTION":
args = stack[-instruction.arg:]
del stack[-instruction.arg:]
if stack[-1] is not None:
called.append(f'{stack[-1]}({", ".join(args)})')
stack.pop()
elif instruction.opname == "CALL_FUNCTION_KW":
# TODO get the names of keyword arguments
called.append(stack[-1 - instruction.arg])
del stack[-1 - instruction.arg:]
stack.append(None)
elif instruction.opname == "CALL_FUNCTION_EX":
# TODO get the arguments
if instruction.arg & 0x01:
stack.pop()
stack.pop()
called.append(stack.pop())
elif instruction.opname == "CALL_METHOD":
# TODO get the arguments
called.append(stack[-2 - instruction.arg])
del stack[-2 - instruction.arg:]
stack.append(None)
elif instruction.opname == "ROT_N":
tos = stack.pop()
stack.insert(1 - instruction.arg, tos)
stack_effect = dis.stack_effect(instruction.opcode, instruction.arg)
while old_stack_len + stack_effect < len(stack):
stack.pop()
while old_stack_len + stack_effect > len(stack):
stack.append(None)
return called
def get_frames(code):
yield code
for c in code.co_consts:
if inspect.iscode(c):
yield from get_frames(c)
def help_fun(a, b):
return a * b
def test1(numbers):
r1, r2 = np.multiply.reduce(numbers), sum(numbers)/len(numbers)
r = math.sin(help_fun(r1, r2))
return math.sqrt(r)
def test2(x):
return [[math.sqrt(m) for m in list(n)] for n in x]
def test3(x,y):
return [pow(i,j) for i,j in zip(x,y)]
def main():
code = list(get_frames(test1.__code__))
out = dict()
for i, c in enumerate(code):
instructions = dis.get_instructions(c)
out[i] = iterate_stack(instructions)
print(out)
main()
这给出了所有三个示例的预期结果,在 Linux 上的 Python 3.10.8 (main, Nov 14 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-2)] 下。它不会在Python 3.11上工作,因为字节码会改变每个版本。
我真的建议不要使用这段代码,因为它总是会崩溃。您可能应该构建一个 pylint 检查器,或者至少使用像 astroid 这样的库。