From 1a465a5a2dda4d84eb81c6007da241e638bfcc54 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 19 May 2017 19:01:13 +0300 Subject: [PATCH 1/3] bpo-30404: The -u option now makes the stdout and stderr streams unbuffered. --- Doc/using/cmdline.rst | 8 ++++---- Lib/test/test_cmd_line.py | 7 +++---- Misc/NEWS | 3 +++ Python/pylifecycle.c | 12 ++++++++---- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index 40a06b9adc06ef1..4e4727169826eda 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -303,13 +303,13 @@ Miscellaneous options .. cmdoption:: -u - Force the binary layer of the stdout and stderr streams (which is - available as their ``buffer`` attribute) to be unbuffered. The text I/O - layer will still be line-buffered if writing to the console, or - block-buffered if redirected to a non-interactive file. + Force the stdout and stderr streams to be unbuffered. See also :envvar:`PYTHONUNBUFFERED`. + .. versionchanged: 3.7 + The text layer of the stdout and stderr streams now is unbuffered. + .. cmdoption:: -v diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 958d282a4288989..e856bb73063c1e0 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -219,13 +219,12 @@ def test_unbuffered_output(self): rc, out, err = assert_python_ok('-u', '-c', code) data = err if stream == 'stderr' else out self.assertEqual(data, b'x', "binary %s not unbuffered" % stream) - # Text is line-buffered - code = ("import os, sys; sys.%s.write('x\\n'); os._exit(0)" + # Text is unbuffered + code = ("import os, sys; sys.%s.write('x'); os._exit(0)" % stream) rc, out, err = assert_python_ok('-u', '-c', code) data = err if stream == 'stderr' else out - self.assertEqual(data.strip(), b'x', - "text %s not line-buffered" % stream) + self.assertEqual(data, b'x', "text %s not unbuffered" % stream) def test_unbuffered_input(self): # sys.stdin still works with '-u' diff --git a/Misc/NEWS b/Misc/NEWS index c6aed7f48c8dcb3..bb61770615919fc 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1? Core and Builtins ----------------- +- bpo-30404: The -u option now makes the stdout and stderr streams unbuffered + rather than line-buffered. + - bpo-30039: If a KeyboardInterrupt happens when the interpreter is in the middle of resuming a chain of nested 'yield from' or 'await' calls, it's now correctly delivered to the innermost frame. diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index c0f41b3ca73463e..2bda3d10ce73765 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1077,7 +1077,7 @@ create_stdio(PyObject* io, PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res; const char* mode; const char* newline; - PyObject *line_buffering; + PyObject *line_buffering, *write_through; int buffering, isatty; _Py_IDENTIFIER(open); _Py_IDENTIFIER(isatty); @@ -1134,7 +1134,11 @@ create_stdio(PyObject* io, Py_DECREF(res); if (isatty == -1) goto error; - if (isatty || Py_UnbufferedStdioFlag) + if (Py_UnbufferedStdioFlag) + write_through = Py_True; + else + write_through = Py_False; + if (isatty && !Py_UnbufferedStdioFlag) line_buffering = Py_True; else line_buffering = Py_False; @@ -1153,9 +1157,9 @@ create_stdio(PyObject* io, newline = "\n"; #endif - stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO", + stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssOO", buf, encoding, errors, - newline, line_buffering); + newline, line_buffering, write_through); Py_CLEAR(buf); if (stream == NULL) goto error; From 014a0b01644eae3342235799112724d7d0acacc8 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 19 May 2017 19:16:03 +0300 Subject: [PATCH 2/3] Fix a typo. --- Doc/using/cmdline.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index 4e4727169826eda..3d36a0ec5c85ebc 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -307,7 +307,7 @@ Miscellaneous options See also :envvar:`PYTHONUNBUFFERED`. - .. versionchanged: 3.7 + .. versionchanged:: 3.7 The text layer of the stdout and stderr streams now is unbuffered. From 2b1f9c4bdd38825016457aa7658272cc2883d100 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 3 Oct 2017 23:47:50 +0300 Subject: [PATCH 3/3] Move a NEWS entry to NEWS.d/. --- Misc/NEWS | 3 --- .../Core and Builtins/2017-10-03-23-46-39.bpo-30404._9Yi5u.rst | 2 ++ 2 files changed, 2 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2017-10-03-23-46-39.bpo-30404._9Yi5u.rst diff --git a/Misc/NEWS b/Misc/NEWS index 614055e7df5ae54..5cfda8d981da070 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,9 +10,6 @@ What's New in Python 3.7.0 alpha 1? Core and Builtins ----------------- -- bpo-30404: The -u option now makes the stdout and stderr streams unbuffered - rather than line-buffered. - - bpo-30682: Removed a too-strict assertion that failed for certain f-strings, such as eval("f'\\\n'") and eval("f'\\\r'"). diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-10-03-23-46-39.bpo-30404._9Yi5u.rst b/Misc/NEWS.d/next/Core and Builtins/2017-10-03-23-46-39.bpo-30404._9Yi5u.rst new file mode 100644 index 000000000000000..6c2802077f0eb74 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2017-10-03-23-46-39.bpo-30404._9Yi5u.rst @@ -0,0 +1,2 @@ +The -u option now makes the stdout and stderr streams unbuffered rather than +line-buffered.