Skip to content

Commit e54e426

Browse files
author
antoine.pitrou
committed
Followup of #4705: we can't skip the binary buffering layer for stdin because FileIO doesn't have a read1() method
git-svn-id: http://svn.python.org/projects/python/branches/py3k@68977 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 1088d08 commit e54e426

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Lib/test/test_cmd_line.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ def test_unbuffered_output(self):
159159
self.assertEqual(data.strip(), b'x',
160160
"text %s not line-buffered" % stream)
161161

162+
def test_unbuffered_input(self):
163+
# sys.stdin still works with '-u'
164+
code = ("import sys; sys.stdout.write(sys.stdin.read(1))")
165+
p = _spawn_python('-u', '-c', code)
166+
p.stdin.write(b'x')
167+
p.stdin.flush()
168+
data, rc = _kill_python_and_exit_code(p)
169+
self.assertEqual(rc, 0)
170+
self.assertEqual(data.strip(), b'x')
171+
162172

163173
def test_main():
164174
test.support.run_unittest(CmdLineTest)

Python/pythonrun.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,12 @@ create_stdio(PyObject* io,
739739
PyObject *line_buffering;
740740
int buffering, isatty;
741741

742-
if (Py_UnbufferedStdioFlag)
742+
/* stdin is always opened in buffered mode, first because it shouldn't
743+
make a difference in common use cases, second because TextIOWrapper
744+
depends on the presence of a read1() method which only exists on
745+
buffered streams.
746+
*/
747+
if (Py_UnbufferedStdioFlag && write_mode)
743748
buffering = 0;
744749
else
745750
buffering = -1;
@@ -753,7 +758,7 @@ create_stdio(PyObject* io,
753758
if (buf == NULL)
754759
goto error;
755760

756-
if (!Py_UnbufferedStdioFlag) {
761+
if (buffering) {
757762
raw = PyObject_GetAttrString(buf, "raw");
758763
if (raw == NULL)
759764
goto error;

0 commit comments

Comments
 (0)