Skip to content

Commit

Permalink
Merge pull request #5 from toddsifleet/tws-fix-issue-2
Browse files Browse the repository at this point in the history
Use functools.wraps in decorator
  • Loading branch information
pnpnpn committed Oct 18, 2014
2 parents d13b00a + e7dfe05 commit 7f86ec1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions tests/test_timeout_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
3 changes: 2 additions & 1 deletion timeout_decorator/timeout_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from __future__ import division

import signal
from functools import wraps

############################################################
# Timeout
Expand All @@ -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)

Expand All @@ -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

0 comments on commit 7f86ec1

Please sign in to comment.