所有
我有一个类似问题2617120的问题,在这里找到:
提问者想要通过跟踪钩子执行python打印输出函数参数的指针。
我正在寻找与此类似的东西,但有一点扭曲。我希望在运行时 eval 代码而不是所有数据被转储出来,并打印出任何已知的变量。例如,使用以下代码:
for modname in modnames:
if not modname or '.' in modname:
continue
...
跟踪挂钩会导致打印出以下内容:
for modname in modnames: | for init in init,., encoding
|
if not modname or '.' in modname: | if not init or '.' in init
continue | continue
if not modname or '.' in modname: | if not . or '.' in .
... |
其中代码行根据运行帧进行插值。我已经在perl中做到了这一点,在某些情况下它是一个救星。
任何人都有关于在python中执行此操作的最佳方法的想法?我有自己的想法,但我想听听人们的想法(如果他们有任何预先制定的解决方案)
这里,btw是参考代码:
import sys
import linecache
import random
def traceit(frame, event, arg):
if event == "line":
lineno = frame.f_lineno
filename = frame.f_globals["__file__"]
if filename == "<stdin>":
filename = "traceit.py"
if (filename.endswith(".pyc") or
filename.endswith(".pyo")):
filename = filename[:-1]
name = frame.f_globals["__name__"]
line = linecache.getline(filename, lineno)
print "%s:%s:%s: %s" % (name, lineno,frame.f_code.co_name,line.rstrip())
return traceit
def main():
print "In main"
for i in range(5):
print i, random.randrange(0, 10)
print "Done."
sys.settrace(traceit)
main()
答案 0 :(得分:0)
这里有一个快速入侵,可能会给你一些启动,给定line
中的一行文本和frame
中的当前堆栈框架(这是traceit
的内部函数1}}顺便说一句。)
import re
from types import *
def interpolatevar(matchobj):
excludetypes = set((NoneType, TypeType, FunctionType, LambdaType, ClassType,
CodeType, InstanceType, MethodType, BuiltinFunctionType,
BuiltinMethodType))
var = matchobj.group(0)
basevar = var.split(".")[0]
if basevar in frame.f_code.co_names or basevar in frame.f_code.co_varnames:
if basevar in frame.f_globals or basevar in frame.f_locals:
val = eval(var, frame.f_globals, frame.f_locals)
if type(val) not in excludetypes:
return repr(val)
return var
line = re.sub(r"[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*",
interpolatevar, line)
由于这使用正则表达式来查找标识符,所以即使它们在字符串文字中,它也会愚蠢地找到它们,但标识符必须实际用在函数中并在本地或全局范围内定义。
它确实处理对象属性访问(例如foo.bar
将替换为该值),我最初发布的版本不会,并且它会过滤掉各种类型的值,因为将foo
替换为{ {1}}并没有真正告诉你很多你还不知道的事情。 (排除列表很容易定制,当然,如果您对这些类型的某些值感兴趣,或者您可以从<function foo at 0x02793470>
模块中添加其他值。)
长期来看,我认为查看该行的字节码可能是有利可图的,因为通过这种方式更容易识别哪些令牌实际上是标识符。 types
模块可能对生成语句的解析树很有用,这可以让你弄清楚标识符是什么,但是当你一次只看到一行时,这对条件和循环来说是有问题的