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

Discussion in 'Python' started by William McBrine, Mar 16, 2008.

  1. 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
    William McBrine, Mar 16, 2008
    #1
    1. Advertising

  2. William McBrine

    Tim Golden Guest

    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
    Tim Golden, Mar 16, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron W. LaFramboise
    Replies:
    4
    Views:
    343
  2. 7stud
    Replies:
    11
    Views:
    682
    Dennis Lee Bieber
    Mar 20, 2007
  3. kgk
    Replies:
    1
    Views:
    295
    Marc 'BlackJack' Rintsch
    Jul 11, 2007
  4. Ittay Dror

    Not enough space error on windows

    Ittay Dror, Nov 19, 2008, in forum: Ruby
    Replies:
    5
    Views:
    126
    Ittay Dror
    Dec 8, 2008
  5. ThomasW
    Replies:
    11
    Views:
    281
    ThomasW
    Sep 28, 2009
Loading...

Share This Page