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