This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: subprocess.Popen closes fds for sys.stdout or sys.stderr
Type: Stage:
Components: Library (Lib) Versions: Python 2.4
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: astrand Nosy List: astrand, ngrover
Priority: normal Keywords:

Created on 2006-11-28 22:17 by ngrover, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
subprocess.py ngrover, 2006-11-28 22:17
Messages (2)
msg30707 - (view) Author: Nishkar Grover (ngrover) Date: 2006-11-28 22:17
I found a problem in subprocess.Popen's _execute_child() method for POSIX, where the child process will close the fds for sys.stdout and/or sys.stderr if I use those as stdout and/or stderr when creating a subprocess.Popen object.

Here's what I saw by default when using the 2.4.4 version of Python...

% ./python
Python 2.4.4 (#1, Nov 28 2006, 14:08:29)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import sys, subprocess
>>> uname = subprocess.Popen('uname -a', shell=True, stdout=sys.stdout)
>>> uname: write error: Bad file descriptor

>>>

Then, I updated subprocess.py and made the following changes...

% diff subprocess.py subprocess.py.orig
924c924
<                     # fd more than once and don't close sys.stdout or sys.stderr.
---
>                     # fd more than once.
927c927
<                     if c2pwrite and c2pwrite not in (p2cread, sys.stdout.fileno(), sys.stderr.fileno()):
---
>                     if c2pwrite and c2pwrite not in (p2cread,):
929c929
<                     if errwrite and errwrite not in (p2cread, c2pwrite, sys.stdout.fileno(), sys.stderr.fileno()):
---
>                     if errwrite and errwrite not in (p2cread, c2pwrite):

After that, I saw the following...

% ./python
Python 2.4.4 (#1, Nov 28 2006, 14:08:29)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import sys, subprocess
>>> uname = subprocess.Popen('uname -a', shell=True, stdout=sys.stdout)
>>> Linux schnauzer 2.6.9-42.0.2.ELsmp #1 SMP Thu Aug 17 18:00:32 EDT 2006 i686 i686 i386 GNU/Linux

>>>

I'm attaching the modified version of subprocess.py. Please consider adding this fix to future versions of Python. Thanks!
msg30708 - (view) Author: Peter Åstrand (astrand) * (Python committer) Date: 2007-01-07 14:05
Duplicate of 1531862. 
History
Date User Action Args
2022-04-11 14:56:21adminsetgithub: 44286
2006-11-28 22:17:14ngrovercreate