pnpnpn / timeout-decorator

Timeout decorator for Python
MIT License
628 stars 94 forks source link

Python 3 support #2

Closed deeplook closed 9 years ago

deeplook commented 10 years ago

A suggestion to make this Python 3 compliant, note the test for PY_VERSION:

import sys
PY_VERSION = sys.version_info[0]
# ...
def timeout(seconds_before_timeout):
    def decorate(f):
        def handler(signum, frame):
            raise TimeoutError()
        def new_f(*args, **kwargs):
            old = signal.signal(signal.SIGALRM, handler)
            signal.alarm(seconds_before_timeout)
            try:
                result = f(*args, **kwargs)
            finally:
                signal.signal(signal.SIGALRM, old)
            signal.alarm(0)
            return result
        if PY_VERSION == 2:
            new_f.func_name = f.func_name
        elif PY_VERSION == 3:
            new_f.func_name = f.__name__
        return new_f
    return decorate
pnpnpn commented 10 years ago

Can you make a pull request? Thanks!

deeplook commented 10 years ago

Sorry, too busy...

toddsifleet commented 10 years ago

This issue was resolved by #5