X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=decorator_utils.py;h=eb5a0c9b9ff581fc3c4398e1e1ab186ec3d00978;hb=8811fb88b36f76360ce8fc20a67931863e59f004;hp=9b848ed792144919b863b20c82e846bcd509bbe8;hpb=d742c4a0f3a177e3ab55a9eb2d30e0e37af2f044;p=python_utils.git diff --git a/decorator_utils.py b/decorator_utils.py index 9b848ed..eb5a0c9 100644 --- a/decorator_utils.py +++ b/decorator_utils.py @@ -443,7 +443,7 @@ def retry_if_false(tries: int, *, delay_sec=3.0, backoff=2.0): 3 >>> dur > 2.0 True - >>> dur < 2.2 + >>> dur < 2.3 True """ @@ -479,7 +479,7 @@ def deprecated(func): def wrapper_deprecated(*args, **kwargs): msg = f"Call to deprecated function {func.__qualname__}" logger.warning(msg) - warnings.warn(msg, category=DeprecationWarning) + warnings.warn(msg, category=DeprecationWarning, stacklevel=2) print(msg, file=sys.stderr) return func(*args, **kwargs) return wrapper_deprecated @@ -507,7 +507,6 @@ def thunkify(func): exc[0] = True exc[1] = sys.exc_info() # (type, value, traceback) msg = f"Thunkify has thrown an exception (will be raised on thunk()):\n{traceback.format_exc()}" - print(msg) logger.warning(msg) finally: wait_event.set() @@ -535,9 +534,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 +554,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 +634,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 +708,6 @@ class non_reentrant_code(object): self._entered = True f(*args, **kwargs) self._entered = False - return _gatekeeper