_deprecated_my_exceptions.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """
  2. Exceptions
  3. This module is deprecated and will be removed in joblib 0.16.
  4. """
  5. # Author: Gael Varoquaux < gael dot varoquaux at normalesup dot org >
  6. # Copyright: 2010, Gael Varoquaux
  7. # License: BSD 3 clause
  8. class JoblibException(Exception):
  9. """A simple exception with an error message that you can get to."""
  10. def __init__(self, *args):
  11. # We need to implement __init__ so that it is picked in the
  12. # multiple heritance hierarchy in the class created in
  13. # _mk_exception. Note: in Python 2, if you implement __init__
  14. # in your exception class you need to set .args correctly,
  15. # otherwise you can dump an exception instance with pickle but
  16. # not load it (at load time an empty .args will be passed to
  17. # the constructor). Also we want to be explicit and not use
  18. # 'super' here. Using 'super' can cause a sibling class method
  19. # to be called and we have no control the sibling class method
  20. # constructor signature in the exception returned by
  21. # _mk_exception.
  22. Exception.__init__(self, *args)
  23. def __repr__(self):
  24. if hasattr(self, 'args') and len(self.args) > 0:
  25. message = self.args[0]
  26. else:
  27. message = ''
  28. name = self.__class__.__name__
  29. return '%s\n%s\n%s\n%s' % (name, 75 * '_', message, 75 * '_')
  30. __str__ = __repr__
  31. class TransportableException(JoblibException):
  32. """An exception containing all the info to wrap an original
  33. exception and recreate it.
  34. """
  35. def __init__(self, message, etype):
  36. # The next line set the .args correctly. This is needed to
  37. # make the exception loadable with pickle
  38. JoblibException.__init__(self, message, etype)
  39. self.message = message
  40. self.etype = etype
  41. def unwrap(self, context_message=""):
  42. report = """\
  43. %s
  44. ---------------------------------------------------------------------------
  45. Joblib worker traceback:
  46. ---------------------------------------------------------------------------
  47. %s""" % (context_message, self.message)
  48. # Unwrap the exception to a JoblibException
  49. exception_type = _mk_exception(self.etype)[0]
  50. return exception_type(report)
  51. _exception_mapping = dict()
  52. def _mk_exception(exception, name=None):
  53. if issubclass(exception, JoblibException):
  54. # No need to wrap recursively JoblibException
  55. return exception, exception.__name__
  56. # Create an exception inheriting from both JoblibException
  57. # and that exception
  58. if name is None:
  59. name = exception.__name__
  60. this_name = 'Joblib%s' % name
  61. if this_name in _exception_mapping:
  62. # Avoid creating twice the same exception
  63. this_exception = _exception_mapping[this_name]
  64. else:
  65. if exception is Exception:
  66. # JoblibException is already a subclass of Exception. No
  67. # need to use multiple inheritance
  68. return JoblibException, this_name
  69. try:
  70. this_exception = type(
  71. this_name, (JoblibException, exception), {})
  72. _exception_mapping[this_name] = this_exception
  73. except TypeError:
  74. # This happens if "Cannot create a consistent method
  75. # resolution order", e.g. because 'exception' is a
  76. # subclass of JoblibException or 'exception' is not an
  77. # acceptable base class
  78. this_exception = JoblibException
  79. return this_exception, this_name
  80. def _mk_common_exceptions():
  81. namespace = dict()
  82. import builtins as _builtin_exceptions
  83. common_exceptions = filter(
  84. lambda x: x.endswith('Error'),
  85. dir(_builtin_exceptions))
  86. for name in common_exceptions:
  87. obj = getattr(_builtin_exceptions, name)
  88. if isinstance(obj, type) and issubclass(obj, BaseException):
  89. this_obj, this_name = _mk_exception(obj, name=name)
  90. namespace[this_name] = this_obj
  91. return namespace
  92. # Updating module locals so that the exceptions pickle right. AFAIK this
  93. # works only at module-creation time
  94. locals().update(_mk_common_exceptions())