我正在尝试编写一个装饰器,通过装饰器向一个函数添加详细的日志记录(一种方法也很好,但我还没有尝试过)。这背后的动机是将一行add_logs装饰器调用修补到生产中的框中比添加100个调试行更容易(也更安全)。
例如:
def hey(name):
print("Hi " + name)
t = 1 + 1
if t > 6:
t = t + 1
print("I was bigger")
else:
print("I was not.")
print("t = ", t)
return t
我想制作一个装饰器,将其转换为执行此操作的代码:
def hey(name):
print("line 1")
print("Hi " + name)
print("line 2")
t = 1 + 1
print("line 3")
if t > 6:
print("line 4")
t = t + 1
print("line 5")
print("I was bigger")
else:
print("line 6")
print("I was not.")
print("line 7")
print("t = ", t)
print("line 8")
return t
到目前为止我得到了什么:
import inspect, ast
import itertools
import imp
def log_maker():
line_num = 1
while True:
yield ast.parse('print("line {line_num}")'.format(line_num=line_num)).body[0]
line_num = line_num + 1
def add_logs(function):
def dummy_function(*args, **kwargs):
pass
lines = inspect.getsourcelines(function)
code = "".join(lines[0][1:])
ast_tree = ast.parse(code)
body = ast_tree.body[0].body
#I realize this doesn't do exactly what I want.
#(It doesn't add debug lines inside the if statement)
#Once I get it almost working, I will rewrite this
#to use something like node visitors
body = list(itertools.chain(*zip(log_maker(), body)))
ast_tree.body[0].body = body
fix_line_nums(ast_tree)
code = compile(ast_tree,"<string>", mode='exec')
dummy_function.__code__ = code
return dummy_function
def fix_line_nums(node):
if hasattr(node, "body"):
for index, child in enumerate(node.body):
if hasattr(child, "lineno"):
if index == 0:
if hasattr(node, "lineno"):
child.lineno = node.lineno + 1
else:
# Hopefully this only happens if the parent is a module...
child.lineno = 1
else:
child.lineno = node.body[index - 1].lineno + 1
fix_line_nums(child)
@add_logs
def hey(name):
print("Hi " + name)
t = 1 + 1
if t > 6:
t = t + 1
print("I was bigger")
else:
print("I was not.")
print("t = ", t)
return t
if __name__ == "__main__":
print(hey("mark"))
print(hey)
这会产生此错误:
Traceback (most recent call last):
File "so.py", line 76, in <module>
print(hey("mark"))
TypeError: <module>() takes no arguments (1 given)
这是有道理的,因为compile创建了一个模块,当然模块不是callables。我已经尝试了一百种不同的方法来完成这项工作,但无法提出一个有效的解决方案。有什么建议?我是以错误的方式解决这个问题吗?
(我还没有找到ast模块的教程,这个教程实际上在运行时修改代码就像这样。指向这样教程的指针也会很有用)
注意:我目前正在CPython 3.2上测试这个,但是2.6 / 3.3_and_up解决方案将不胜感激。目前在2.7和3.3上的行为是相同的。
答案 0 :(得分:1)
当您使用修饰函数调用inspect.getsource()
时,您还会获得装饰器,在您的情况下,它会被递归调用(只需两次,第二次生成OSError
)。 / p>
您可以使用此解决方法从源中删除@add_logs
行:
lines = inspect.getsourcelines(function)
code = "".join(lines[0][1:])
修改强>
看起来你的问题是你的dummy_function没有参数:
>>> print(dummy_function.__code__.co_argcount)
0
>>> print(dummy_function.__code__.co_varnames)
()
而您的原始功能确实如此:
>>> print(hey.__code__.co_argcount)
1
>>> print(hey.__code__.co_varnames)
('name')
修改强>
您对作为模块返回的code
对象是正确的。正如另一个答案中所指出的,您必须执行此对象,然后将结果函数(可由function.__name__
标识)分配给dummy_function
。
像这样:
code = compile(ast_tree,"<string>", mode='exec')
mod = {}
exec(code, mod)
dummy_function = mod[function.__name__]
return dummy_function
然后:
>>> print(hey('you'))
line 1
Hi you
line 2
line 3
I was not.
line 4
t = 2
line 5
2
答案 1 :(得分:1)
编译源代码时,会得到一个代表模块的代码对象,而不是函数。将此代码对象替换为现有函数不会起作用,因为它不是函数代码对象,而是模块代码对象。它仍然是一个代码对象,但不是一个真正的模块,你不能只是hey.hey
来从中获取函数。
相反,如this answer中所述,您需要使用exec
来执行模块的代码,将生成的对象存储在字典中,然后提取所需的对象。你大概可以做的是:
code = compile(ast_tree,"<string>", mode='exec')
mod = {}
exec(code, mod)
现在mod['hey']
是修改过的函数。您可以将全局hey
重新分配给此,或替换其代码对象。
我不确定你对AST做了什么是完全正确的,但你需要无论如何都要做到这一点,如果AST操作有问题,那么这样做会让你您可以开始调试它们。
答案 2 :(得分:1)
看起来你正试图通过hackily实现跟踪功能。我可以建议使用sys.settrace
以更可重复的方式实现这一目标吗?
import sys
def trace(f):
_counter = [0] #in py3, we can use `nonlocal`, but this is compatible with py2
def _tracer(frame, event, arg):
if event == 'line':
_counter[0] += 1
print('line {}'.format(_counter[0]))
elif event == 'return': #we're done here, reset the counter
_counter[0] = 0
return _tracer
def _inner(*args, **kwargs):
try:
sys.settrace(_tracer)
f(*args, **kwargs)
finally:
sys.settrace(None)
return _inner
@trace
def hey(name):
print("Hi " + name)
t = 1 + 1
if t > 6:
t = t + 1
print("I was bigger")
else:
print("I was not.")
print("t = ", t)
return t
hey('bob')
输出:
$ python3 test.py
line 1
Hi bob
line 2
line 3
line 4
I was not.
line 5
t = 2
line 6
请注意,其语义与您的实现略有不同;例如,您的代码未执行的if
分支不计算在内。
这最终变得不那么脆弱了 - 你实际上并没有修改你正在装修的功能的代码 - 并且具有额外的实用性。跟踪函数允许您在执行每行代码之前访问框架对象,因此您可以自由地记录本地/全局(或者做一些狡猾的注入事项,如果您这么倾向)。