24

When I'm in a python application (the python shell, for instance), pressing ctrl + \ results in

>>> Quit (core dumped)

Why is this, and how can I avoid this? It is very inconvenient if application bails out whenever I press ctrl + \ by accident.

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
chtenb
  • 14,924
  • 14
  • 78
  • 116
  • For me it only quits, but does not dump. I don't know why it quits in the first place, but that it dumps core might indicate a problem with your python installation. – Waldheinz Oct 08 '13 at 13:10
  • 9
    `CTRL + \ ` is a default shortcut for sending `SIGQUIT` to foreground process. By default, `SIGQUIT` causes a core dump. More in `man kill`. If you wish, you can remove the shortcut from your terminal perferences. – Maciej Gol Oct 08 '13 at 13:17
  • That's weird, because it doesn't happen when I'm in nano or vim for instance. – chtenb Oct 08 '13 at 13:24
  • 2
    Nano and Vim take special care to intercept ^C, ^Z and ^\. Most programs accept the default behavior, as Python does. – Robᵩ Oct 08 '13 at 14:36
  • It breaks `sudo` on my machine because quitting `ipython` this way changes the output of `stty` – darw Apr 16 '22 at 17:53

2 Answers2

40

CTRL-\ is the Linux key that generates the QUIT signal. Generally, that signal causes a program to terminate and dump core. This is a feature of UNIX and Linux, wholly unrelated to Python. (For example, try sleep 30 followed by CTRL-\.)

If you want to disable that feature, use the stty command.

From the Linux command line, before Python starts:

stty quit undef
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • 1
    while this works for the python shell, ipython still quits even though `quit` has been `undef`ed – zeawoas Apr 22 '21 at 09:42
14

The python module signal is convenient to deal with this.

import signal

# Intercept ctrl-c, ctrl-\ and ctrl-z
def signal_handler(signal, frame):
    pass
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGQUIT, signal_handler)
signal.signal(signal.SIGTSTP, signal_handler)

Just add handlers to the signal that (in this case) do nothing.

chtenb
  • 14,924
  • 14
  • 78
  • 116