Title: subprocess.Popen.stdout.flush fails os OS-X 10.6.1
Type: crash Stage:
Components: Library (Lib), macOS Versions: Python 2.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: ronaldoussoren Nosy List: ned.deily, petegibson, pjenvey, ronaldoussoren
Priority: normal Keywords:

Created on 2009-10-30 05:15 by petegibson, last changed 2009-11-03 20:57 by pjenvey. This issue is now closed.

Messages (5)
msg94694 - (view) Author: Peter Gibson (petegibson) Date: 2009-10-30 05:15
subprocess.Popen.stdout.flush() fails on OS-X 10.6.1 under the bundled
Python 2.6.1 and 2.6.3 from Macports.

>>> from subprocess import Popen, PIPE
>>> p = Popen('cat', stdin=PIPE, stdout=PIPE)
>>> p.stdout.flush()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 9] Bad file descriptor

However it works on Python 2.6.2 on Linux.
msg94695 - (view) Author: Philip Jenvey (pjenvey) * (Python committer) Date: 2009-10-30 06:23
Why are you flushing stdout? It's read-only and flush is for writing. This 
behavior is dependent on the underlying platform's fflush, which really 
*should* be raising EBADF when fflushing a read only file, anyway
msg94696 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2009-10-30 06:26
Philip is correct:

>>> p.stdout.flush()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 9] Bad file descriptor
>>> p.stdout
<open file '<fdopen>', mode 'rb' at 0x100527470>

You'll get the same error on OS X (at least as far back as Python 2.3.5 
on OS X 10.4) if you try to flush a disk read-only file:

>>> f = open('a.txt', 'rb')
>>> f.flush()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IOError: [Errno 9] Bad file descriptor

Note, both the OS X and Linux 2.6 fflush(3) man pages clearly state that 
EBADF can be returned if the stream is not open for writing but there 
seems to be a difference in behavior between the two OS's.

As this doesn't seem to be a new issue and can easily be avoided (don't 
flush a read-only file), I suggest closing the issue.
msg94705 - (view) Author: Peter Gibson (petegibson) Date: 2009-10-30 13:42
Not my code, but as it's using a pipe to communicate with another
process, I assume that the flush call is intended to discard any
unwanted output prior to sending a command and processing the result.

Is there another way to achieve the same effect, such as reading and
remaining characters in the buffer?
msg94858 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2009-11-03 07:52
I don't think so. stdio on OSX has a fdiscard function, but that's not 
exposed to Python.

I tend to explicitly synchronize on prompts when communicating with an 
interactive program over a pipe. That is, read until you found the prompt, 
then send a command, then read again until getting to the prompt, ...
Date User Action Args
2009-11-03 20:57:24pjenveysetstatus: open -> closed
resolution: not a bug
2009-11-03 07:52:39ronaldoussorensetmessages: + msg94858
2009-10-30 13:42:27petegibsonsetmessages: + msg94705
2009-10-30 06:26:13ned.deilysetnosy: + ned.deily, ronaldoussoren
messages: + msg94696

assignee: ronaldoussoren
components: + macOS
2009-10-30 06:23:15pjenveysetnosy: + pjenvey
messages: + msg94695
2009-10-30 05:15:47petegibsoncreate