调用自定义分析器时在 python 中抛出奇怪的异常
Weird exception thrown in python when called customized profiler
我写了一个分析器演示,如下所示:
# 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)
可以消除该异常。我还没有窥探代码,但它似乎试图在函数执行后删除它们,但由于堆栈是空的,它试图删除任何东西
我写了一个分析器演示,如下所示:
# 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)
可以消除该异常。我还没有窥探代码,但它似乎试图在函数执行后删除它们,但由于堆栈是空的,它试图删除任何东西