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

William McBrine wmcbrine at users.sf.net
Sun Mar 16 12:25:56 EDT 2008


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?

-- 
09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 -- pass it on



More information about the Python-list mailing list