Last active
August 8, 2019 18:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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