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

Skip to content

Commit 4a8ea9e

Browse files
committed
Fixes issue #17488: Change the subprocess.Popen bufsize parameter default value
from unbuffered (0) to buffering (-1) to match the behavior existing code expects and match the behavior of the subprocess module in Python 2 to avoid introducing hard to track down bugs.
2 parents 59addb7 + a1b9ed3 commit 4a8ea9e

4 files changed

Lines changed: 55 additions & 19 deletions

File tree

Doc/library/subprocess.rst

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ are able to handle the less common cases not covered by the convenience
356356
functions.
357357

358358

359-
.. class:: Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, \
359+
.. class:: Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, \
360360
stderr=None, preexec_fn=None, close_fds=True, shell=False, \
361361
cwd=None, env=None, universal_newlines=False, \
362362
startupinfo=None, creationflags=0, restore_signals=True, \
@@ -428,17 +428,20 @@ functions.
428428
untrusted input. See the warning under :ref:`frequently-used-arguments`
429429
for details.
430430

431-
*bufsize*, if given, has the same meaning as the corresponding argument to the
432-
built-in open() function: :const:`0` means unbuffered, :const:`1` means line
433-
buffered, any other positive value means use a buffer of (approximately) that
434-
size. A negative *bufsize* means to use the system default, which usually means
435-
fully buffered. The default value for *bufsize* is :const:`0` (unbuffered).
431+
*bufsize* will be supplied as the corresponding argument to the :meth:`io.open`
432+
function when creating the stdin/stdout/stderr pipe file objects:
433+
:const:`0` means unbuffered (read and write are one system call and can return short),
434+
:const:`1` means line buffered, any other positive value means use a buffer of
435+
approximately that size. A negative bufsize (the default) means
436+
the system default of io.DEFAULT_BUFFER_SIZE will be used.
436437

437-
.. note::
438+
.. versionchanged:: 3.2.4, 3.3.1
438439

439-
If you experience performance issues, it is recommended that you try to
440-
enable buffering by setting *bufsize* to either -1 or a large enough
441-
positive value (such as 4096).
440+
*bufsize* now defaults to -1 to enable buffering by default to match the
441+
behavior that most code expects. In 3.2.0 through 3.2.3 and 3.3.0 it
442+
incorrectly defaulted to :const:`0` which was unbuffered and allowed
443+
short reads. This was unintentional and did not match the behavior of
444+
Python 2 as most code expected.
442445

443446
The *executable* argument specifies a replacement program to execute. It
444447
is very seldom needed. When ``shell=False``, *executable* replaces the

Lib/subprocess.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
===========================
2626
This module defines one class called Popen:
2727
28-
class Popen(args, bufsize=0, executable=None,
28+
class Popen(args, bufsize=-1, executable=None,
2929
stdin=None, stdout=None, stderr=None,
3030
preexec_fn=None, close_fds=True, shell=False,
3131
cwd=None, env=None, universal_newlines=False,
@@ -56,12 +56,12 @@ class Popen(args, bufsize=0, executable=None,
5656
way: The list2cmdline is designed for applications using the same
5757
rules as the MS C runtime.
5858
59-
bufsize, if given, has the same meaning as the corresponding argument
60-
to the built-in open() function: 0 means unbuffered, 1 means line
61-
buffered, any other positive value means use a buffer of
62-
(approximately) that size. A negative bufsize means to use the system
63-
default, which usually means fully buffered. The default value for
64-
bufsize is 0 (unbuffered).
59+
bufsize will be supplied as the corresponding argument to the io.open()
60+
function when creating the stdin/stdout/stderr pipe file objects:
61+
0 means unbuffered (read & write are one system call and can return short),
62+
1 means line buffered, any other positive value means use a buffer of
63+
approximately that size. A negative bufsize, the default, means the system
64+
default of io.DEFAULT_BUFFER_SIZE will be used.
6565
6666
stdin, stdout and stderr specify the executed programs' standard
6767
input, standard output and standard error file handles, respectively.
@@ -709,7 +709,7 @@ def getoutput(cmd):
709709

710710

711711
class Popen(object):
712-
def __init__(self, args, bufsize=0, executable=None,
712+
def __init__(self, args, bufsize=-1, executable=None,
713713
stdin=None, stdout=None, stderr=None,
714714
preexec_fn=None, close_fds=_PLATFORM_DEFAULT_CLOSE_FDS,
715715
shell=False, cwd=None, env=None, universal_newlines=False,
@@ -723,7 +723,7 @@ def __init__(self, args, bufsize=0, executable=None,
723723
self._input = None
724724
self._communication_started = False
725725
if bufsize is None:
726-
bufsize = 0 # Restore default
726+
bufsize = -1 # Restore default
727727
if not isinstance(bufsize, int):
728728
raise TypeError("bufsize must be an integer")
729729

Lib/test/test_subprocess.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,34 @@ def _execute_child(self, *args, **kwargs):
8282

8383
class ProcessTestCase(BaseTestCase):
8484

85+
def test_io_buffered_by_default(self):
86+
p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
87+
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
88+
stderr=subprocess.PIPE)
89+
try:
90+
self.assertIsInstance(p.stdin, io.BufferedIOBase)
91+
self.assertIsInstance(p.stdout, io.BufferedIOBase)
92+
self.assertIsInstance(p.stderr, io.BufferedIOBase)
93+
finally:
94+
p.stdin.close()
95+
p.stdout.close()
96+
p.stderr.close()
97+
p.wait()
98+
99+
def test_io_unbuffered_works(self):
100+
p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"],
101+
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
102+
stderr=subprocess.PIPE, bufsize=0)
103+
try:
104+
self.assertIsInstance(p.stdin, io.RawIOBase)
105+
self.assertIsInstance(p.stdout, io.RawIOBase)
106+
self.assertIsInstance(p.stderr, io.RawIOBase)
107+
finally:
108+
p.stdin.close()
109+
p.stdout.close()
110+
p.stderr.close()
111+
p.wait()
112+
85113
def test_call_seq(self):
86114
# call() function with sequence argument
87115
rc = subprocess.call([sys.executable, "-c",

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ Core and Builtins
294294
Library
295295
-------
296296

297+
- Issue #17488: Change the subprocess.Popen bufsize parameter default value
298+
from unbuffered (0) to buffering (-1) to match the behavior existing code
299+
expects and match the behavior of the subprocess module in Python 2 to avoid
300+
introducing hard to track down bugs.
301+
297302
- Issue #17521: Corrected non-enabling of logger following two calls to
298303
fileConfig().
299304

0 commit comments

Comments
 (0)