popen and exit code on Windows

I

iker.arizmendi

On UNIX one can use popen* to get a pipe for reading, a pipe for
writing, and the exit code of the child process via a call to close()
on the last pipe. Is there any way, in principle, to simulate such
behaviour on Windows? Some googling reveals that direct use of the
popen* functions on Windows will not do the trick, but are there
indirect ways?

Regards,
Iker
 
G

Giovanni Bajo

On UNIX one can use popen* to get a pipe for reading, a pipe for
writing, and the exit code of the child process via a call to close()
on the last pipe. Is there any way, in principle, to simulate such
behaviour on Windows? Some googling reveals that direct use of the
popen* functions on Windows will not do the trick, but are there
indirect ways?

This is well-tested:

def launch(cmd, split_lines=True):
"""Launch a sub-process. Return its output (both stdout and stderr),
optionally split by lines (if split_lines is True). Raise a LaunchError
exception if the exit code of the process is non-zero (failure)."""
if os.name not in ['nt', 'os2']:
p = popen2.Popen4(cmd)
p.tochild.close()
if split_lines:
out = p.fromchild.readlines()
else:
out = p.fromchild.read()
ret = p.wait()
if ret == 0:
ret = None
else:
ret >>= 8
else:
i,k = os.popen4(cmd)
i.close()
if split_lines:
out = k.readlines()
else:
out = k.read()
ret = k.close()

if ret is None:
return out
raise LaunchError(ret, cmd, out)
 
I

iker.arizmendi

Thanks for the reply Giovanni. Is this behaviour of the close method on
Windows documented anywhere?

Regards,
Iker
 

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

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top