Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop python2 support, add py38 and py39 #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions func_timeout/dafunc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# vim: set ts=4 sw=4 expandtab :

'''
Expand All @@ -9,27 +8,25 @@
'''

import copy
import inspect
import threading
import time
import types
import sys

from .exceptions import FunctionTimedOut
from .StoppableThread import StoppableThread

try:
from .py3_raise import raise_exception
except SyntaxError:
from .py2_raise import raise_exception
except ImportError:
from .py2_raise import raise_exception

from functools import wraps

__all__ = ('func_timeout', 'func_set_timeout')


# PEP 409 - Raise with the chained exception context disabled
# This, in effect, prevents the "funcwrap" wrapper ( chained
# in context to an exception raised here, due to scope )
# Only available in python3.3+
def raise_exception(exception):
raise exception[0] from None


def func_timeout(timeout, func, args=(), kwargs=None):
'''
func_timeout - Runs the given function for up to #timeout# seconds.
Expand Down Expand Up @@ -175,7 +172,7 @@ def calculateTimeout(*args, **kwargs):
try:
timeout = float(timeout)
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:
Expand Down
4 changes: 2 additions & 2 deletions func_timeout/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ def getMsg(self):
else:
timedOutFuncName = 'Unknown Function'
if self.timedOutAfter is not None:
timedOutAfterStr = "%f" %(self.timedOutAfter, )
timedOutAfterStr = f"{self.timedOutAfter:f}"
else:
timedOutAfterStr = "Unknown"

return 'Function %s (args=%s) (kwargs=%s) timed out after %s seconds.\n' %(timedOutFuncName, repr(self.timedOutArgs), repr(self.timedOutKwargs), timedOutAfterStr)
return f'Function {timedOutFuncName} (args={repr(self.timedOutArgs)}) (kwargs={repr(self.timedOutKwargs)}) timed out after {timedOutAfterStr} seconds.\n'

def retry(self, timeout=RETRY_SAME_TIMEOUT):
'''
Expand Down
5 changes: 0 additions & 5 deletions func_timeout/py2_raise.py

This file was deleted.

7 changes: 0 additions & 7 deletions func_timeout/py3_raise.py

This file was deleted.

10 changes: 4 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
with open('README.rst', 'rt') as f:
long_description = f.read()
except Exception as e:
sys.stderr.write('Error reading from README.rst: %s\n' %(str(e),))
sys.stderr.write(f'Error reading from README.rst: {str(e)}\n')
log_description = summary

setup(name='func_timeout',
version='4.3.5',
version='4.4.0',
packages=['func_timeout'],
author='Tim Savannah',
author_email='[email protected]',
Expand All @@ -44,12 +44,10 @@
classifiers=['Development Status :: 5 - Production/Stable',
'Programming Language :: Python',
'License :: OSI Approved :: GNU Lesser General Public License v2 (LGPLv2)',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Topic :: Software Development :: Libraries :: Python Modules'
]
)
Expand Down
8 changes: 4 additions & 4 deletions testit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ def doit(howmany):
if __name__ == '__main__':

print ( "Should get return value of 23:" )
print ( "\tGot Return: %s\n" %(str(func_timeout(4, doit, args=(6,))),) )
print ( f"\tGot Return: {str(func_timeout(4, doit, args=(6,)))}\n" )

print ( "\nShould time out (exception):" )
myException = None
try:
print ("\tGot Return: %s\n" %(str(func_timeout(1, doit, kwargs={'howmany' : 16})),))
print (f"\tGot Return: {str(func_timeout(1, doit, kwargs={'howmany': 16}))}\n")
except FunctionTimedOut as e:
sys.stderr.write('\tGot Exception: %s\n' %(str(e),))
sys.stderr.write(f'\tGot Exception: {str(e)}\n')
myException = e
pass

print ( "\nRetrying with longer timeout, should get 16+17=33:" )
if myException is not None:
print ( "\nGot: %s\n" %( str(myException.retry(2.5)), ) )
print ( f"\nGot: {str(myException.retry(2.5))}\n" )
else:
sys.stderr.write('Did not get exception before?\n')
5 changes: 2 additions & 3 deletions tests/FuncTimeoutTests/TestUtils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# vim: set ts=4 sw=4 expandtab :

'''
Expand Down Expand Up @@ -93,7 +92,7 @@ def getSleepLambda(sleepTime):



return eval('''lambda a, b : int(bool(time.sleep(%f))) + a + b''' %(_sleepTime,))
return eval(f'''lambda a, b : int(bool(time.sleep({_sleepTime:f}))) + a + b''')


def getSleepLambdaWithArgs(sleepTime, args):
Expand Down Expand Up @@ -145,7 +144,7 @@ def getSleepLambdaWithArgs(sleepTime, args):


# print ( 'Function is: %s' %('''lambda %s : int(bool(time.sleep(%f))) + %s''' %(argStr, sleepTime, sumStr, ) ) )
return eval('''lambda %s : int(bool(time.sleep(%f))) + %s''' % (argStr, sleepTime, sumStr, ) )
return eval(f'''lambda {argStr} : int(bool(time.sleep({sleepTime:f}))) + {sumStr}''' )


def compareTimes(timeEnd, timeStart, cmpTime, roundTo=None, deltaFixed=.05, deltaPct=None):
Expand Down
18 changes: 9 additions & 9 deletions tests/FuncTimeoutTests/test_Basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def test_funcTimeout(self):
try:
result = func_timeout(2.5, sleepFunction, args=(5, 13))
except FunctionTimedOut as te:
raise AssertionError('Got unexpected timeout at 2.5 second timeout for 2.00 second function: %s' %(str(te),))
raise AssertionError(f'Got unexpected timeout at 2.5 second timeout for 2.00 second function: {str(te)}')

assert result == expectedResult , 'Got wrong return from func_timeout.\nGot: %s\nExpected: %s\n' %(repr(result), repr(expectedResult))
assert result == expectedResult , f'Got wrong return from func_timeout.\nGot: {repr(result)}\nExpected: {repr(expectedResult)}\n'

gotException = False
try:
Expand All @@ -53,9 +53,9 @@ def test_funcTimeout(self):
try:
result = func_timeout(2.5, sleepFunction, args=(5,), kwargs={ 'b' : 13})
except FunctionTimedOut as te:
raise AssertionError('Got unexpected timeout at 2.5 second timeout for 2.00 second function: %s' %(str(te), ))
raise AssertionError(f'Got unexpected timeout at 2.5 second timeout for 2.00 second function: {str(te)}')
except Exception as e:
raise AssertionError('Got unknown exception mixing args and kwargs: < %s > %s' %(e.__class__.__name__, str(e)))
raise AssertionError(f'Got unknown exception mixing args and kwargs: < {e.__class__.__name__} > {str(e)}')

assert result == expectedResult , 'Got wrong result when mixing args and kwargs'

Expand All @@ -76,7 +76,7 @@ def test_retry(self):
endTime = time.time()

assert gotException , 'Expected to get exception'
assert compareTimes(endTime, startTime, .8, 3, deltaFixed=.15) == 0 , 'Expected to wait .8 seconds. Was: %f - %f = %f' %(endTime, startTime, round(endTime - startTime, 3))
assert compareTimes(endTime, startTime, .8, 3, deltaFixed=.15) == 0 , f'Expected to wait .8 seconds. Was: {endTime:f} - {startTime:f} = {round(endTime - startTime, 3):f}'

gotException = False
startTime = time.time()
Expand Down Expand Up @@ -143,8 +143,8 @@ def test_exception(self):

assert gotException , 'Expected to get exception'

assert 'timed out after ' in functionTimedOut.msg , 'Expected message to be constructed. Got: %s' %(repr(functionTimedOut.msg), )
assert round(functionTimedOut.timedOutAfter, 1) == .5 , 'Expected timedOutAfter to equal timeout ( .5 ). Got: %s' %(str(round(functionTimedOut.timedOutAfter, 1)), )
assert 'timed out after ' in functionTimedOut.msg , f'Expected message to be constructed. Got: {repr(functionTimedOut.msg)}'
assert round(functionTimedOut.timedOutAfter, 1) == .5 , f'Expected timedOutAfter to equal timeout ( .5 ). Got: {str(round(functionTimedOut.timedOutAfter, 1))}'
assert functionTimedOut.timedOutFunction == sleepFunction , 'Expected timedOutFunction to equal sleepFunction'
assert functionTimedOut.timedOutArgs == (5, 19) , 'Expected args to equal (5, 19)'
assert functionTimedOut.timedOutKwargs == {} , 'Expected timedOutKwargs to equal {}'
Expand All @@ -160,7 +160,7 @@ def test_instantiateExceptionNoArgs(self):
msg2 = exc.getMsg()

except Exception as _e:
sys.stderr.write('Got unexpected exception in test_instantiateExceptionNoArgs with no arguments. %s %s\n\n' %(str(type(_e)), str(_e)))
sys.stderr.write(f'Got unexpected exception in test_instantiateExceptionNoArgs with no arguments. {str(type(_e))} {str(_e)}\n\n')
gotException = True

assert gotException is False, 'Expected to be able to create FunctionTimedOut exception without arguments.'
Expand All @@ -173,7 +173,7 @@ def test_instantiateExceptionNoArgs(self):
msg2 = str(exc.getMsg())

except Exception as _e:
sys.stderr.write('Got unexpected exception in test_instantiateExceptionNoArgs with fixed message string. %s %s\n\n' %(str(type(_e)), str(_e)))
sys.stderr.write(f'Got unexpected exception in test_instantiateExceptionNoArgs with fixed message string. {str(type(_e))} {str(_e)}\n\n')
gotException = True

assert gotException is False , 'Expected to be able to create a FunctionTimedOut exception with a fixed message.'
Expand Down
8 changes: 4 additions & 4 deletions tests/FuncTimeoutTests/test_Decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def doSleepFuncUnder(a, b):
except FunctionTimedOut as fte2:
gotException = True
except Exception as e:
raise AssertionError('Got exception trying to retry with same timeout: < %s > : %s' %(e.__name__, str(e)))
raise AssertionError(f'Got exception trying to retry with same timeout: < {e.__name__} > : {str(e)}')
endTime = time.time()

assert gotException , 'Expected to get exception with calculated same 80% timeout on retry'
Expand All @@ -345,7 +345,7 @@ def doSleepFuncUnder(a, b):
except FunctionTimedOut as fte2:
gotException = True
except Exception as e:
raise AssertionError('Got exception trying to retry with same timeout: < %s > : %s' %(e.__name__, str(e)))
raise AssertionError(f'Got exception trying to retry with same timeout: < {e.__name__} > : {str(e)}')
endTime = time.time()

assert not gotException , 'Expected to get exception with calculated 80% timeout on retry ( None ) [ No timeout ]'
Expand All @@ -361,7 +361,7 @@ def doSleepFuncUnder(a, b):
except FunctionTimedOut as fte2:
gotException = True
except Exception as e:
raise AssertionError('Got exception trying to retry with same timeout: < %s > : %s' %(e.__name__, str(e)))
raise AssertionError(f'Got exception trying to retry with same timeout: < {e.__name__} > : {str(e)}')
endTime = time.time()

assert gotException , 'Expected to get exception with calculated 80% timeout overriden by 60% timeout on retry'
Expand All @@ -375,7 +375,7 @@ def doSleepFuncUnder(a, b):
except FunctionTimedOut as fte2:
gotException = True
except Exception as e:
raise AssertionError('Got exception trying to retry with same timeout: < %s > : %s' %(e.__name__, str(e)))
raise AssertionError(f'Got exception trying to retry with same timeout: < {e.__name__} > : {str(e)}')
endTime = time.time()

assert not gotException , 'Expected to get exception with calculated 80% timeout overriden by 150% timeout on retry'
Expand Down
14 changes: 7 additions & 7 deletions tests/FuncTimeoutTests/test_StoppableThread.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def test_funcTimeout(self):
try:
result = func_timeout(1.5, sleepFunction, args=(5, 13))
except FunctionTimedOut as te:
raise AssertionError('Got unexpected timeout at 1.5 second timeout for 1.25 second function: %s' %(str(te),))
raise AssertionError(f'Got unexpected timeout at 1.5 second timeout for 1.25 second function: {str(te)}')

assert result == expectedResult , 'Got wrong return from func_timeout.\nGot: %s\nExpected: %s\n' %(repr(result), repr(expectedResult))
assert result == expectedResult , f'Got wrong return from func_timeout.\nGot: {repr(result)}\nExpected: {repr(expectedResult)}\n'

gotException = False
try:
Expand All @@ -53,9 +53,9 @@ def test_funcTimeout(self):
try:
result = func_timeout(1.5, sleepFunction, args=(5,), kwargs={ 'b' : 13})
except FunctionTimedOut as te:
raise AssertionError('Got unexpected timeout at 1.5 second timeout for 1.25 second function: %s' %(str(te), ))
raise AssertionError(f'Got unexpected timeout at 1.5 second timeout for 1.25 second function: {str(te)}')
except Exception as e:
raise AssertionError('Got unknown exception mixing args and kwargs: < %s > %s' %(e.__class__.__name__, str(e)))
raise AssertionError(f'Got unknown exception mixing args and kwargs: < {e.__class__.__name__} > {str(e)}')

assert result == expectedResult , 'Got wrong result when mixing args and kwargs'

Expand All @@ -76,7 +76,7 @@ def test_retry(self):
endTime = time.time()

assert gotException , 'Expected to get exception'
assert compareTimes(endTime, startTime, .3, 3, .15, None) == 0 , 'Expected to wait .3 seconds. Was: %f - %f = %f' %(endTime, startTime, round(endTime - startTime, 3))
assert compareTimes(endTime, startTime, .3, 3, .15, None) == 0 , f'Expected to wait .3 seconds. Was: {endTime:f} - {startTime:f} = {round(endTime - startTime, 3):f}'

gotException = False
startTime = time.time()
Expand Down Expand Up @@ -143,8 +143,8 @@ def test_exception(self):

assert gotException , 'Expected to get exception'

assert 'timed out after ' in functionTimedOut.msg , 'Expected message to be constructed. Got: %s' %(repr(functionTimedOut.msg), )
assert round(functionTimedOut.timedOutAfter, 1) == .3 , 'Expected timedOutAfter to equal timeout ( .3 ). Got: %s' %(str(round(functionTimedOut.timedOutAfter, 1)), )
assert 'timed out after ' in functionTimedOut.msg , f'Expected message to be constructed. Got: {repr(functionTimedOut.msg)}'
assert round(functionTimedOut.timedOutAfter, 1) == .3 , f'Expected timedOutAfter to equal timeout ( .3 ). Got: {str(round(functionTimedOut.timedOutAfter, 1))}'
assert functionTimedOut.timedOutFunction == sleepFunction , 'Expected timedOutFunction to equal sleepFunction'
assert functionTimedOut.timedOutArgs == (5, 19) , 'Expected args to equal (5, 19)'
assert functionTimedOut.timedOutKwargs == {} , 'Expected timedOutKwargs to equal {}'
Expand Down
Loading