all Python 3signalfunction
signal.default_int_handler()
handlers
The default SIGINT handler: it raises KeyboardInterrupt. Reinstall it to restore the usual reaction to Ctrl+C.
Syntax
signal.signal(signal.SIGINT, signal.default_int_handler)
Restoring Ctrl+C with SIG_DFL is a common mistake: SIG_DFL means the OS default, i.e. the process dies at once with no KeyboardInterrupt, no finally and no with. The right value to restore is signal.default_int_handler. If the parent process started Python with SIGINT ignored, the interpreter does not install this handler and Ctrl+C does nothing at all.
Examples
import signal
signal.signal(signal.SIGINT, signal.SIG_IGN) # временно глушим Ctrl+C
signal.signal(signal.SIGINT, signal.default_int_handler) # а не SIG_DFL
try:
signal.raise_signal(signal.SIGINT)
except KeyboardInterrupt:
print('KeyboardInterrupt') # → KeyboardInterrupt
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy