why does popen2 silently ignore IOError?
donn at u.washington.edu
Thu Feb 21 13:57:20 EST 2002
Quoth Graham Guttocks <graham_guttocks at yahoo.co.nz>:
| "Donn Cave" <donn at u.washington.edu> wrote:
| > in the general sense that it does not raise an exception in the case
| > of a non-zero exit from the child process.
| > I personally prefer to get an exception, with value from stderr
| > output
| This was what I was looking for. It sounds like it isn't possible.
| I thought what I wanted was simple. I'm sending mail by opening a
| pipe to /usr/sbin/sendmail -- if that fails for whatever reason (e.g,
| sendmail program is missing) I want an exception raised with some
| indication of the problem, instead of the current silent failure.
| Is there any way to do this in Python? Any example code to get me
It can be done in Python.
- Look at popen2. Use the Popen3 class directly, and trap stderr.
- Write email to tochild file object.
- Read from the childerr object. Your program will block until exit.
- Get exit status from the wait() member function.
- For extra credit, process as os.spawnv does to get the integer exit
- If status is non-zero, raise exception with data from childerr.
But I didn't write that out as sample code, because there's a big
problem with it as an algorithm: The read and write steps are in
principle concurrent. How you solve this is up to you. Maybe it
isn't even a problem - if you do it in the above order, you may
be delivered a SIGPIPE, or who knows depending on platform, but I'm
pretty sure it wouldn't just block forever. If there might be input
on the fromchild object, you have another similar problem - if that
fills up because you're looking at childerr, you have a deadlock.
Donn Cave, donn at u.washington.edu
More information about the Python-list