def wrapper(self, *rest, **kw_args): # This gives us a wrapped method that ignores the suitable # exceptions ... func = Utils.exception_wrapper(functor, exc_list, return_on_exc, self.logger) # ... and here we call the wrapped method. return func(self, *rest, **kw_args)
def test_exception_wrapper_behaviour(): """ Utils.exception_wrapper with valid arguments. """ # Ignoring all exceptions with defaults always yields None assert Utils.exception_wrapper(noop, None)() is None # Ignoring the exception raised with defaults always yields None assert Utils.exception_wrapper(raise1, ValueError)() is None # Exceptions can be given as tuples ... assert Utils.exception_wrapper(raise1, (ValueError,))() is None # ... lists assert Utils.exception_wrapper(raise1, [ValueError, ])() is None # ... or sets without affecting the result assert Utils.exception_wrapper(raise1, set((ValueError,)))() is None # Exception not matching the spec are not caught nose.tools.assert_raises(ValueError, Utils.exception_wrapper(raise1, AttributeError)) # Return value with no exceptions is not altered assert Utils.exception_wrapper(noop, None, '')() is None # Return value with exceptions matches the arg assert Utils.exception_wrapper(raise1, ValueError, '')() == ''
def test_exception_wrapper_returns_callable(): """ Utils.exception_wrapper returns callable. """ assert hasattr(Utils.exception_wrapper(noop), '__call__') Utils.exception_wrapper(noop)()