Re: Keyboard Interrupts

Guido.van.Rossum@cwi.nl
Sun, 26 Feb 1995 11:11:34 +0100

> glenc@netcom.com (Glen Collins) writes:
> | I would like to know if there is is a way to specify in python to
> | ignore keyboard interrupts(^C). I would like to do this a the very
> | beginning of the script. There are thress way I can see of doing
> | this, but I would prefer to do it within python.
> |
> | #1. Us try/except statements throughout the script (too many!)
> | #2. Patch python to ignore all SIGINT's
> | #3. Compile python with a "SIGNAL" type of library able to use include
> | statements.

donn@u.washington.edu (Donn Cave):
> #4. Change the terminal driver setting for the INTR key. Problems:
> a. Not specific to python process, so it has to be restored afterwards.
> b. No python function (that I know of) to do it, though not much work
> to invoke stty. A POSIX termios module would be doable.
> c. UNIX only, though other systems may have equivalent.

Actually, Glenn can solve his problem with the signal module -- this
is present though undocumented in 1.1(.1) and will be a first-class
citizen in 1.2. Try this:

import signal
signal.signal(signal.SIGINT, signal.SIG_IGN)

The signal module is not intended to be UNIX specific but I haven't
heard whether it can be used on non-UNIX systems -- most Mac/PC
compilers I know of don't support signal() in a way that makes it
possible to use the signal module (it requires that signal handlers
really run asynchronously).

Note that there's also a termios module (again undocumented) in 1.1,
so you could also implement Donn's suggestion.

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>