From: Scott Date: Tue, 11 Jan 2022 01:22:08 +0000 (-0800) Subject: Add another doctest. X-Git-Url: https://wannabe.guru.org/gitweb/?a=commitdiff_plain;h=d29fd1c72456b3c81ffeccd7994c5a65c91a81ed;p=python_utils.git Add another doctest. --- 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