def sighandler(signum, handler):
    """
    Context manager to run code with UNIX signal `signum` bound to `handler`.

    The existing handler is saved upon entering the context and restored upon
    exit.

    The `handler` argument may be anything that can be passed to Python's
    `signal.signal <https://docs.python.org/2/library/signal.html#signal.signal>`_
    standard library call.
    """
    prev_handler = signal.getsignal(signum)
    signal.signal(signum, handler)
    yield
    signal.signal(signum, prev_handler)
Context manager to run code with UNIX signal `signum` bound to `handler`.

    The existing handler is saved upon entering the context and restored upon
    exit.

    The `handler` argument may be anything that can be passed to Python's
    `signal.signal <https://docs.python.org/2/library/signal.html#signal.signal>`_
    standard library call.