When file-like objects aren't file-like enough for Windows

W

William McBrine

This is proving to be a recurring problem for me.

First, I used the save() method of a Python Imaging Library "Image"
object to write directly to the "wfile" of a BaseHTTPRequestHandler-
derived class:

pic.save(self.wfile, 'JPEG')

Worked great in Linux, barfed in Windows. I had to do this to get around
it:

out = StringIO()
pic.save(out, 'JPEG')
encoded = out.getvalue()
self.wfile.write(encoded)

Now, I have a similar problem with subprocess.Popen... The code that
works in Linux looks like this:

source = urllib.urlopen(url)
child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=source)
try:
shutil.copyfileobj(child.stdout, self.wfile)
except:
kill(child.pid)

But wfile isn't the problem this time; instead, it's the source:

....
child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=source)
File "C:\Python25\lib\subprocess.py", line 586, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "C:\Python25\lib\subprocess.py", line 698, in _get_handles
p2cread = msvcrt.get_osfhandle(stdin.fileno())
IOError: [Errno 9] Bad file descriptor

How can I get around this, short of resorting to copying all of the input
before passing it to the child?
 
T

Tim Golden

William said:
Now, I have a similar problem with subprocess.Popen... The code that
works in Linux looks like this:

source = urllib.urlopen(url)
child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=source)
try:
shutil.copyfileobj(child.stdout, self.wfile)
except:
kill(child.pid)

But wfile isn't the problem this time; instead, it's the source:

...
child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=source)
File "C:\Python25\lib\subprocess.py", line 586, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "C:\Python25\lib\subprocess.py", line 698, in _get_handles
p2cread = msvcrt.get_osfhandle(stdin.fileno())
IOError: [Errno 9] Bad file descriptor

How can I get around this, short of resorting to copying all of the input
before passing it to the child?

It looks like you're stuck, I'm afraid. Basically you're
falling foul of a documented limitation of the underlying
socket file-likeness whose fileno () under Windows "cannot be
used where a file descriptor can be used (such as os.fdopen())"

I doubt you have any choice but to channel your urlopen
data through some real file so it can make the stdin of
the external process.

TJG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top