Copying file descriptors

D

Didier Trosset

I am using the following code. It is used to spawn a new process (using
popen) and to change the file handles so that subsequent writes to
standard output with printf goes into this child process standard input.


import os

child_stdin = os.popen("cat -", "w")
old_stdout = os.dup(1)
os.close(child_stdin.fileno())

print "foo"

os.dup2(old_stdout, 1)
os.close(old_stdout)



It works. But the results is:
close failed: [Errno 9] Bad file descriptor
when I am using python 2.4. Note that python 2.3 did not output such an
error.

I am guessing that upon ending the script, python tries to close
child_stdin file object, whereas the underlying file descriptor has
already been closed. Thus the error.

Does anyone knows what to do to avoir this error message?

Thanks
Didier
 

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

No members online now.

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top