Python 2.2.1: os.popen() bug?

Donn Cave donn at u.washington.edu
Tue May 28 12:35:25 EDT 2002


Quoth Carsten Gaebler <clpy at snakefarm.org>:

| If I write to a pipe that was opened read-only, python throws an
| exception, but then the interpreter hangs instead of exiting. Is this
| beavior expected or a bug? Consider this example on Linux:
|
| #! /usr/bin/python
|
| import os
|
| pipe = os.popen("mail -s Test foo at bar.org")
| print >> pipe, "Test"  # Raises IOError.
| pipe.close()           # Never reached.

I think you'll find that the same happens even if you don't write
to it.

On exit, Python cleans up everything, and its cleanup for your "pipe"
file object eventually calls pclose(3).  That function closes the
pipe, and then waits for the process to exit so it can return the
exit status.  That works -- if the process on the other end is
actually going to use the pipe.  If you had supplied the 'w' option,
mail would have seen EOF at that point, and would have gone on to
send the mail and exit.  If it had been for some reason writing to
output, it would have gotten a SIGPIPE.  But it doesn't write to
output, it just sits there and waits for input.  And Python's cleanup
procedure waits for it.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list