From 877766dee8e60c7971ed0cabba89fbe981c2ab1b Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sat, 19 Mar 2011 17:00:37 +0100 Subject: [PATCH] Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates unbuffered pipes, such that select() works properly on them. --- Doc/library/platform.rst | 2 +- Lib/os.py | 4 +++- Lib/platform.py | 2 +- Lib/subprocess.py | 2 -- Lib/test/test_subprocess.py | 15 +++++++++++++++ Misc/NEWS | 3 +++ 6 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Doc/library/platform.rst b/Doc/library/platform.rst index 56518f79e84..a71d5335a2c 100644 --- a/Doc/library/platform.rst +++ b/Doc/library/platform.rst @@ -205,7 +205,7 @@ Windows Platform Win95/98 specific ^^^^^^^^^^^^^^^^^ -.. function:: popen(cmd, mode='r', bufsize=None) +.. function:: popen(cmd, mode='r', bufsize=-1) Portable :func:`popen` interface. Find a working popen implementation preferring :func:`win32pipe.popen`. On Windows NT, :func:`win32pipe.popen` diff --git a/Lib/os.py b/Lib/os.py index aa1a40f7a75..b46c02f5800 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -629,11 +629,13 @@ if not _exists("urandom"): return bs # Supply os.popen() -def popen(cmd, mode="r", buffering=None): +def popen(cmd, mode="r", buffering=-1): if not isinstance(cmd, str): raise TypeError("invalid cmd type (%s, expected string)" % type(cmd)) if mode not in ("r", "w"): raise ValueError("invalid mode %r" % mode) + if buffering == 0 or buffering == None: + raise ValueError("popen() does not support unbuffered streams") import subprocess, io if mode == "r": proc = subprocess.Popen(cmd, diff --git a/Lib/platform.py b/Lib/platform.py index 2294604ea41..dd09460aad5 100755 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -398,7 +398,7 @@ class _popen: # Alias __del__ = close -def popen(cmd, mode='r', bufsize=None): +def popen(cmd, mode='r', bufsize=-1): """ Portable popen() interface. """ diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 97792dbe605..dc5b6088da6 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -653,8 +653,6 @@ class Popen(object): if errread is not None: errread = msvcrt.open_osfhandle(errread.Detach(), 0) - if bufsize == 0: - bufsize = 1 # Nearly unbuffered (XXX for now) if p2cwrite is not None: self.stdin = io.open(p2cwrite, 'wb', bufsize) if self.universal_newlines: diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index c781904ed9c..410849fc286 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -864,6 +864,21 @@ class ProcessTestCase(BaseTestCase): def prepare(): raise ValueError("surrogate:\uDCff") + def test_select_unbuffered(self): + # Issue #11459: bufsize=0 should really set the pipes as + # unbuffered (and therefore let select() work properly). + select = support.import_module("select") + p = subprocess.Popen([sys.executable, "-c", + 'import sys;' + 'sys.stdout.write("apple")'], + stdout=subprocess.PIPE, + bufsize=0) + f = p.stdout + try: + self.assertEqual(f.read(4), b"appl") + self.assertIn(f, select.select([f], [], [], 0.0)[0]) + finally: + p.wait() # # Windows tests diff --git a/Misc/NEWS b/Misc/NEWS index f18e557fd00..be44a06ac14 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -40,6 +40,9 @@ Core and Builtins Library ------- +- Issue #11459: A ``bufsize`` value of 0 in subprocess.Popen() really creates + unbuffered pipes, such that select() works properly on them. + - Issue #5421: Fix misleading error message when one of socket.sendto()'s arguments has the wrong type. Patch by Nikita Vetoshkin.