File: futures.py

package info (click to toggle)
python-pytray 0.3.5-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 192 kB
  • sloc: python: 510; sh: 30; makefile: 3
file content (21 lines) | stat: -rw-r--r-- 658 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- coding: utf-8 -*-
import contextlib

__all__ = ("capture_exceptions",)


@contextlib.contextmanager
def capture_exceptions(future, ignore=()):
    """
    Capture any uncaught exceptions in the context and set them as the result of the given future

    :param future: The future to the exception on, has to have a `set_exception()` method
    :param ignore: An optional list of exception types to ignore, these will be raised and not set on the future
    """
    try:
        yield
    except Exception as exception:  # pylint: disable=broad-except
        if isinstance(exception, ignore):
            raise

        future.set_exception(exception)