【问题标题】:Weird exception thrown in python when called customized profiler调用自定义分析器时在 python 中抛出奇怪的异常
【发布时间】:2017-03-10 09:27:27
【问题描述】:

我写了一个分析器演示,如下所示:

# python version is 2.7.10
import inspect

def _profiler(frame, event, arg):
    print 'name:', frame.f_code.co_name
    print 'is_function:', inspect.isfunction(arg)


import sys

sys.setprofile(_profiler)

def orz():
    print 'why?'

orz()

然后我就这样出来了:

name: orz
is_function: False
why?
name: orz
is_function: False
name: <module>
is_function: False
name: _remove
is_function:Exception AttributeError: "'NoneType' object has no attribute 'isfunction'" in <function _remove at 0x7fb32707c668> ignored

这让我很困惑。此问题由inspect.isfunction 引起。我尝试在函数_profile 中使用许多其他模块,它们都得到了相同的错误:

Exception AttributeError: "'NoneType' object has no attribute 'XXX'" in <function _remove at XXX> ignored

如果我不使用_profiler 中的任何模块,那么程序运行得很好。

为什么?

【问题讨论】:

  • 显然,将 sys.setprofile(None) 添加到代码末尾会删除该异常。我还没有窥探代码,但它似乎试图在执行后删除函数,但由于堆栈为空,它试图删除任何内容(或类似的东西)
  • 你似乎是对的。我注意到内置的配置文件包做了你所说的。我马上试试。
  • 如果这能回答你要找的东西,请告诉我,以便我发布答案。
  • 我试过了,你是对的。你怎么知道的,有没有一些文档提到这个?谢谢回复。
  • 我没有在文档中看到这一点,尽管sys.setprofile() 记录在这里:docs.python.org/2/library/sys.html。我意识到你可以传递任何配置文件,甚至是一个空的配置文件(它什么都不做),因为setprofile 函数必须接收一个参数。满意请确认答案。

标签: python profile


【解决方案1】:

显然,将sys.setprofile(None) 添加到代码末尾会删除该异常。我还没有窥探代码,但它似乎试图在执行后删除函数,但由于堆栈为空,它试图删除任何内容

【讨论】:

    猜你喜欢
    • 2019-02-16
    • 2010-09-16
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    相关资源
    最近更新 更多