Re: subprocess.Popen inheriting



On Dec 17, 5:05 pm, "Gabriel Genellina" <gagsl-...@xxxxxxxxxxxx>
wrote:
En Wed, 17 Dec 2008 12:21:38 -0200, Jeremy Sanders  
<jeremy+complangpyt...@xxxxxxxxxxxxxxxxx> escribió:

Aaron Brady wrote:

I thought so too.  The web seems to say that on Linux they are, and on
Windows, you need to call DuplicateHandle for it.

Or set bInheritHandle=True when creating the pipe initially. os.pipe()  
doesn't do that.

I hit this problem - it looks like pipes aren't very versatile on  
Windows.
There's also the complicating factor that the handles in windows aren't  
the
same as the file numbers that Python uses, so you have to convert between
them.

It would be nice if Python created pipes that are properly inheritable by
default by child processes, as they're mostly used for IPC.

I'd say it is a bug in os.pipe implementation; they should be inheritable  
by default, as in posix (after all, the code is in "posixmodule.c").

The code looks like this:

ok = CreatePipe(&read, &write, NULL, 0);
Py_END_ALLOW_THREADS
if (!ok)
return win32_error("CreatePipe", NULL);
read_fd = _open_osfhandle((Py_intptr_t)read, 0);
write_fd = _open_osfhandle((Py_intptr_t)write, 1);

'If lpPipeAttributes is NULL, the handle cannot be inherited.' You
could populate a 'SECURITY_ATTRIBUTES' structure, or call
DuplicateHandle on both of them.

A patch would look like this:

SECURITY_ATTRIBUTES sattribs;
sattribs.nLength = sizeof(sattribs);
sattribs.lpSecurityDescriptor = NULL;
sattribs.bInheritHandle = TRUE;
ok = CreatePipe(&read, &write, &sattribs, 0);

This still doesn't answer whether the file descriptor return by
'_open_osfhandle' can be inherited too.
.



Relevant Pages

  • Re: subprocess.Popen inheriting
    ... Windows, you need to call DuplicateHandle for it. ... There's also the complicating factor that the handles in windows aren't the ... It would be nice if Python created pipes that are properly inheritable by ...
    (comp.lang.python)
  • Re: fseek on a file opened with _popen
    ... insert windows specific code under an #ifdef ... fileno - Maps a stream pointer to a file descriptor ... The filenofunction returns the file descriptor of a stream ... MinGw has a bug when using ftell/fseekfor pipes: ...
    (comp.lang.c)
  • Re: Download Microsoft C/C++ compiler for use with Python 2.6/2.7 ASAP
    ... one free from another one is trouble everywhere), but on windows: ... libraries in one process on UNIX, but it's obviously quite possible on ... windows (file descriptor: AFAIK, a file descriptor as returned from ...
    (comp.lang.python)
  • Re: Error: channel "stdout" wasnt opened for writing when "exec >&@stdout printenv"
    ... 'stdout' there has no file descriptor in the underlying ... It's a channel to a virtual file that's implemented ... Windows). ... the lack of a std channels is particular to Windows gui subsystem apps and OS X apps run from the Finder. ...
    (comp.lang.tcl)
  • Re: Win32 Event vs Conditional variable
    ... Not on windows, because it has a message queue mechanism already. ... Not on unix because you might also wait for a message (file descriptor ... Kernel API. ...
    (comp.programming.threads)