【发布时间】: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函数必须接收一个参数。满意请确认答案。