diff -r 768234f5c246 Lib/test/support.py --- a/Lib/test/support.py Sat Jun 25 15:03:52 2011 +0200 +++ b/Lib/test/support.py Sat Jun 25 16:48:41 2011 +0200 @@ -1601,8 +1601,31 @@ def patch(test_instance, object_to_patch setattr(object_to_patch, attr_name, old_value) else: delattr(object_to_patch, attr_name) test_instance.addCleanup(cleanup) # actually override the attribute setattr(object_to_patch, attr_name, new_value) + +@contextlib.contextmanager +def operation_timeout(timeout): + """A context manager that raise IOError if the operation didn't finish + before the given `timeout`. + + Example: + >>> import os, time + >>> with operation_timeout(1): time.sleep(5) + Traceback (most recent call last): + ... + IOError: Timeout exceeded! + >>> with operation_timeout(1): pass + + """ + import signal + def handler(signum, frame): + raise IOError("Timeout exceeded!") + + signal.signal(signal.SIGALRM, handler) + signal.alarm(5) + yield + signal.alarm(0)