mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 14:03:30 +00:00
python: Reformat with Black, except quotes.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
committed by
Tim Abbott
parent
5028c081cb
commit
11741543da
@@ -7,29 +7,32 @@ from typing import Any, Callable, Optional, Tuple, Type, TypeVar
|
||||
|
||||
# Based on https://code.activestate.com/recipes/483752/
|
||||
|
||||
|
||||
class TimeoutExpired(Exception):
|
||||
'''Exception raised when a function times out.'''
|
||||
|
||||
def __str__(self) -> str:
|
||||
return 'Function call timed out.'
|
||||
|
||||
|
||||
ResultT = TypeVar('ResultT')
|
||||
|
||||
|
||||
def timeout(timeout: float, func: Callable[..., ResultT], *args: Any, **kwargs: Any) -> ResultT:
|
||||
'''Call the function in a separate thread.
|
||||
Return its return value, or raise an exception,
|
||||
within approximately 'timeout' seconds.
|
||||
"""Call the function in a separate thread.
|
||||
Return its return value, or raise an exception,
|
||||
within approximately 'timeout' seconds.
|
||||
|
||||
The function may receive a TimeoutExpired exception
|
||||
anywhere in its code, which could have arbitrary
|
||||
unsafe effects (resources not released, etc.).
|
||||
It might also fail to receive the exception and
|
||||
keep running in the background even though
|
||||
timeout() has returned.
|
||||
The function may receive a TimeoutExpired exception
|
||||
anywhere in its code, which could have arbitrary
|
||||
unsafe effects (resources not released, etc.).
|
||||
It might also fail to receive the exception and
|
||||
keep running in the background even though
|
||||
timeout() has returned.
|
||||
|
||||
This may also fail to interrupt functions which are
|
||||
stuck in a long-running primitive interpreter
|
||||
operation.'''
|
||||
This may also fail to interrupt functions which are
|
||||
stuck in a long-running primitive interpreter
|
||||
operation."""
|
||||
|
||||
class TimeoutThread(threading.Thread):
|
||||
def __init__(self) -> None:
|
||||
@@ -57,7 +60,8 @@ def timeout(timeout: float, func: Callable[..., ResultT], *args: Any, **kwargs:
|
||||
assert self.ident is not None # Thread should be running; c_long expects int
|
||||
tid = ctypes.c_long(self.ident)
|
||||
result = ctypes.pythonapi.PyThreadState_SetAsyncExc(
|
||||
tid, ctypes.py_object(TimeoutExpired))
|
||||
tid, ctypes.py_object(TimeoutExpired)
|
||||
)
|
||||
if result > 1:
|
||||
# "if it returns a number greater than one, you're in trouble,
|
||||
# and you should call it again with exc=NULL to revert the effect"
|
||||
|
||||
Reference in New Issue
Block a user