import code
import signal
import traceback


def debug(sig, frame):
    """Interrupt running process, and provide a python prompt for
    interactive debugging."""
    d = {'_frame': frame}         # Allow access to frame object.
    d.update(frame.f_globals)  # Unless shadowed by global
    d.update(frame.f_locals)

    i = code.InteractiveConsole(d)
    message = "Signal received : entering python shell.\nTraceback:\n"
    message += ''.join(traceback.format_stack(frame))
    i.interact(message)


def print_stack_on_signal():
    # signal.signal(signal.SIGUSR1, debug)  # Register handler
    signal.signal(signal.SIGINT, debug)  # Register handler
