def timeout(limit, handler):
def wrapper(f):<EOL><INDENT>def wrapped_f(*args, **kwargs):<EOL><INDENT>old_handler = signal.getsignal(signal.SIGALRM)<EOL>signal.signal(signal.SIGALRM, timeout_handler)<EOL>signal.alarm(limit)<EOL>try:<EOL><INDENT>res = f(*args, **kwargs)<EOL><DEDENT>except Timeout:<EOL><INDENT>handler(limit, f, args, kwargs)<EOL><DEDENT>else:<EOL><INDENT>return res<EOL><DEDENT>finally:<EOL><INDENT>signal.signal(signal.SIGALRM, old_handler)<EOL>signal.alarm(<NUM_LIT:0>)<EOL><DEDENT><DEDENT>return wrapped_f<EOL><DEDENT>return wrapper<EOL>
A decorator ensuring that the decorated function tun time does not
    exceeds the argument limit.

    :args limit: the time limit
    :type limit: int

    :args handler: the handler function called when the decorated
    function times out.
    :type handler: callable

    Example:
    >>>def timeout_handler(limit, f, *args, **kwargs):
    ...     print "{func} call timed out after {lim}s.".format(
    ...         func=f.__name__, lim=limit)
    ...
    >>>@timeout(limit=5, handler=timeout_handler)
    ... def work(foo, bar, baz="spam")
    ...     time.sleep(10)
    >>>work("foo", "bar", "baz")
    # time passes...
    work call timed out after 5s.
    >>>