subprocess.Popen inheriting

Aaron Brady castironpi at
Thu Dec 18 01:46:32 CET 2008

On Dec 17, 5:05 pm, "Gabriel Genellina" <gagsl-... at>
> En Wed, 17 Dec 2008 12:21:38 -0200, Jeremy Sanders  
> <jeremy+complangpyt... at> 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);
	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:

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.

More information about the Python-list mailing list