Python six.reraise() Examples
The following are 30
code examples of six.reraise().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
six
, or try the search function
.
Example #1
Source File: driver.py From compute-hyperv with Apache License 2.0 | 6 votes |
def convert_exceptions(function, exception_map): expected_exceptions = tuple(exception_map.keys()) @functools.wraps(function) def wrapper(*args, **kwargs): try: return function(*args, **kwargs) except expected_exceptions as ex: raised_exception = exception_map.get(type(ex)) if not raised_exception: # exception might be a subclass of an expected exception. for expected in expected_exceptions: if isinstance(ex, expected): raised_exception = exception_map[expected] break exc_info = sys.exc_info() # NOTE(claudiub): Python 3 raises the exception object given as # the second argument in six.reraise. # The original message will be maintained by passing the original # exception. exc = raised_exception(six.text_type(exc_info[1])) six.reraise(raised_exception, exc, exc_info[2]) return wrapper
Example #2
Source File: middleware.py From cloudbridge with MIT License | 6 votes |
def wrap_exception(self, event_args, *args, **kwargs): next_handler = event_args.pop("next_handler") if not next_handler: return try: return next_handler.invoke(event_args, *args, **kwargs) except Exception as e: if isinstance(e, CloudBridgeBaseException): raise else: ex_type, ex_value, traceback = sys.exc_info() cb_ex = CloudBridgeBaseException( "CloudBridgeBaseException: {0} from exception type: {1}" .format(ex_value, ex_type)) if sys.version_info >= (3, 0): six.raise_from(cb_ex, e) else: six.reraise(CloudBridgeBaseException, cb_ex, traceback)
Example #3
Source File: utils.py From gin-config with Apache License 2.0 | 6 votes |
def augment_exception_message_and_reraise(exception, message): """Reraises `exception`, appending `message` to its string representation.""" class ExceptionProxy(type(exception)): """Acts as a proxy for an exception with an augmented message.""" __module__ = type(exception).__module__ def __init__(self): pass def __getattr__(self, attr_name): return getattr(exception, attr_name) def __str__(self): return str(exception) + message ExceptionProxy.__name__ = type(exception).__name__ proxy = ExceptionProxy() if six.PY3: ExceptionProxy.__qualname__ = type(exception).__qualname__ six.raise_from(proxy.with_traceback(exception.__traceback__), None) else: six.reraise(proxy, None, sys.exc_info()[2])
Example #4
Source File: util.py From scalyr-agent-2 with Apache License 2.0 | 6 votes |
def join(self, timeout=None): """Blocks until the thread has finished. If the thread also raised an uncaught exception, this method will raise that same exception. Note, the only way to tell for sure that the thread finished is by invoking 'is_alive' after this method returns. If the thread is still alive, that means this method exited due to a timeout expiring. @param timeout: The number of seconds to wait for the thread to finish or None if it should block indefinitely. @type timeout: float|None """ threading.Thread.join(self, timeout) if not self.isAlive() and self.__exception_info is not None: six.reraise( self.__exception_info[0], self.__exception_info[1], self.__exception_info[2], )
Example #5
Source File: utils_misc.py From avocado-vt with GNU General Public License v2.0 | 6 votes |
def join(self, timeout=None, suppress_exception=False): """ Join the thread. If target raised an exception, re-raise it. Otherwise, return the value returned by target. :param timeout: Timeout value to pass to threading.Thread.join(). :param suppress_exception: If True, don't re-raise the exception. """ threading.Thread.join(self, timeout) try: if self._e: if not suppress_exception: # Because the exception was raised in another thread, we # need to explicitly insert the current context into it s = error_context.exception_context(self._e[1]) s = error_context.join_contexts(error_context.get_context(), s) error_context.set_exception_context(self._e[1], s) six.reraise(*self._e) else: return self._retval finally: # Avoid circular references (join() may be called multiple times # so we can't delete these) self._e = None self._retval = None
Example #6
Source File: __init__.py From insights-core with Apache License 2.0 | 6 votes |
def parse_content(self, content): try: if type(content) is list: self.data = yaml.load('\n'.join(content), Loader=SafeLoader) else: self.data = yaml.load(content, Loader=SafeLoader) if self.data is None: raise SkipException("There is no data") if not isinstance(self.data, (dict, list)): raise ParseException("YAML didn't produce a dictionary or list.") except SkipException as se: tb = sys.exc_info()[2] six.reraise(SkipException, SkipException(str(se)), tb) except: tb = sys.exc_info()[2] cls = self.__class__ name = ".".join([cls.__module__, cls.__name__]) msg = "%s couldn't parse yaml." % name six.reraise(ParseException, ParseException(msg), tb)
Example #7
Source File: __init__.py From django-compat with MIT License | 6 votes |
def import_string(dotted_path): """ Import a dotted module path and return the attribute/class designated by the last name in the path. Raise ImportError if the import failed. Backported from Django 1.7 """ try: module_path, class_name = dotted_path.rsplit('.', 1) except ValueError: msg = "%s doesn't look like a module path" % dotted_path six.reraise(ImportError, ImportError(msg), sys.exc_info()[2]) module = import_module(module_path) try: return getattr(module, class_name) except AttributeError: msg = 'Module "%s" does not define a "%s" attribute/class' % ( dotted_path, class_name) six.reraise(ImportError, ImportError(msg), sys.exc_info()[2])
Example #8
Source File: running.py From planet with Apache License 2.0 | 6 votes |
def __iter__(self): """Iterate over the process function and finalize the log directory.""" try: args = self._init_fn and self._init_fn(self._logdir) if args is None: args = () if not isinstance(args, tuple): args = (args,) for value in self._process_fn(self._logdir, *args): if not self._running[0]: break yield value self._logger.info('Done.') self._store_done() except WorkerConflict: self._logging.warn('Unexpected takeover.') raise SkipRun except Exception as e: exc_info = sys.exc_info() self._handle_exception(e) six.reraise(*exc_info) finally: self._running[0] = False self._thread and self._thread.join()
Example #9
Source File: base.py From omniduct with MIT License | 6 votes |
def set(self, key, value, namespace=None, serializer=None, metadata=None): """ Set the value of a key. Args: key (str): The key for which `value` should be stored. value (object): The value to be stored. namespace (str, None): The namespace to be used. serializer (Serializer): The `Serializer` subclass to use for the serialisation of value into the cache. (default=PickleSerializer) metadata (dict, None): Additional metadata to be stored with the value in the cache. Values must be serializable via `yaml.safe_dump`. """ namespace, key = self._namespace(namespace), self._key(key) serializer = serializer or PickleSerializer() try: with self._get_stream_for_key(namespace, key, 'data{}'.format(serializer.file_extension), mode='wb', create=True) as fh: serializer.serialize(value, fh) self.set_metadata(key, metadata, namespace=namespace, replace=True) except: self.unset(key, namespace=namespace) six.reraise(*sys.exc_info())
Example #10
Source File: utils.py From python-logstash-async with MIT License | 6 votes |
def import_string(dotted_path): """ Import a dotted module path and return the attribute/class designated by the last name in the path. Raise ImportError if the import failed. (stolen from Django) """ try: module_path, class_name = dotted_path.rsplit('.', 1) except ValueError: msg = "{} doesn't look like a module path".format(dotted_path) six.reraise(ImportError, ImportError(msg), sys.exc_info()[2]) module = import_module(module_path) try: return getattr(module, class_name) except AttributeError: msg = 'Module "{}" does not define a "{}" attribute/class'.format(module_path, class_name) six.reraise(ImportError, ImportError(msg), sys.exc_info()[2])
Example #11
Source File: manager.py From linter-pylama with MIT License | 6 votes |
def file_from_module_name(self, modname, contextfile): try: value = self._mod_file_cache[(modname, contextfile)] traceback = sys.exc_info()[2] except KeyError: try: value = modutils.file_info_from_modpath( modname.split('.'), context_file=contextfile) traceback = sys.exc_info()[2] except ImportError as ex: value = exceptions.AstroidImportError( 'Failed to import module {modname} with error:\n{error}.', modname=modname, error=ex) traceback = sys.exc_info()[2] self._mod_file_cache[(modname, contextfile)] = value if isinstance(value, exceptions.AstroidBuildingError): six.reraise(exceptions.AstroidBuildingError, value, traceback) return value
Example #12
Source File: data_utils.py From GraphicDesignPatternByPython with MIT License | 6 votes |
def get(self): """Creates a generator to extract data from the queue. Skip the data if it is `None`. # Yields The next element in the queue, i.e. a tuple `(inputs, targets)` or `(inputs, targets, sample_weights)`. """ try: while self.is_running(): inputs = self.queue.get(block=True).get() self.queue.task_done() if inputs is not None: yield inputs except Exception as e: self.stop() six.reraise(*sys.exc_info())
Example #13
Source File: response.py From pyspider with Apache License 2.0 | 6 votes |
def raise_for_status(self, allow_redirects=True): """Raises stored :class:`HTTPError` or :class:`URLError`, if one occurred.""" if self.status_code == 304: return elif self.error: if self.traceback: six.reraise(Exception, Exception(self.error), Traceback.from_string(self.traceback).as_traceback()) http_error = HTTPError(self.error) elif (self.status_code >= 300) and (self.status_code < 400) and not allow_redirects: http_error = HTTPError('%s Redirection' % (self.status_code)) elif (self.status_code >= 400) and (self.status_code < 500): http_error = HTTPError('%s Client Error' % (self.status_code)) elif (self.status_code >= 500) and (self.status_code < 600): http_error = HTTPError('%s Server Error' % (self.status_code)) else: return http_error.response = self raise http_error
Example #14
Source File: data_utils.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def get(self): """Creates a generator to extract data from the queue. Skip the data if it is `None`. # Yields The next element in the queue, i.e. a tuple `(inputs, targets)` or `(inputs, targets, sample_weights)`. """ while self.is_running(): if not self.queue.empty(): success, value = self.queue.get() # Rethrow any exceptions found in the queue if not success: six.reraise(value.__class__, value, value.__traceback__) # Yield regular values if value is not None: yield value else: all_finished = all([not thread.is_alive() for thread in self._threads]) if all_finished and self.queue.empty(): raise StopIteration() else: time.sleep(self.wait_time) # Make sure to rethrow the first exception in the queue, if any while not self.queue.empty(): success, value = self.queue.get() if not success: six.reraise(value.__class__, value, value.__traceback__)
Example #15
Source File: data_utils.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def get(self): """Creates a generator to extract data from the queue. Skip the data if it is `None`. # Yields The next element in the queue, i.e. a tuple `(inputs, targets)` or `(inputs, targets, sample_weights)`. """ while self.is_running(): if not self.queue.empty(): success, value = self.queue.get() # Rethrow any exceptions found in the queue if not success: six.reraise(value.__class__, value, value.__traceback__) # Yield regular values if value is not None: yield value else: all_finished = all([not thread.is_alive() for thread in self._threads]) if all_finished and self.queue.empty(): raise StopIteration() else: time.sleep(self.wait_time) # Make sure to rethrow the first exception in the queue, if any while not self.queue.empty(): success, value = self.queue.get() if not success: six.reraise(value.__class__, value, value.__traceback__)
Example #16
Source File: data_utils.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def get(self): """Creates a generator to extract data from the queue. Skip the data if it is `None`. # Yields The next element in the queue, i.e. a tuple `(inputs, targets)` or `(inputs, targets, sample_weights)`. """ while self.is_running(): if not self.queue.empty(): success, value = self.queue.get() # Rethrow any exceptions found in the queue if not success: six.reraise(value.__class__, value, value.__traceback__) # Yield regular values if value is not None: yield value else: all_finished = all([not thread.is_alive() for thread in self._threads]) if all_finished and self.queue.empty(): raise StopIteration() else: time.sleep(self.wait_time) # Make sure to rethrow the first exception in the queue, if any while not self.queue.empty(): success, value = self.queue.get() if not success: six.reraise(value.__class__, value, value.__traceback__)
Example #17
Source File: data_utils.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def get(self): """Creates a generator to extract data from the queue. Skip the data if it is `None`. # Yields The next element in the queue, i.e. a tuple `(inputs, targets)` or `(inputs, targets, sample_weights)`. """ while self.is_running(): if not self.queue.empty(): success, value = self.queue.get() # Rethrow any exceptions found in the queue if not success: six.reraise(value.__class__, value, value.__traceback__) # Yield regular values if value is not None: yield value else: all_finished = all([not thread.is_alive() for thread in self._threads]) if all_finished and self.queue.empty(): raise StopIteration() else: time.sleep(self.wait_time) # Make sure to rethrow the first exception in the queue, if any while not self.queue.empty(): success, value = self.queue.get() if not success: six.reraise(value.__class__, value, value.__traceback__)
Example #18
Source File: subproc.py From insights-core with Apache License 2.0 | 5 votes |
def write(self, output, mode="w", keep_rc=False): """ Executes the pipeline and writes the results to the supplied output. If output is a filename and the file didn't already exist before trying to write, the file will be removed if an exception is raised. Args: output (str or file like object): will create a new file of this name or overwrite an existing file. If output is already a file like object, it is used. mode (str): mode to use when creating or opening the provided file name if it is a string. Ignored if output is a file like object. Returns: The final output of the pipeline. Raises: CalledProcessError if any return code in the pipeline is nonzero. """ if isinstance(output, six.string_types): already_exists = os.path.exists(output) try: with open(output, mode) as f: p = self._build_pipes(f) rc = p.wait() if keep_rc: return rc if rc: raise CalledProcessError(rc, self.cmds[0], "") except BaseException as be: if not already_exists and os.path.exists(output): os.remove(output) six.reraise(be.__class__, be, sys.exc_info()[2]) else: p = self._build_pipes(output) rc = p.wait() if keep_rc: return rc if rc: raise CalledProcessError(rc, self.cmds[0], "")
Example #19
Source File: date.py From insights-core with Apache License 2.0 | 5 votes |
def parse_content(self, content): """ Parses the output of the ``date`` and ``date --utc`` command. Sample: Fri Jun 24 09:13:34 CST 2016 Sample: Fri Jun 24 09:13:34 UTC 2016 Attributes ---------- datetime: datetime.datetime A native datetime.datetime of the parsed date string timezone: str The string portion of the date string containing the timezone Raises: DateParseException: Raised if any exception occurs parsing the content. """ self.data = get_active_lines(content, comment_char="COMMAND>")[0] parts = self.data.split() if not len(parts) == 6: msg = "Expected six date parts. Got [%s]" raise DateParseException(msg % self.data) try: self.timezone = parts[4] no_tz = ' '.join(parts[:4]) + ' ' + parts[-1] self.datetime = datetime.strptime(no_tz, '%a %b %d %H:%M:%S %Y') except: six.reraise(DateParseException, DateParseException(self.data), sys.exc_info()[2])
Example #20
Source File: exception.py From vdi-broker with Apache License 2.0 | 5 votes |
def __init__(self, message=None, **kwargs): self.kwargs = kwargs if 'code' not in self.kwargs: try: self.kwargs['code'] = self.code except AttributeError: pass for k, v in self.kwargs.items(): if isinstance(v, Exception): self.kwargs[k] = six.text_type(v) if self._should_format(message): try: message = self.message % kwargs except Exception: exc_info = sys.exc_info() # kwargs doesn't match a variable in the message # log the issue and the kwargs LOG.exception(_LE('Exception in string format operation')) for name, value in kwargs.items(): LOG.error(_LE("%(name)s: %(value)s"), {'name': name, 'value': value}) if CONF.fatal_exception_format_errors: six.reraise(*exc_info) # at least get the core message out if something happened message = self.message elif isinstance(message, Exception): message = six.text_type(message) # NOTE(luisg): We put the actual message in 'msg' so that we can access # it, because if we try to access the message via 'message' it will be # overshadowed by the class' message attribute self.msg = message super(VDIBrokerException, self).__init__(message)
Example #21
Source File: zdesk.py From zdesk with MIT License | 5 votes |
def _handle_retry(self, resp): """Handle any exceptions during API request or parsing its response status code. Parameters: resp: requests.Response instance obtained during concerning request or None, when request failed Returns: True if should retry our request or raises original Exception """ exc_t, exc_v, exc_tb = sys.exc_info() if exc_t is None: raise TypeError('Must be called in except block.') retry_on_exc = tuple( (x for x in self._retry_on if inspect.isclass(x))) retry_on_codes = tuple( (x for x in self._retry_on if isinstance(x, int))) if issubclass(exc_t, ZendeskError): code = exc_v.error_code if exc_t not in retry_on_exc and code not in retry_on_codes: six.reraise(exc_t, exc_v, exc_tb) else: if not issubclass(exc_t, retry_on_exc): six.reraise(exc_t, exc_v, exc_tb) if resp is not None: try: retry_after = float(resp.headers.get('Retry-After', 0)) time.sleep(retry_after) except (TypeError, ValueError): pass return True
Example #22
Source File: kmip_client.py From PyKMIP with Apache License 2.0 | 5 votes |
def open(self): self.logger.debug("KMIPProxy keyfile: {0}".format(self.keyfile)) self.logger.debug("KMIPProxy certfile: {0}".format(self.certfile)) self.logger.debug( "KMIPProxy cert_reqs: {0} (CERT_REQUIRED: {1})".format( self.cert_reqs, ssl.CERT_REQUIRED)) self.logger.debug( "KMIPProxy ssl_version: {0} (PROTOCOL_SSLv23: {1})".format( self.ssl_version, ssl.PROTOCOL_SSLv23)) self.logger.debug("KMIPProxy ca_certs: {0}".format(self.ca_certs)) self.logger.debug("KMIPProxy do_handshake_on_connect: {0}".format( self.do_handshake_on_connect)) self.logger.debug("KMIPProxy suppress_ragged_eofs: {0}".format( self.suppress_ragged_eofs)) last_error = None for host in self.host_list: self.host = host sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._create_socket(sock) self.protocol = KMIPProtocol(self.socket) try: self.socket.connect((self.host, self.port)) except Exception as e: self.logger.error("An error occurred while connecting to " "appliance %s: %s", self.host, e) self.socket.close() last_error = sys.exc_info() else: return self.socket = None if last_error: six.reraise(*last_error)
Example #23
Source File: crawler.py From learn_python3_spider with MIT License | 5 votes |
def crawl(self, *args, **kwargs): assert not self.crawling, "Crawling already taking place" self.crawling = True try: self.spider = self._create_spider(*args, **kwargs) self.engine = self._create_engine() start_requests = iter(self.spider.start_requests()) yield self.engine.open_spider(self.spider, start_requests) yield defer.maybeDeferred(self.engine.start) except Exception: # In Python 2 reraising an exception after yield discards # the original traceback (see https://bugs.python.org/issue7563), # so sys.exc_info() workaround is used. # This workaround also works in Python 3, but it is not needed, # and it is slower, so in Python 3 we use native `raise`. if six.PY2: exc_info = sys.exc_info() self.crawling = False if self.engine is not None: yield self.engine.close() if six.PY2: six.reraise(*exc_info) raise
Example #24
Source File: crawler.py From learn_python3_spider with MIT License | 5 votes |
def crawl(self, *args, **kwargs): assert not self.crawling, "Crawling already taking place" self.crawling = True try: self.spider = self._create_spider(*args, **kwargs) self.engine = self._create_engine() start_requests = iter(self.spider.start_requests()) yield self.engine.open_spider(self.spider, start_requests) yield defer.maybeDeferred(self.engine.start) except Exception: # In Python 2 reraising an exception after yield discards # the original traceback (see https://bugs.python.org/issue7563), # so sys.exc_info() workaround is used. # This workaround also works in Python 3, but it is not needed, # and it is slower, so in Python 3 we use native `raise`. if six.PY2: exc_info = sys.exc_info() self.crawling = False if self.engine is not None: yield self.engine.close() if six.PY2: six.reraise(*exc_info) raise
Example #25
Source File: python_message.py From coremltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _ReraiseTypeErrorWithFieldName(message_name, field_name): """Re-raise the currently-handled TypeError with the field name added.""" exc = sys.exc_info()[1] if len(exc.args) == 1 and type(exc) is TypeError: # simple TypeError; add field name to exception message exc = TypeError('%s for field %s.%s' % (str(exc), message_name, field_name)) # re-raise possibly-amended exception with original traceback: six.reraise(type(exc), exc, sys.exc_info()[2])
Example #26
Source File: expressions.py From yaql with Apache License 2.0 | 5 votes |
def __call__(self, receiver, context, engine): if not context.collect_functions('#finalize'): context = context.create_child_context() context.register_function(lambda x: x, name='#finalize') try: return super(Statement, self).__call__(receiver, context, engine) except exceptions.WrappedException as e: six.reraise(type(e.wrapped), e.wrapped, sys.exc_info()[2])
Example #27
Source File: runner.py From yaql with Apache License 2.0 | 5 votes |
def call(name, context, args, kwargs, engine, receiver=utils.NO_VALUE, data_context=None, use_convention=False, function_filter=None): if data_context is None: data_context = context if function_filter is None: function_filter = lambda fd, ctx: True if receiver is utils.NO_VALUE: predicate = lambda fd, ctx: fd.is_function and function_filter(fd, ctx) else: predicate = lambda fd, ctx: fd.is_method and function_filter(fd, ctx) all_overloads = context.collect_functions( name, predicate, use_convention=use_convention) if not all_overloads: if receiver is utils.NO_VALUE: raise exceptions.NoFunctionRegisteredException(name) else: raise exceptions.NoMethodRegisteredException(name, receiver) else: delegate = choose_overload( name, all_overloads, engine, receiver, data_context, args, kwargs) try: result = delegate() utils.limit_memory_usage(engine, (1, result)) return result except StopIteration as e: six.reraise( exceptions.WrappedException, exceptions.WrappedException(e), sys.exc_info()[2])
Example #28
Source File: queries.py From yaql with Apache License 2.0 | 5 votes |
def __call__(self, group_item): if self.aggregator is None: return group_item if self._failure_info is None: key, value_list = group_item try: result = self.aggregator(value_list) except (exceptions.NoMatchingMethodException, exceptions.NoMatchingFunctionException, IndexError): self._failure_info = sys.exc_info() else: if not (len(value_list) == 2 and isinstance(result, collections.Sequence) and not isinstance(result, six.string_types) and len(result) == 2 and result[0] == value_list[0]): # We are not dealing with (correct) version 1.1.1 syntax, # so don't bother trying to fall back if there's an error # with a later group. self.allow_fallback = False return key, result if self.allow_fallback: # Fall back to assuming version 1.1.1 syntax. try: result = self.aggregator(group_item) if len(result) == 2: return result except Exception: pass # If we are unable to successfully fall back, re-raise the first # exception encountered to help the user debug in the new style. six.reraise(*self._failure_info)
Example #29
Source File: data_utils.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def get(self): """Creates a generator to extract data from the queue. Skip the data if it is `None`. # Yields The next element in the queue, i.e. a tuple `(inputs, targets)` or `(inputs, targets, sample_weights)`. """ try: while self.is_running(): inputs = self.queue.get(block=True).get() self.queue.task_done() if inputs is not None: yield inputs except StopIteration: # Special case for finite generators last_ones = [] while self.queue.qsize() > 0: last_ones.append(self.queue.get(block=True)) # Wait for them to complete list(map(lambda f: f.wait(), last_ones)) # Keep the good ones last_ones = [future.get() for future in last_ones if future.successful()] for inputs in last_ones: if inputs is not None: yield inputs except Exception as e: self.stop() if 'generator already executing' in str(e): raise RuntimeError( "Your generator is NOT thread-safe." "Keras requires a thread-safe generator when" "`use_multiprocessing=False, workers > 1`." "For more information see issue #1638.") six.reraise(*sys.exc_info())
Example #30
Source File: wsgi.py From ontask_b with MIT License | 5 votes |
def null_technical_500_response(request, exc_type, exc_value, tb): """Catching the 500 response.""" del request six.reraise(exc_type, exc_value, tb)