[Python-ideas] Utility to override custom signal handlers

Demian Brecht demianbrecht at gmail.com
Fri Oct 9 22:21:12 CEST 2015


Sometimes it's useful to be able to reset a select number of
overridden signal handlers temporarily to allow for default handling.
While trivial, might there be use for such a utility existing in the
signal module? Something to the effect of:


@contextmanager
def default_sighandlers(*signums):
    handlers = []
    for signum in signums:
        handlers.append((signum, signal.getsignal(signum)))
        signal.signal(
            signum,
            signal.SIG_DFL if signum != signal.SIGINT
            else signal.default_int_handler)

    try:
        yield
    finally:
        for signum, handler in handlers:
            signal.signal(signum, handler)


Thoughts?


More information about the Python-ideas mailing list