classification
Title: subprocess: inheritance of std descriptors inconsistent
Type: behavior Stage: patch review
Components: Documentation, Library (Lib) Versions: Python 3.2, Python 3.1, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: astrand, docs@python, kiilerix, ndparker, rosslagerwall
Priority: normal Keywords: patch

Created on 2005-06-26 13:37 by ndparker, last changed 2011-02-05 09:50 by ned.deily.

Files
File name Uploaded Description Edit
1227748.patch rosslagerwall, 2011-01-15 06:47 Doc patch
Messages (4)
msg25643 - (view) Author: André Malo (ndparker) Date: 2005-06-26 13:37
The inheritance of std descriptors is inconsistent
between Unix and Windows implementations.

If one calls Popen with stdin = stdout = stderr = None,
the caller's std descriptors are inherited on *x, but
not on Windows, because of the following optimization
(from subprocess.py r1.20):

   655	        def _get_handles(self, stdin, stdout,
stderr):
   656	            """Construct and return tupel with
IO objects:
   657	            p2cread, p2cwrite, c2pread,
c2pwrite, errread, errwrite
   658	            """
   659	            if stdin is None and stdout is None
and stderr is None:
   660	                return (None, None, None, None,
None, None)
   661	

I suggest to just remove those lines 659 and 660. The
current workaround is to duplicate the handles by the
application and supply an own STARTUPINFO structure.
msg25644 - (view) Author: Peter Ă…strand (astrand) * (Python committer) Date: 2007-01-29 20:54
>If one calls Popen with stdin = stdout = stderr = None,
>the caller's std descriptors are inherited on *x, but
>not on Windows, 

This is a correct observation. However, the current implementation is not necessarily wrong. This could instead be seen as a consequence of the different environments. The subprocess documentation states that "With None, no redirection will occur". So, it becomes an interpretation of what this really mean. Since the "default" behaviour on UNIX is to inherit and the default behaviour on Windows is to attach the standard handles to (an often newly created) console window, one could argue that this fits fairly good with the description "no redirection will occur". 

If we would change this, so that the parents handles are always inherited, then how would you specify that you want to attach the standard handles to the new console window? 

For best flexibility, the API should allow both cases: Both inherit all handles from the parent as well as attaching all standard handles to the new console window. As you point out, the current API allows this. So why change this?

One thing that's clearly an bug is the second part of the documentation:

"With None, no redirection will occur; the child's file handles will be inherited from the
parent"

This is currently only true on UNIX. If we should keep the current behaviour, at least the comment needs to be fixed. 
msg63330 - (view) Author: Mads Kiilerich (kiilerix) Date: 2008-03-06 18:48
Note to others searching for a solution to this and similar problems: 
http://svn.python.org/view/python/trunk/Lib/subprocess.py?rev=60115&view=auto
shows that this now (for 2.6?) has been changed so that close_fds now
controls inheritance through the CreateProcess parameter bInheritHandles

Thanks!
msg126315 - (view) Author: Ross Lagerwall (rosslagerwall) (Python committer) Date: 2011-01-15 06:47
I think all that is needed is a documentation patch. Attached is a doc patch which changes the doc to explicitly describe what happens on unix & windows (as described by Peter).
History
Date User Action Args
2011-02-05 09:50:00ned.deilysetassignee: astrand -> docs@python

nosy: + docs@python
stage: needs patch -> patch review
2011-01-15 06:47:51rosslagerwallsetfiles: + 1227748.patch

nosy: + rosslagerwall
messages: + msg126315

components: + Documentation
keywords: + patch
2010-08-21 16:06:18BreamoreBoysetstage: needs patch
type: behavior
versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.4
2008-03-06 18:48:18kiilerixsetnosy: + kiilerix
messages: + msg63330
2005-06-26 13:37:33ndparkercreate