[Tutor] how popen works?

Alan Gauld alan.gauld at blueyonder.co.uk
Thu Jul 22 23:32:55 CEST 2004


> While we're talking about popen, I'll ask you about a trouble I'm
> having with it.  According to the documentation, you can only tell
if
> the command failed when you close the pipe.  If it failed, it
returns
> a tuple, otherwise it returns None.

You can usually tell by looking at the output returned by read()
but the close() function returns the formal exitcode of the command
- the same status value you get from os.system()

The current website documentation says:
'''
The exit status of the command (encoded in the format specified for
wait()) is available as the return value of the close() method
'''

And about wait() it says:

'''
return a tuple containing its pid and
exit status indication: a 16-bit number,
whose low byte is the signal number that killed the process,
and whose high byte is the exit status (if the signal number is zero);
the high bit of the low byte is set if a core file was produced.
Availability: Unix.
'''

Phew!

So the exit status consists of a 16 bit int (not a tuple
- thats what wait() returns) the high byte being the exit
status from the process.

You can extract the high byte by dividing by 256 or right
shifting 8 bits using the >> operator.

> And I found those numbers by experimenting.

The numbers should be documented by the program that you run
- so look in the EXIM docs. But the EXIM numbers will only
correspond after you do the decoding described above...

> I wonder what I'm missing?

Hopefully that explains it a bit better?

Alan G.

PS You might find it easier to use popen2 or popen3 and monitor
the stderr stream...

Alan G.



More information about the Tutor mailing list