From f20e8e727c8a059abfa7a93d6bd72468bff35c40 Mon Sep 17 00:00:00 2001 From: Tim Savannah Date: Sat, 20 May 2017 21:14:58 -0400 Subject: [PATCH] Fix so instantiation works in async exception. Also, dont need to worry about the exception comment, as we use BaseException and have for a while to prevent such. --- func_timeout/dafunc.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/func_timeout/dafunc.py b/func_timeout/dafunc.py index 524f758..03d2b23 100644 --- a/func_timeout/dafunc.py +++ b/func_timeout/dafunc.py @@ -41,17 +41,6 @@ def func_timeout(timeout, func, args=(), kwargs=None): but will not block the calling thread (a new thread will be created to perform the join). If possible, you should try/except FunctionTimedOut to return cleanly, but in most cases it will 'just work'. - Be careful of code like: - - def myfunc(): - while True: - try: - dosomething() - except Exception: - continue - - because it will never terminate. - @return - The return value that #func# gives ''' @@ -84,7 +73,14 @@ def func_timeout(timeout, func, args=(), kwargs=None): stopException = None if thread.isAlive(): isStopped = True - stopException = FunctionTimedOut + + class FunctionTimedOutTempType(FunctionTimedOut): + def __init__(self): + 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__)) + + stopException = FunctionTimedOutTemp thread._stopThread(stopException) thread.join(min(.1, timeout / 50.0)) raise FunctionTimedOut('', timeout, func, args, kwargs)