Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 667d4b5

Browse files
author
Victor Stinner
committed
Issue #10763: subprocess.communicate() closes stdout and stderr if both are
pipes (bug specific to Windows). Improve also the unit test: write a portable unit test.
1 parent 291151b commit 667d4b5

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

Lib/subprocess.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,7 @@ def wait(self):
985985

986986
def _readerthread(self, fh, buffer):
987987
buffer.append(fh.read())
988+
fh.close()
988989

989990

990991
def _communicate(self, input):

Lib/test/test_subprocess.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -366,22 +366,28 @@ def test_communicate(self):
366366
self.assertEqual(stdout, b"banana")
367367
self.assertStderrEqual(stderr, b"pineapple")
368368

369-
# This test is Linux specific for simplicity to at least have
370-
# some coverage. It is not a platform specific bug.
371-
@unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()),
372-
"Linux specific")
373369
# Test for the fd leak reported in http://bugs.python.org/issue2791.
374370
def test_communicate_pipe_fd_leak(self):
375-
fd_directory = '/proc/%d/fd' % os.getpid()
376-
num_fds_before_popen = len(os.listdir(fd_directory))
377-
p = subprocess.Popen([sys.executable, "-c", "print()"],
378-
stdout=subprocess.PIPE)
379-
p.communicate()
380-
num_fds_after_communicate = len(os.listdir(fd_directory))
381-
del p
382-
num_fds_after_destruction = len(os.listdir(fd_directory))
383-
self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
384-
self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
371+
for stdin_pipe in (False, True):
372+
for stdout_pipe in (False, True):
373+
for stderr_pipe in (False, True):
374+
options = {}
375+
if stdin_pipe:
376+
options['stdin'] = subprocess.PIPE
377+
if stdout_pipe:
378+
options['stdout'] = subprocess.PIPE
379+
if stderr_pipe:
380+
options['stderr'] = subprocess.PIPE
381+
if not options:
382+
continue
383+
p = subprocess.Popen((sys.executable, "-c", "pass"), **options)
384+
p.communicate()
385+
if p.stdin is not None:
386+
self.assertTrue(p.stdin.closed)
387+
if p.stdout is not None:
388+
self.assertTrue(p.stdout.closed)
389+
if p.stderr is not None:
390+
self.assertTrue(p.stderr.closed)
385391

386392
def test_communicate_returns(self):
387393
# communicate() should return None if no redirection is active

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Core and Builtins
1111
Library
1212
-------
1313

14+
- Issue #10763: subprocess.communicate() closes stdout and stderr if both are
15+
pipes (bug specific to Windows).
16+
1417
- Issue #1693546: fix email.message RFC 2231 parameter encoding to be in better
1518
compliance (no "s around encoded values).
1619

0 commit comments

Comments
 (0)