X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=decorator_utils.py;h=d5349cc31aed2e74352822c3175ace022ab20e74;hb=d2478310649d51e14f8ece57651ca9d925d98793;hp=9b848ed792144919b863b20c82e846bcd509bbe8;hpb=d742c4a0f3a177e3ab55a9eb2d30e0e37af2f044;p=python_utils.git diff --git a/decorator_utils.py b/decorator_utils.py index 9b848ed..d5349cc 100644 --- a/decorator_utils.py +++ b/decorator_utils.py @@ -535,9 +535,9 @@ def thunkify(func): def _raise_exception(exception, error_message: Optional[str]): if error_message is None: - raise exception() + raise Exception() else: - raise exception(error_message) + raise Exception(error_message) def _target(queue, function, *args, **kwargs): @@ -555,10 +555,10 @@ def _target(queue, function, *args, **kwargs): class _Timeout(object): - """Wrap a function and add a timeout (limit) attribute to it. + """Wrap a function and add a timeout to it. Instances of this class are automatically generated by the add_timeout - function defined below. + function defined below. Do not use directly. """ def __init__( @@ -635,10 +635,24 @@ def timeout( main thread). When not using signals, timeout granularity will be rounded to the nearest 0.1s. - Raises an exception when the timeout is reached. + Raises an exception when/if the timeout is reached. It is illegal to pass anything other than a function as the first parameter. The function is wrapped and returned to the caller. + + >>> @timeout(0.2) + ... def foo(delay: float): + ... time.sleep(delay) + ... return "ok" + + >>> foo(0) + 'ok' + + >>> foo(1.0) + Traceback (most recent call last): + ... + Exception: Function call timed out + """ if use_signals is None: import thread_utils @@ -695,7 +709,6 @@ class non_reentrant_code(object): self._entered = True f(*args, **kwargs) self._entered = False - return _gatekeeper