Decorating a timeout function
1. Review


There are no docstrings. What do these functions do? How am I supposed to use them?
The use of the alarm mechanism means that the timeout occurs asynchronously with respect to the timed out function, and so data structures may be left in an inconsistent state. It's important to warn the user about this: it's hard to write functions that are safe against timeouts of this kind.
The functions and classes could have better names: I suggest TimedOutExc → TimedOut (it's clear that it's an exception because of its superclass); TimedOutFn → call_with_timeout (following PEP8); timed_out → with_timeout.
There's no need to give the exception class its own __init__ and __str__ method: the Exception class already has these methods.
The implementation of the timed_out decorator is almost identical to TimedOutFn. It would be better for new_f to call TimedOutFn instead of repeating the code.
The decorator copies __name__ from the original function, but what about __doc__ and __module__ and __qualname__? It would be better to use the built-in functools.wraps.
I think it's slightly better for call_with_timeout to take the timeout argument first. That way the function and its arguments are adjacent.
In call_with_timeout, there are two state changes that need to be reverted: the updating of the signal handler, and the setting of the alarm. But only one of these is protected by a try: ... finally: ....
There's a lot of duplicated code in the test cases. This could be refactored into methods.
The test cases don't actually tell you whether the tests passed or failed (you have to know what the output is supposed to be in each case). It would be a good idea to use the facilities in the unittest module.


2. Revised code

import functools
import signal

class TimedOut(Exception):
    pass

def call_with_timeout(timeout, f, *args, **kwargs):
    """Call f with the given arguments, but if timeout seconds pass before
    f returns, raise TimedOut. The exception is raised asynchronously,
    so data structures being updated by f may be in an inconsistent state.

    """
    def handler(signum, frame):
        raise TimedOut("Timed out after {} seconds.".format(timeout))
    old = signal.signal(signal.SIGALRM, handler)
    try:
        signal.alarm(timeout)
        try:
            return f(*args, **kwargs)
        finally:
            signal.alarm(0)
    finally:
        signal.signal(signal.SIGALRM, old)

def with_timeout(timeout):
    """Decorator for a function that causes it to timeout after the given
    number of seconds.

    """
    def decorator(f):
        @functools.wraps(f)
        def wrapped(*args, **kwargs):
            return call_with_timeout(timeout, f, *args, **kwargs)
        return wrapped
    return decorator