2017-01-12 7 views
2

コンソールウィンドウでトレイト通知ハンドラのトレースバックメッセージの表示を無効にするにはどうすればよいですか?例は次のとおりです。traits通知ハンドラのトレースバックメッセージを無効にするにはどうすればよいですか?

次の出力(Iがないようにしたい)を生成
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

答えて

2

は、現在のハンドラ関数をオーバーライドするためにダミーの例外ハンドラ関数をpush_exception_handler()を提供する必要があります。ただ、reraise_exceptions=Trueは、現在のハンドラ関数は、それが通常行うものは何でもしましょう、しかし、それが終了した後にリレイズします渡す

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

関連する問題