def notification_context(handler=null_handler, reraise_exceptions=True, main=False, locked=True): """ Context manager to temporarily add a TraitsExceptionHandler We use a context manager to ensure that the exception handler gets cleared no matter what. Default behaviour is to use the null_handler with exceptions re-raised, which means any exceptions which occur will be passed through. """ yield push_exception_handler(handler, reraise_exceptions, main, locked) pop_exception_handler()
def test_validator_change(self): """ Check that changing a validator works properly. """ output = [False] def handler(obj, name, old, new): output[0] = True push_exception_handler(handler) old = self.component.validator self.component.validator = IntValidator() self.component.validator = old pop_exception_handler() self.assertTrue(output[0])
def reraise_exceptions(logger=_TRAITSUI_LOGGER): """ Context manager to capture all exceptions occurred in the context and then reraise a RuntimeError if there are any exceptions captured. Exceptions from traits change notifications are also captured and reraised. Depending on the GUI toolkit backend, unexpected exceptions occurred in the GUI event loop may (1) cause fatal early exit of the test suite or (2) be printed to the console without causing the test to error. This context manager is intended for testing purpose such that unexpected exceptions will result in a test error. Parameters ---------- logger : logging.Logger Logger to use for logging errors. """ serialized_exceptions = [] def excepthook(type, value, tb): serialized = _serialize_exception(type, value, tb) serialized_exceptions.append(serialized) logger.error("Unexpected error captured in sys excepthook. \n%s", serialized[-1]) def handler(object, name, old, new): type, value, tb = sys.exc_info() serialized_exceptions.append(_serialize_exception(type, value, tb)) logger.exception( "Unexpected error occurred from change handler " "(object: %r, name: %r, old: %r, new: %r).", object, name, old, new, ) push_exception_handler(handler=handler) sys.excepthook = excepthook try: yield finally: sys.excepthook = sys.__excepthook__ pop_exception_handler() if serialized_exceptions: msg = "Uncaught exceptions found.\n" msg += "\n".join("=== Exception (type: {}, value: {}) ===\n" "{}".format(*record) for record in serialized_exceptions) raise RuntimeError(msg)
def tearDown(self): pop_exception_handler()
def __exit__(self, exc_type, exc_value, traceback): pop_exception_handler() return super(ShellNotificationContext, self).__exit__(exc_type, exc_value, traceback)
def teardown(): pop_exception_handler()
def tearDown(self): pop_exception_handler() super(TestUiLoader, self).tearDown()