Reputation: 142
How can I disable presentation of traits notification handler traceback messages in a console window? An example is:
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 >>>')
which produces the following output (I want to prevent):
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
My system is Python 3.6.0, traits 4.6.0, and Microsoft Windows 10.
Thanks, -jim
Upvotes: 2
Views: 412
Reputation: 13430
You need to provide push_exception_handler()
with a dummy exception handler function to override the current handler function.
push_exception_handler(lambda *args: None, reraise_exceptions=True)
Just passing reraise_exceptions=True
will let the current handler function do whatever it normally does, but then reraise after it has finished.
Upvotes: 3