Blocking ctrl-c to os.popen() or os.system()

Donn Cave donn at drizzle.com
Sun Jan 5 20:15:32 CET 2003


Quoth Erik Max Francis <max at alcyone.com>:
...
| The problem here (which other responders failed to identify) is that
| it's the spawned program that's getting the INT signal, not the Python
| program launching it.  Signals are really independent of stdin/stdout,
| so that's why your redirection and file closing isn't helping.

True.  It is related to terminal I/O, but not directly enough to depend
on the actual open files.  Instead, it's inherited from tty processes
through the terminal process group, in systems that use Berkeley job
control (i.e., all modern UNIX & clones like Linux.)

When system or popen starts a new process, it inherits the caller's
process group, and if the caller is subject to signals from the
terminal driver like <ctrl>C, then so will the new process.  (This
is also true of processes started in a shell script, incidentally,
so I think you will find that "trap" won't really save them.)

posix.setpgrp() establishes a new process group.  It has to be invoked
from the child fork, so one would need to do something like copy
os.spawnv or popen2.Popen* and modify it accordingly.

	Donn Cave, donn at drizzle.com




More information about the Python-list mailing list