Update dafunc.py
minor changes: just rewrote the code little bit make it shorter and removed unnecessary statements
This commit is contained in:
parent
50baa8db50
commit
d9556e52ec
@ -8,6 +8,7 @@
|
|||||||
LICENSE, otherwise it is available at https://github.com/kata198/func_timeout/LICENSE
|
LICENSE, otherwise it is available at https://github.com/kata198/func_timeout/LICENSE
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
import inspect
|
import inspect
|
||||||
import threading
|
import threading
|
||||||
@ -20,11 +21,8 @@ from .StoppableThread import StoppableThread
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
from .py3_raise import raise_exception
|
from .py3_raise import raise_exception
|
||||||
except SyntaxError:
|
except (SyntaxError, ImportError):
|
||||||
from .py2_raise import raise_exception
|
from .py2_raise import raise_exception
|
||||||
except ImportError:
|
|
||||||
from .py2_raise import raise_exception
|
|
||||||
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
__all__ = ('func_timeout', 'func_set_timeout')
|
__all__ = ('func_timeout', 'func_set_timeout')
|
||||||
@ -65,19 +63,19 @@ def func_timeout(timeout, func, args=(), kwargs=None):
|
|||||||
|
|
||||||
def funcwrap(args2, kwargs2):
|
def funcwrap(args2, kwargs2):
|
||||||
try:
|
try:
|
||||||
ret.append( func(*args2, **kwargs2) )
|
ret.append(func(*args2, **kwargs2))
|
||||||
except FunctionTimedOut:
|
except FunctionTimedOut:
|
||||||
# Don't print traceback to stderr if we time out
|
# Don't print traceback to stderr if we time out
|
||||||
pass
|
pass
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
exc_info = sys.exc_info()
|
exc_info = sys.exc_info()
|
||||||
if isStopped is False:
|
if not isStopped:
|
||||||
# Assemble the alternate traceback, excluding this function
|
# Assemble the alternate traceback, excluding this function
|
||||||
# from the trace (by going to next frame)
|
# from the trace (by going to next frame)
|
||||||
# Pytohn3 reads native from __traceback__,
|
# Pytohn3 reads native from __traceback__,
|
||||||
# python2 has a different form for "raise"
|
# python2 has a different form for "raise"
|
||||||
e.__traceback__ = exc_info[2].tb_next
|
e.__traceback__ = exc_info[2].tb_next
|
||||||
exception.append( e )
|
exception.append(e)
|
||||||
|
|
||||||
thread = StoppableThread(target=funcwrap, args=(args, kwargs))
|
thread = StoppableThread(target=funcwrap, args=(args, kwargs))
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
@ -93,7 +91,8 @@ def func_timeout(timeout, func, args=(), kwargs=None):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
return FunctionTimedOut.__init__(self, '', timeout, func, args, kwargs)
|
return FunctionTimedOut.__init__(self, '', timeout, func, args, kwargs)
|
||||||
|
|
||||||
FunctionTimedOutTemp = type('FunctionTimedOut' + str( hash( "%d_%d_%d_%d" %(id(timeout), id(func), id(args), id(kwargs))) ), FunctionTimedOutTempType.__bases__, dict(FunctionTimedOutTempType.__dict__))
|
FunctionTimedOutTemp = type('FunctionTimedOut' + str(hash("%d_%d_%d_%d" % (id(timeout), id(func), id(
|
||||||
|
args), id(kwargs)))), FunctionTimedOutTempType.__bases__, dict(FunctionTimedOutTempType.__dict__))
|
||||||
|
|
||||||
stopException = FunctionTimedOutTemp
|
stopException = FunctionTimedOutTemp
|
||||||
thread._stopThread(stopException)
|
thread._stopThread(stopException)
|
||||||
@ -168,21 +167,21 @@ def func_set_timeout(timeout, allowOverride=False):
|
|||||||
# Helps closure issue on some versions of python
|
# Helps closure issue on some versions of python
|
||||||
defaultTimeout = copy.copy(timeout)
|
defaultTimeout = copy.copy(timeout)
|
||||||
|
|
||||||
isTimeoutAFunction = bool( issubclass(timeout.__class__, (types.FunctionType, types.MethodType, types.LambdaType, types.BuiltinFunctionType, types.BuiltinMethodType) ) )
|
isTimeoutAFunction = issubclass(timeout.__class__, (types.FunctionType, types.MethodType,
|
||||||
|
types.LambdaType, types.BuiltinFunctionType, types.BuiltinMethodType))
|
||||||
|
|
||||||
if not isTimeoutAFunction:
|
if not isTimeoutAFunction and not issubclass(timeout.__class__, (float, int)):
|
||||||
if not issubclass(timeout.__class__, (float, int)):
|
|
||||||
try:
|
try:
|
||||||
timeout = float(timeout)
|
timeout = float(timeout)
|
||||||
except:
|
except:
|
||||||
raise ValueError('timeout argument must be a float/int for number of seconds, or a function/lambda which gets passed the function arguments and returns a calculated timeout (as float or int). Passed type: < %s > is not of any of these, and cannot be converted to a float.' %( timeout.__class__.__name__, ))
|
raise ValueError(
|
||||||
|
f'timeout argument must be a float/int for number of seconds, or a function/lambda which gets passed the function arguments and returns a calculated timeout (as float or int). Passed type: < {timeout.__class__.__name__} > is not of any of these, and cannot be converted to a float.')
|
||||||
|
|
||||||
if not allowOverride and not isTimeoutAFunction:
|
if not allowOverride and not isTimeoutAFunction:
|
||||||
# Only defaultTimeout provided. Simple function wrapper
|
# Only defaultTimeout provided. Simple function wrapper
|
||||||
def _function_decorator(func):
|
def _function_decorator(func):
|
||||||
|
|
||||||
return wraps(func)(lambda *args, **kwargs : func_timeout(defaultTimeout, func, args=args, kwargs=kwargs))
|
return wraps(func)(lambda *args, **kwargs: func_timeout(defaultTimeout, func, args=args, kwargs=kwargs))
|
||||||
|
|
||||||
# def _function_wrapper(*args, **kwargs):
|
# def _function_wrapper(*args, **kwargs):
|
||||||
# return func_timeout(defaultTimeout, func, args=args, kwargs=kwargs)
|
# return func_timeout(defaultTimeout, func, args=args, kwargs=kwargs)
|
||||||
@ -203,7 +202,6 @@ def func_set_timeout(timeout, allowOverride=False):
|
|||||||
return wraps(func)(_function_wrapper)
|
return wraps(func)(_function_wrapper)
|
||||||
return _function_decorator
|
return _function_decorator
|
||||||
|
|
||||||
|
|
||||||
# At this point, timeout IS known to be a function.
|
# At this point, timeout IS known to be a function.
|
||||||
timeoutFunction = timeout
|
timeoutFunction = timeout
|
||||||
|
|
||||||
@ -230,6 +228,7 @@ def func_set_timeout(timeout, allowOverride=False):
|
|||||||
return func_timeout(useTimeout, func, args=args, kwargs=kwargs)
|
return func_timeout(useTimeout, func, args=args, kwargs=kwargs)
|
||||||
|
|
||||||
return wraps(func)(_function_wrapper)
|
return wraps(func)(_function_wrapper)
|
||||||
|
|
||||||
return _function_decorator
|
return _function_decorator
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user