From e7dfe05682a1070c255f272d28a14bb331978cae Mon Sep 17 00:00:00 2001 From: Todd Sifleet Date: Wed, 15 Oct 2014 19:15:47 -0700 Subject: [PATCH] Use functools.wraps in decorator * This will ensure that the func_name/doc strings is correct across py versions * Fixes issue #2 --- tests/test_timeout_decorator.py | 8 ++++++++ timeout_decorator/timeout_decorator.py | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/test_timeout_decorator.py b/tests/test_timeout_decorator.py index 143dfc8..c40c82e 100644 --- a/tests/test_timeout_decorator.py +++ b/tests/test_timeout_decorator.py @@ -37,3 +37,11 @@ def test_timeout_ok(): def f(): time.sleep(1) f() + + +def test_function_name(): + @timeout(seconds=2) + def func_name(): + pass + + assert func_name.__name__ == 'func_name' diff --git a/timeout_decorator/timeout_decorator.py b/timeout_decorator/timeout_decorator.py index 2b86bfb..e44f544 100644 --- a/timeout_decorator/timeout_decorator.py +++ b/timeout_decorator/timeout_decorator.py @@ -11,6 +11,7 @@ from __future__ import division import signal +from functools import wraps ############################################################ # Timeout @@ -32,6 +33,7 @@ def decorate(f): def handler(signum, frame): raise TimeoutError() + @wraps(f) def new_f(*args, **kwargs): old = signal.signal(signal.SIGALRM, handler) @@ -46,6 +48,5 @@ def new_f(*args, **kwargs): signal.signal(signal.SIGALRM, old) signal.alarm(0) return result - new_f.func_name = f.func_name return new_f return decorate