Skip to content

Commit 482e7e0

Browse files
author
Skip Montanaro
committed
merge from upstream
2 parents b61efd0 + 1679f4d commit 482e7e0

File tree

6 files changed

+24
-5
lines changed

6 files changed

+24
-5
lines changed

Doc/library/platform.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ Windows Platform
208208
Win95/98 specific
209209
^^^^^^^^^^^^^^^^^
210210

211-
.. function:: popen(cmd, mode='r', bufsize=None)
211+
.. function:: popen(cmd, mode='r', bufsize=-1)
212212

213213
Portable :func:`popen` interface. Find a working popen implementation
214214
preferring :func:`win32pipe.popen`. On Windows NT, :func:`win32pipe.popen`

Lib/os.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,11 +779,13 @@ def urandom(n):
779779
return bs
780780

781781
# Supply os.popen()
782-
def popen(cmd, mode="r", buffering=None):
782+
def popen(cmd, mode="r", buffering=-1):
783783
if not isinstance(cmd, str):
784784
raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
785785
if mode not in ("r", "w"):
786786
raise ValueError("invalid mode %r" % mode)
787+
if buffering == 0 or buffering == None:
788+
raise ValueError("popen() does not support unbuffered streams")
787789
import subprocess, io
788790
if mode == "r":
789791
proc = subprocess.Popen(cmd,

Lib/platform.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def dist(distname='',version='',id='',
357357
supported_dists=supported_dists,
358358
full_distribution_name=0)
359359

360-
def popen(cmd, mode='r', bufsize=None):
360+
def popen(cmd, mode='r', bufsize=-1):
361361

362362
""" Portable popen() interface.
363363
"""

Lib/subprocess.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,6 @@ def __init__(self, args, bufsize=0, executable=None,
743743
if errread != -1:
744744
errread = msvcrt.open_osfhandle(errread.Detach(), 0)
745745

746-
if bufsize == 0:
747-
bufsize = 1 # Nearly unbuffered (XXX for now)
748746
if p2cwrite != -1:
749747
self.stdin = io.open(p2cwrite, 'wb', bufsize)
750748
if self.universal_newlines:

Lib/test/test_subprocess.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,22 @@ def test_wait_when_sigchild_ignored(self):
13151315
" non-zero with this error:\n%s" %
13161316
stderr.decode('utf-8'))
13171317

1318+
def test_select_unbuffered(self):
1319+
# Issue #11459: bufsize=0 should really set the pipes as
1320+
# unbuffered (and therefore let select() work properly).
1321+
select = support.import_module("select")
1322+
p = subprocess.Popen([sys.executable, "-c",
1323+
'import sys;'
1324+
'sys.stdout.write("apple")'],
1325+
stdout=subprocess.PIPE,
1326+
bufsize=0)
1327+
f = p.stdout
1328+
try:
1329+
self.assertEqual(f.read(4), b"appl")
1330+
self.assertIn(f, select.select([f], [], [], 0.0)[0])
1331+
finally:
1332+
p.wait()
1333+
13181334

13191335
@unittest.skipUnless(mswindows, "Windows specific tests")
13201336
class Win32ProcessTestCase(BaseTestCase):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ Core and Builtins
7575
Library
7676
-------
7777

78+
- Issue #11459: A ``bufsize`` value of 0 in subprocess.Popen() really creates
79+
unbuffered pipes, such that select() works properly on them.
80+
7881
- Issue #5421: Fix misleading error message when one of socket.sendto()'s
7982
arguments has the wrong type. Patch by Nikita Vetoshkin.
8083

0 commit comments

Comments
 (0)