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

Tim Golden mail at timgolden.me.uk
Sun Mar 16 19:42:41 CET 2008


William McBrine wrote:
> 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



More information about the Python-list mailing list