gevent.util – Low-level utilities

class wrap_errors(errors, func)

Bases: object

Helper to make function return an exception, rather than raise it.

Because every exception that is unhandled by greenlet will be logged, it is desirable to prevent non-error exceptions from leaving a greenlet. This can done with a simple try/except construct:

def wrapped_func(*args, **kwargs):
    try:
        return func(*args, **kwargs)
    except (TypeError, ValueError, AttributeError) as ex:
        return ex

This class provides a shortcut to write that in one line:

wrapped_func = wrap_errors((TypeError, ValueError, AttributeError), func)

It also preserves __str__ and __repr__ of the original function.

Calling this makes a new function from func, such that it catches errors (an BaseException subclass, or a tuple of BaseException subclasses) and return it as a value.

Next page: Low-level details