Ctrl-C forwarded to command executed via popen on Unix
Eric Brunel
eric.brunel at pragmadev.com
Thu May 23 13:02:12 EDT 2002
Hi all,
We've just noticed a very strange problem regarding the use of the Ctrl-C
key in the Python interpreter when a command is being run through a pipe on
Unix: the interpreter gets it, raising a KeyboardInterrupt exception as it
should, but the running command also seems to get the interrupt!
Here is a small example involving a small interrupt handling C program:
--- handleCtrlC.c ----------------------------
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void interruptHandler(int ignored)
{
printf("Ctrl-C caught!\n");
fflush(stdout);
}
int main()
{
signal(SIGINT, interruptHandler);
while ( 1 )
{
printf("Tick\n");
fflush(stdout);
sleep(1);
}
}
-----------------------------------------------
I compiled this program to handleCtrlC, then ran the Python interpreter and
did:
>>> import os
>>> p = os.popen('./handleCtrlC')
>>> p.readline()
'Tick\n'
After a few readline's, I press Ctrl-C at the prompt; the interpreter
answers as expected:
>>>
KeyboardInterrupt
Then I continue the readline's: they answer 'Tick\n' a few times, then
'Ctrl-C caught!\n', which seems to indicate that the signal went along the
pipe to the executed program?! [NB: if you try this, you may have to kill
the interpreter, as the pipe won't close since there's always input on
it...]
I tried this with Python 2.1 on Mandrake Linux 8.0 and Solaris 7, and the
result is the same. Is it a feature or a bug? If it's a feature, what is
the reason behind it? Or maybe it is the standard behaviour of popen?
TIA
--
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
More information about the Python-list
mailing list