Why does pressing Ctrl-backslash result in core dump?
Asked Answered
D

2

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.

Depilatory answered 8/10, 2013 at 13:7 Comment(5)
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.Fulllength
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.Hexastich
That's weird, because it doesn't happen when I'm in nano or vim for instance.Depilatory
Nano and Vim take special care to intercept ^C, ^Z and ^\. Most programs accept the default behavior, as Python does.Kirksey
It breaks sudo on my machine because quitting ipython this way changes the output of sttyBigmouth
D
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.

Depilatory answered 23/11, 2013 at 12:52 Comment(0)
M
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
Maidinwaiting answered 8/10, 2013 at 13:17 Comment(1)
while this works for the python shell, ipython still quits even though quit has been undefedRh
D
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.

Depilatory answered 23/11, 2013 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.