出于好奇,这是我出于这种非生产性目的的最终工作代码。乐趣无处不在! (几乎)
我不会立即将此标记为已接受的答案,希望有人能在不久的将来为我们提供更好的选择......
它按预期提取整个调用表达式。此代码假定调用表达式是一个裸函数调用,没有任何魔法、特殊技巧或嵌套/递归调用。这些特殊情况显然会使检测部分变得不那么琐碎,并且无论如何都是偏离主题的。
具体来说,我使用当前函数名称来帮助定位调用表达式的 AST 节点,以及由inspect
作为起点。
我无法使用inspect.getsource()
隔离调用者的块,这本来可以更加优化,因为我发现它返回不完整的源代码的情况。例如,当调用者的代码直接位于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")
它仍然感觉有点混乱,但是哦,这是一个非常不寻常的目标。至少在 Python 中看起来很不寻常。
例如,乍一看,我希望找到一种方法来直接访问已加载的 AST 节点,该节点可以由inspect
通过框架对象或以类似的方式,而不必手动创建新的 AST 节点。
请注意,我完全不知道这是否是 CPython 特定代码。不应该是这样。至少从我从文档中读到的内容来看。
我也想知道怎么没有官方的漂亮的印刷品函数在ast
模块(或作为侧面模块)。ast.dump()
可能会用额外的东西来完成这项工作indent
参数允许格式化输出并调试AST
更容易。
作为旁注,我发现这个非常整洁和小function帮助使用 AST。