from functools import wraps
class _InterruptHandler(object):
def __init__(self, default_handler):
self.original_handler = default_handler
if isinstance(default_handler, int):
if default_handler == signal.SIG_DFL:
# Pretend it's signal.default_int_handler instead.
default_handler = signal.default_int_handler
elif default_handler == signal.SIG_IGN:
# Not quite the same thing as SIG_IGN, but the closest we
# can make it: do nothing.
def default_handler(unused_signum, unused_frame):
raise TypeError("expected SIGINT signal handler to be "
"signal.SIG_IGN, signal.SIG_DFL, or a "
self.default_handler = default_handler
def __call__(self, signum, frame):
installed_handler = signal.getsignal(signal.SIGINT)
if installed_handler is not self:
# if we aren't the installed handler, then delegate immediately
self.default_handler(signum, frame)
self.default_handler(signum, frame)
for result in _results.keys():
_results = weakref.WeakKeyDictionary()
def registerResult(result):
def removeResult(result):
return bool(_results.pop(result, None))
_interrupt_handler = None
global _interrupt_handler
if _interrupt_handler is None:
default_handler = signal.getsignal(signal.SIGINT)
_interrupt_handler = _InterruptHandler(default_handler)
signal.signal(signal.SIGINT, _interrupt_handler)
def removeHandler(method=None):
def inner(*args, **kwargs):
initial = signal.getsignal(signal.SIGINT)
return method(*args, **kwargs)
signal.signal(signal.SIGINT, initial)
global _interrupt_handler
if _interrupt_handler is not None:
signal.signal(signal.SIGINT, _interrupt_handler.original_handler)