test_my_exceptions.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """
  2. Test my automatically generate exceptions
  3. """
  4. from joblib.my_exceptions import (
  5. JoblibException, JoblibNameError, _mk_exception)
  6. class CustomException(Exception):
  7. def __init__(self, a, b, c, d):
  8. self.a, self.b, self.c, self.d = a, b, c, d
  9. class CustomException2(Exception):
  10. """A custom exception with a .args attribute
  11. Just to check that the JoblibException created from it
  12. has it args set correctly
  13. """
  14. def __init__(self, a, *args):
  15. self.a = a
  16. self.args = args
  17. def test_inheritance():
  18. assert isinstance(JoblibNameError(), NameError)
  19. assert isinstance(JoblibNameError(), JoblibException)
  20. assert (JoblibNameError is _mk_exception(NameError)[0])
  21. def test_inheritance_special_cases():
  22. # _mk_exception should transform Exception to JoblibException
  23. assert (_mk_exception(Exception)[0] is JoblibException)
  24. # Subclasses of JoblibException should be mapped to
  25. # them-selves by _mk_exception
  26. assert (_mk_exception(JoblibException)[0] is JoblibException)
  27. # Non-inheritable exception classes should be mapped to
  28. # JoblibException by _mk_exception. That can happen with classes
  29. # generated with SWIG. See
  30. # https://github.com/joblib/joblib/issues/269 for a concrete
  31. # example.
  32. non_inheritable_classes = [type(lambda: None), bool]
  33. for exception in non_inheritable_classes:
  34. assert (_mk_exception(exception)[0] is JoblibException)
  35. def test__mk_exception():
  36. # Check that _mk_exception works on a bunch of different exceptions
  37. for klass in (Exception, TypeError, SyntaxError, ValueError,
  38. ImportError, CustomException, CustomException2):
  39. message = 'This message should be in the exception repr'
  40. exc = _mk_exception(klass)[0](
  41. message, 'some', 'other', 'args', 'that are not', 'in the repr')
  42. exc_repr = repr(exc)
  43. assert isinstance(exc, klass)
  44. assert isinstance(exc, JoblibException)
  45. assert exc.__class__.__name__ in exc_repr
  46. assert message in exc_repr