]> granicus.if.org Git - python/commitdiff
Followup of #4705: we can't skip the binary buffering layer for stdin because FileIO...
authorAntoine Pitrou <solipsis@pitrou.net>
Mon, 26 Jan 2009 21:48:00 +0000 (21:48 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Mon, 26 Jan 2009 21:48:00 +0000 (21:48 +0000)
Lib/test/test_cmd_line.py
Python/pythonrun.c

index e56718438f5ae820d44d31276a09a4e5cd5ca431..77bd6bb07ed375e8177c653455b34b5bc8636d52 100644 (file)
@@ -159,6 +159,16 @@ class CmdLineTest(unittest.TestCase):
             self.assertEqual(data.strip(), b'x',
                 "text %s not line-buffered" % stream)
 
+    def test_unbuffered_input(self):
+        # sys.stdin still works with '-u'
+        code = ("import sys; sys.stdout.write(sys.stdin.read(1))")
+        p = _spawn_python('-u', '-c', code)
+        p.stdin.write(b'x')
+        p.stdin.flush()
+        data, rc = _kill_python_and_exit_code(p)
+        self.assertEqual(rc, 0)
+        self.assertEqual(data.strip(), b'x')
+
 
 def test_main():
     test.support.run_unittest(CmdLineTest)
index 6819be55477949c254a22844de0c8278ab2a30f2..65c6f5f2da14598055728f2d7c2f51d5547a7784 100644 (file)
@@ -739,7 +739,12 @@ create_stdio(PyObject* io,
        PyObject *line_buffering;
        int buffering, isatty;
 
-       if (Py_UnbufferedStdioFlag)
+       /* stdin is always opened in buffered mode, first because it shouldn't
+          make a difference in common use cases, second because TextIOWrapper
+          depends on the presence of a read1() method which only exists on
+          buffered streams.
+       */
+       if (Py_UnbufferedStdioFlag && write_mode)
                buffering = 0;
        else
                buffering = -1;
@@ -753,7 +758,7 @@ create_stdio(PyObject* io,
        if (buf == NULL)
                goto error;
 
-       if (!Py_UnbufferedStdioFlag) {
+       if (buffering) {
                raw = PyObject_GetAttrString(buf, "raw");
                if (raw == NULL)
                        goto error;