Skip to content

Instantly share code, notes, and snippets.

@leorochael
Last active August 8, 2019 18:17
Show Gist options
  • Save leorochael/40871ab213fbc62e1e2f6453cb240161 to your computer and use it in GitHub Desktop.
Save leorochael/40871ab213fbc62e1e2f6453cb240161 to your computer and use it in GitHub Desktop.
Python snippet to add a SIGINT signal handler (i.e. CTRL+C) that prints a traceback and offers to start pdb
import signal
def may_debug_handler(sig, frame):
# allow double CTRL+C to really break interrupt:
signal.signal(signal.SIGINT, signal.default_int_handler)
c = input('(d)ebug? (i)nterrupt? ')
print 'got:', c
if c == 'd':
breakpoint()
elif c == 'i':
return signal.default_int_handler(signal, frame)
# restore this handler:
signal.signal(signal.SIGINT, may_debug_handler)
signal.signal(signal.SIGINT, may_debug_handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment