出于教育目的,我希望能够打印出当前函数的完整调用表达式。不一定来自异常处理程序。
经过一些研究,我得到了这段非常简单的代码:
import inspect
import linecache
def print_callexp(*args, **kwargs):
try:
frame = inspect.currentframe()
# method 1, using inspect module only
print(inspect.getframeinfo(frame.f_back).code_context)
# method 2, just for the heck of it
linecache.checkcache(frame.f_code.co_filename)
line = linecache.getline(
frame.f_back.f_code.co_filename,
frame.f_back.f_lineno,
frame.f_back.f_globals)
print(line)
# there's probably a similar method with traceback as well
except:
print("Omagad")
a_var = "but"
print_callexp(a_var, "why?!", 345, hello="world")
结果:
[' print_callexp(a_var, "why?!", 345, hello="world")\n']
print_callexp(a_var, "why?!", 345, hello="world")
它完全符合我的要求,只要调用表达式位于一行即可。但是对于多行表达式,它只会得到最后一行,显然需要我进一步挖掘调用上下文。
# same example but with a multiple lines call
a_var = "but"
print_callexp(
a_var, "why?!", 345, hello="world")
这给了我们:
[' a_var, "why?!", 345, hello="world")\n']
a_var, "why?!", 345, hello="world")
如何正确打印完整的调用表达式?
“使用 lineno 值并应用一些正则表达式/评估技巧”不是一个可接受的答案。我更喜欢更清洁的东西。我不介意导入更多模块,只要它们是 Python 3.x 标准库的一部分即可。尽管如此,我还是会对任何引用资料感兴趣。
最佳答案
出于好奇,这是我用于这种非生产性目的的最终工作代码。乐趣无处不在! (差不多)
我不会立即将此标记为已接受的答案,希望有人能在不久的将来为我们提供更好的选择...
它按预期提取了整个调用表达式。此代码假定调用表达式是一个裸函数调用,没有任何魔法、特殊技巧或嵌套/递归调用。这些特殊情况显然会使检测部分变得不那么琐碎,而且无论如何都是题外话。
在细节上,我使用当前函数名称来帮助定位调用表达式的 AST 节点,以及 inspect
提供的行号作为起点。
我无法使用 inspect.getsource()
来隔离调用者的 block ,这本来可以更优化,因为我发现它返回不完整的源代码的情况。例如,当调用者的代码直接位于 main 的范围内时。不知道它应该是错误还是功能...
一旦我们有了源代码,我们只需要输入 ast.parse()
来获取根 AST 节点并遍历树以找到对当前函数的最新调用,瞧!
#!/usr/bin/env python3
import inspect
import ast
def print_callexp(*args, **kwargs):
def _find_caller_node(root_node, func_name, last_lineno):
# init search state
found_node = None
lineno = 0
def _luke_astwalker(parent):
nonlocal found_node
nonlocal lineno
for child in ast.iter_child_nodes(parent):
# break if we passed the last line
if hasattr(child, "lineno"):
lineno = child.lineno
if lineno > last_lineno:
break
# is it our candidate?
if (isinstance(child, ast.Name)
and isinstance(parent, ast.Call)
and child.id == func_name):
# we have a candidate, but continue to walk the tree
# in case there's another one following. we can safely
# break here because the current node is a Name
found_node = parent
break
# walk through children nodes, if any
_luke_astwalker(child)
# dig recursively to find caller's node
_luke_astwalker(root_node)
return found_node
# get some info from 'inspect'
frame = inspect.currentframe()
backf = frame.f_back
this_func_name = frame.f_code.co_name
# get the source code of caller's module
# note that we have to reload the entire module file since the
# inspect.getsource() function doesn't work in some cases (i.e.: returned
# source content was incomplete... Why?!).
# --> is inspect.getsource broken???
# source = inspect.getsource(backf.f_code)
#source = inspect.getsource(backf.f_code)
with open(backf.f_code.co_filename, "r") as f:
source = f.read()
# get the ast node of caller's module
# we don't need to use ast.increment_lineno() since we've loaded the whole
# module
ast_root = ast.parse(source, backf.f_code.co_filename)
#ast.increment_lineno(ast_root, backf.f_code.co_firstlineno - 1)
# find caller's ast node
caller_node = _find_caller_node(ast_root, this_func_name, backf.f_lineno)
# now, if caller's node has been found, we have the first line and the last
# line of the caller's source
if caller_node:
#start_index = caller_node.lineno - backf.f_code.co_firstlineno
#end_index = backf.f_lineno - backf.f_code.co_firstlineno + 1
print("Hoooray! Found it!")
start_index = caller_node.lineno - 1
end_index = backf.f_lineno
lineno = caller_node.lineno
for ln in source.splitlines()[start_index:end_index]:
print(" {:04d} {}".format(lineno, ln))
lineno += 1
def main():
a_var = "but"
print_callexp(
a_var, "why?!",
345, (1, 2, 3), hello="world")
if __name__ == "__main__":
main()
你应该得到这样的东西:
Hoooray! Found it!
0079 print_callexp(
0080 a_var, "why?!",
0081 345, (1, 2, 3), hello="world")
它仍然感觉有点乱,但是 OTOH,这是一个非常不寻常的目标。至少在 Python 中看起来很不寻常。
例如,乍一看,我希望找到一种方法来直接访问一个已经加载的 AST 节点,该节点可以由 inspect
通过框架对象或以类似的方式提供服务,而不是让手动创建一个新的 AST 节点。
请注意,我完全不知道这是否是特定于 CPython 的代码。它不应该是。至少从我从文档中读到的内容来看是这样。
此外,我想知道为什么 ast
模块(或作为辅助模块)中没有官方的 pretty-print 函数。 ast.dump()
可能会使用额外的 indent
参数来完成这项工作,以允许格式化输出并更轻松地调试 AST
。
作为旁注,我发现它非常整洁小巧 function帮助使用 AST。
关于python - 如何在 Python 中获取函数的调用表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28244921/