Issue #2 (Resolve) - Ensure we do not lose function information when using the func_set_timeout wrapper.

This includes things like like name, docstring, etc) when using the func_set_timeout wrapper.

This resolves using functools.wrap standard library function.

Includes unit test (failing before this commit, passing after)

Thanks to belongwqz for noting the issue and suggesting functools.wrap as resolution.
This commit is contained in:
Tim Savannah 2017-08-10 18:46:14 -04:00
parent 2bd33c12fd
commit 00685ec546
2 changed files with 45 additions and 4 deletions

View File

@ -25,6 +25,8 @@ except SyntaxError:
except ImportError:
from .py2_raise import raise_exception
from functools import wraps
__all__ = ('func_timeout', 'func_set_timeout')
@ -180,7 +182,7 @@ def func_set_timeout(timeout, allowOverride=False):
# Only defaultTimeout provided. Simple function wrapper
def _function_decorator(func):
return 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):
# return func_timeout(defaultTimeout, func, args=args, kwargs=kwargs)
@ -198,7 +200,7 @@ def func_set_timeout(timeout, allowOverride=False):
return func_timeout(useTimeout, func, args=args, kwargs=kwargs)
return _function_wrapper
return wraps(func)(_function_wrapper)
return _function_decorator
@ -217,7 +219,7 @@ def func_set_timeout(timeout, allowOverride=False):
return func_timeout(useTimeout, func, args=args, kwargs=kwargs)
return _function_wrapper
return wraps(func)(_function_wrapper)
return _function_decorator
# Cannot override, and calculate timeout function
@ -227,7 +229,7 @@ def func_set_timeout(timeout, allowOverride=False):
return func_timeout(useTimeout, func, args=args, kwargs=kwargs)
return _function_wrapper
return wraps(func)(_function_wrapper)
return _function_decorator

View File

@ -394,6 +394,45 @@ class TestDecorator(object):
assert threadsCleanedUp , 'Expected other threads to get cleaned up after gc collection'
def test_nameRetained(self):
# Case of just timeout
@func_set_timeout(2, allowOverride=False)
def hello():
pass
assert hello.__name__ == 'hello'
del hello
def getTimeoutFunc():
return 2
# Timeout is function
@func_set_timeout(getTimeoutFunc, allowOverride=False)
def hello2():
pass
assert hello2.__name__ == 'hello2'
del hello2
# Now the same with allowOverride=True
@func_set_timeout(2, allowOverride=True)
def hello3():
pass
assert hello3.__name__ == 'hello3'
del hello3
@func_set_timeout(getTimeoutFunc, allowOverride=True)
def hello4():
pass
assert hello4.__name__ == 'hello4'
if __name__ == '__main__':