如何禁用特征通知处理程序回溯消息?

How to disable traits notification handler traceback messages?

如何禁止在控制台中显示特征通知处理程序回溯消息 window?一个例子是:

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,traits 4.6.0,Microsoft Windows 10.

谢谢, -吉姆

您需要提供 push_exception_handler() 虚拟异常处理函数来覆盖当前处理函数。

push_exception_handler(lambda *args: None, reraise_exceptions=True)

只要传递 reraise_exceptions=True 就会让当前的处理函数做它通常做的任何事情,但在它完成后会重新引发。