diff --git a/func_timeout/dafunc.py b/func_timeout/dafunc.py index e22bd22..c44f0d0 100644 --- a/func_timeout/dafunc.py +++ b/func_timeout/dafunc.py @@ -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 diff --git a/tests/FuncTimeoutTests/test_Decorator.py b/tests/FuncTimeoutTests/test_Decorator.py index e60d379..a14ecee 100755 --- a/tests/FuncTimeoutTests/test_Decorator.py +++ b/tests/FuncTimeoutTests/test_Decorator.py @@ -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__':