跟踪python解释器的执行路径 [英] trace execution path of python interpreter

查看:39
本文介绍了跟踪python解释器的执行路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码可以很好地了解解释器的作用:

The following works nice to see what the interpreter does:

python -m trace --ignore-dir=$HOME/lib64:$HOME/lib:/usr -t path-to/script.py

但是行数太多了.我想隐藏导入文件期间发生的行.

But there are too many lines. I would like to hide the lines which happen during importing a file.

例如:我对这样的行不感兴趣:

Example: I am not interested in lines like this:

saved_filter.py(9): class SavedFilter(models.Model):
saved_filter.py(10):     name = models.TextField(max_length=256)
saved_filter.py(11):     user = models.ForeignKey('auth.User', null=True, blank=True)

我在文档中找不到解决方案:https://docs.python.org/2/library/trace.html

I could not find a solution in the docs: https://docs.python.org/2/library/trace.html

更新如果有不同的方法来获得结果,例如不同的 python 包(不是跟踪),这也是一个很好的解决方案.

Update If there is a different way to get the result, for example a different python package (not trace), this would be a good solution, too.

更新 2跟踪应该是非交互式的.

Update 2 The tracing should be non-interactive.

更新 3

我尝试了 Martin v. Löwis 提供的解决方案.它在某些情况下有效,但不是全部.

I tried the solution provided by Martin v. Löwis. It works in some cases, but not all.

文件 foo.py

import bar

def main():
    f=bar.Foo()
    f.my_func()

if __name__=='__main__':
    main()

文件 bar.py

class Foo(object):
    def my_func(self):
        def inner():
            print('#in inner')
            return 'inner'
        print('#in my_func()')
        inner()
        return 1

如果我调用 foo.py,想要的结果类似于:

If I call foo.py, the wanted result looks similar to this:

foo.py: f=bar.Foo()foo.py: f.my_func()bar.py: print('#in my_func()')bar.py:内部()bar.py: 打印('#in 内部')bar.py:返回内部"bar.py: 返回 1

foo.py: f=bar.Foo() foo.py: f.my_func() bar.py: print('#in my_func()') bar.py: inner() bar.py: print('#in inner') bar.py: return 'inner' bar.py: return 1

trace2.py 的结果

Result of trace2.py

> python tmp/trace2-orig.py --trace tmp/foo.py 
 --- modulename: foo, funcname: <module>
 --- modulename: bar, funcname: <module>
 --- modulename: bar, funcname: Foo
bar.py(1): class Foo(object):               <======= Import lines
bar.py(2):     def my_func(self):
 --- modulename: foo, funcname: main
foo.py(4):     f=bar.Foo()
foo.py(5):     f.my_func()
 --- modulename: bar, funcname: my_func
bar.py(3):         def inner():
bar.py(6):         print('#in my_func()')
#in my_func()
bar.py(7):         inner()
 --- modulename: bar, funcname: inner
bar.py(4):             print('#in inner')
#in inner
bar.py(5):             return 'inner'
bar.py(8):         return 1
 --- modulename: trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)

不幸的是,仍然有 class Foo(object) 这是在导入期间执行的东西.

Unfortunately there is still class Foo(object) which is something executed during import.

我猜代码加载和执行的检测不能涵盖所有情况.

I guess the detection of code loading and executing does not cover all cases.

推荐答案

如果创建脚本 trace2.py 为

If you create a script trace2.py as

import trace

OrigTrace = trace.Trace
class Trace2(trace.Trace):
    def localtrace_trace(self, frame, why, arg):
        if why == "line" and frame.f_code.co_name == '<module>':
            return self.localtrace
        return OrigTrace.localtrace_trace(self, frame, why, arg)

trace.Trace=Trace2
trace.main()

并运行 python -m trace2 -t script.py 你不会看到模块级别的行的跟踪输出.

and run python -m trace2 -t script.py you will not see trace output from lines that are on the module level.

这篇关于跟踪python解释器的执行路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆