如何在控制台窗口中禁用特征通知处理程序回溯消息的显示?一个例子是:
from traits.api import HasPrivateTraits, Property, String, TraitError
from traits.api import cached_property, on_trait_change
class Equipment (HasPrivateTraits):
manufacturer = String (minlen=2,maxlen=50)
model = String (minlen=2,maxlen=20)
_id = Property (depends_on='manufacturer, model')
@cached_property
def _get__id (self):
return hash ((hash(self.manufacturer), hash(self.model)))
@on_trait_change ('manufacturer, model')
def check_manufacturer_model (self):
if self.manufacturer.upper() == self.model.upper():
raise TraitError ('manufacturer and model are the same')
if __name__ == '__main__':
## http://docs.enthought.com/traits/traits_user_manual/debugging.html
from traits.api import push_exception_handler
push_exception_handler(reraise_exceptions=True)
try:
Equipment (manufacturer='foo', model='foo')
except TraitError as result:
assert str(result) == 'manufacturer and model are the same'
else:
print ('LOGIC ERROR: <<< 01 >>>')
产生以下输出(我想阻止):
Exception occurred in traits notification handler.
Please check the log file for details.
Exception occurred in traits notification handler for object: <__main__.Equipment object at 0x00000275C8AE0468>, trait: model, old value: , new value: foo
Traceback (most recent call last):
File "C:\Users\jgv\AppData\Local\Programs\Python\Python36\lib\site-packages\traits\trait_notifiers.py", line 519, in _dispatch_change_event
self.dispatch( handler, *args )
File "C:\Users\jgv\AppData\Local\Programs\Python\Python36\lib\site-packages\traits\trait_notifiers.py", line 482, in dispatch
handler( *args )
File "testit.py", line 18, in check_manufacturer_model
raise TraitError ('manufacturer and model are the same ({})'.format(self.model))
traits.trait_errors.TraitError: manufacturer and model are the same
我的系统是Python 3.6.0,特性4.6.0和Microsoft Windows 10。
谢谢, -Jim
答案 0 :(得分:2)
您需要为push_exception_handler()
提供一个虚拟异常处理函数来覆盖当前的处理函数。
push_exception_handler(lambda *args: None, reraise_exceptions=True)
只是传递reraise_exceptions=True
会让当前的处理程序函数执行通常的操作,但在完成后再重新加载。