<s> import signal <EOL> import weakref <EOL> from combi . _python_toolbox . third_party . unittest2 . compatibility import wraps <EOL> __unittest = True <EOL> class _InterruptHandler ( object ) : <EOL> def __init__ ( self , default_handler ) : <EOL> self . called = False <EOL> self . original_handler = default_handler <EOL> if isinstance ( default_handler , int ) : <EOL> if default_handler == signal . SIG_DFL : <EOL> default_handler = signal . default_int_handler <EOL> elif default_handler == signal . SIG_IGN : <EOL> def default_handler ( unused_signum , unused_frame ) : <EOL> pass <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . default_handler = default_handler <EOL> def __call__ ( self , signum , frame ) : <EOL> installed_handler = signal . getsignal ( signal . SIGINT ) <EOL> if installed_handler is not self : <EOL> self . default_handler ( signum , frame ) <EOL> if self . called : <EOL> self . default_handler ( signum , frame ) <EOL> self . called = True <EOL> for result in _results . keys ( ) : <EOL> result . stop ( ) <EOL> _results = weakref . WeakKeyDictionary ( ) <EOL> def registerResult ( result ) : <EOL> _results [ result ] = <NUM_LIT:1> <EOL> def removeResult ( result ) : <EOL> return bool ( _results . pop ( result , None ) ) <EOL> _interrupt_handler = None <EOL> def installHandler ( ) : <EOL> global _interrupt_handler <EOL> if _interrupt_handler is None : <EOL> default_handler = signal . getsignal ( signal . SIGINT ) <EOL> _interrupt_handler = _InterruptHandler ( default_handler ) <EOL> signal . signal ( signal . SIGINT , _interrupt_handler ) <EOL> def removeHandler ( method = None ) : <EOL> if method is not None : <EOL> @ wraps ( method ) <EOL> def inner ( * args , ** kwargs ) : <EOL> initial = signal . getsignal ( signal . SIGINT ) <EOL> removeHandler ( ) <EOL> try : <EOL> return method ( * args , ** kwargs ) <EOL> finally : <EOL> signal . signal ( signal . SIGINT , initial ) <EOL> return inner <EOL> global _interrupt_handler <EOL> if _interrupt_handler is not None : <EOL> signal . signal ( signal . SIGINT , _interrupt_handler . original_handler ) </s>
