summaryrefslogtreecommitdiff
path: root/decorator_utils.py
diff options
context:
space:
mode:
authorScott <[email protected]>2022-01-10 17:22:08 -0800
committerScott <[email protected]>2022-01-10 17:22:08 -0800
commitd29fd1c72456b3c81ffeccd7994c5a65c91a81ed (patch)
treea23b3c9091a88f0c2fd9f72696df19a54b19905d /decorator_utils.py
parentd742c4a0f3a177e3ab55a9eb2d30e0e37af2f044 (diff)
Add another doctest.
Diffstat (limited to 'decorator_utils.py')
-rw-r--r--decorator_utils.py25
1 files changed, 19 insertions, 6 deletions
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