[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

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

William McBrine

3/16/2008 4:26:00 PM

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
2 Answers

Tim Golden

3/16/2008 6:43:00 PM

0

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

rst9

4/18/2013 4:16:00 PM

0

Go rape and kill Indian women. That is the only thing you can do
well, Satish.
You are too stupid to know any better.