Skip to content

Commit

Permalink
Use functools.wraps in decorator
Browse files Browse the repository at this point in the history
* This will ensure that the func_name/doc strings is correct across py versions
* Fixes issue pnpnpn#2
  • Loading branch information
toddsifleet committed Oct 16, 2014
1 parent e54bea9 commit e7dfe05
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 e7dfe05

Please sign in to comment.