mirror of
https://github.com/python/cpython.git
synced 2025-10-17 12:18:23 +00:00
bpo-30404: The -u option now makes the stdout and stderr streams totally unbuffered. (#1667)
This commit is contained in:
parent
0b5e61ddca
commit
77732be801
4 changed files with 17 additions and 12 deletions
|
@ -303,13 +303,13 @@ Miscellaneous options
|
||||||
|
|
||||||
.. cmdoption:: -u
|
.. cmdoption:: -u
|
||||||
|
|
||||||
Force the binary layer of the stdout and stderr streams (which is
|
Force the stdout and stderr streams to be unbuffered.
|
||||||
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.
|
|
||||||
|
|
||||||
See also :envvar:`PYTHONUNBUFFERED`.
|
See also :envvar:`PYTHONUNBUFFERED`.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.7
|
||||||
|
The text layer of the stdout and stderr streams now is unbuffered.
|
||||||
|
|
||||||
|
|
||||||
.. cmdoption:: -v
|
.. cmdoption:: -v
|
||||||
|
|
||||||
|
|
|
@ -221,13 +221,12 @@ class CmdLineTest(unittest.TestCase):
|
||||||
rc, out, err = assert_python_ok('-u', '-c', code)
|
rc, out, err = assert_python_ok('-u', '-c', code)
|
||||||
data = err if stream == 'stderr' else out
|
data = err if stream == 'stderr' else out
|
||||||
self.assertEqual(data, b'x', "binary %s not unbuffered" % stream)
|
self.assertEqual(data, b'x', "binary %s not unbuffered" % stream)
|
||||||
# Text is line-buffered
|
# Text is unbuffered
|
||||||
code = ("import os, sys; sys.%s.write('x\\n'); os._exit(0)"
|
code = ("import os, sys; sys.%s.write('x'); os._exit(0)"
|
||||||
% stream)
|
% stream)
|
||||||
rc, out, err = assert_python_ok('-u', '-c', code)
|
rc, out, err = assert_python_ok('-u', '-c', code)
|
||||||
data = err if stream == 'stderr' else out
|
data = err if stream == 'stderr' else out
|
||||||
self.assertEqual(data.strip(), b'x',
|
self.assertEqual(data, b'x', "text %s not unbuffered" % stream)
|
||||||
"text %s not line-buffered" % stream)
|
|
||||||
|
|
||||||
def test_unbuffered_input(self):
|
def test_unbuffered_input(self):
|
||||||
# sys.stdin still works with '-u'
|
# sys.stdin still works with '-u'
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
The -u option now makes the stdout and stderr streams unbuffered rather than
|
||||||
|
line-buffered.
|
|
@ -1498,7 +1498,7 @@ create_stdio(PyObject* io,
|
||||||
PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
|
PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
|
||||||
const char* mode;
|
const char* mode;
|
||||||
const char* newline;
|
const char* newline;
|
||||||
PyObject *line_buffering;
|
PyObject *line_buffering, *write_through;
|
||||||
int buffering, isatty;
|
int buffering, isatty;
|
||||||
_Py_IDENTIFIER(open);
|
_Py_IDENTIFIER(open);
|
||||||
_Py_IDENTIFIER(isatty);
|
_Py_IDENTIFIER(isatty);
|
||||||
|
@ -1555,7 +1555,11 @@ create_stdio(PyObject* io,
|
||||||
Py_DECREF(res);
|
Py_DECREF(res);
|
||||||
if (isatty == -1)
|
if (isatty == -1)
|
||||||
goto error;
|
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;
|
line_buffering = Py_True;
|
||||||
else
|
else
|
||||||
line_buffering = Py_False;
|
line_buffering = Py_False;
|
||||||
|
@ -1574,9 +1578,9 @@ create_stdio(PyObject* io,
|
||||||
newline = "\n";
|
newline = "\n";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
|
stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssOO",
|
||||||
buf, encoding, errors,
|
buf, encoding, errors,
|
||||||
newline, line_buffering);
|
newline, line_buffering, write_through);
|
||||||
Py_CLEAR(buf);
|
Py_CLEAR(buf);
|
||||||
if (stream == NULL)
|
if (stream == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue