]> granicus.if.org Git - python/commitdiff
Issue #11395: io.FileIO().write() clamps the data length to 32,767 bytes on
authorVictor Stinner <victor.stinner@haypocalc.com>
Sun, 20 Mar 2011 22:36:35 +0000 (23:36 +0100)
committerVictor Stinner <victor.stinner@haypocalc.com>
Sun, 20 Mar 2011 22:36:35 +0000 (23:36 +0100)
Windows if the file is a TTY to workaround a Windows bug. The Windows console
returns an error (12: not enough space error) on writing into stdout if
stdout mode is binary and the length is greater than 66,000 bytes (or less,
depending on heap usage).

Lib/test/test_os.py
Misc/NEWS
Modules/_io/fileio.c

index 77fcbafed064c329844659657dff10cce61788c7..56be375b78c32b800e6b2ddc7c776c7c6b863441 100644 (file)
@@ -90,6 +90,25 @@ class FileTests(unittest.TestCase):
             self.assertEqual(fobj.read().splitlines(),
                 [b"bacon", b"eggs", b"spam"])
 
+    def write_windows_console(self, *args):
+        retcode = subprocess.call(args,
+            # use a new console to not flood the test output
+            creationflags=subprocess.CREATE_NEW_CONSOLE,
+            # use a shell to hide the console window (SW_HIDE)
+            shell=True)
+        self.assertEqual(retcode, 0)
+
+    @unittest.skipUnless(sys.platform == 'win32',
+                         'test specific to the Windows console')
+    def test_write_windows_console(self):
+        # Issue #11395: the Windows console returns an error (12: not enough
+        # space error) on writing into stdout if stdout mode is binary and the
+        # length is greater than 66,000 bytes (or less, depending on heap
+        # usage).
+        code = "print('x' * 100000)"
+        self.write_windows_console(sys.executable, "-c", code)
+        self.write_windows_console(sys.executable, "-u", "-c", code)
+
 
 class TemporaryFileTests(unittest.TestCase):
     def setUp(self):
index 19ed8f9edb224c675e79ffe4036cccfdc2042576..49626f1af44db4c2f0e6f4e66ca929e2d00cc3e6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,12 @@ What's New in Python 3.2.1?
 Core and Builtins
 -----------------
 
+- Issue #11395: io.FileIO().write() clamps the data length to 32,767 bytes on
+  Windows if the file is a TTY to workaround a Windows bug. The Windows console
+  returns an error (12: not enough space error) on writing into stdout if
+  stdout mode is binary and the length is greater than 66,000 bytes (or less,
+  depending on heap usage).
+
 - Issue #11320: fix bogus memory management in Modules/getpath.c, leading to
   a possible crash when calling Py_SetPath().
 
index f96f2e2ede23d169ec7ce029f2995088119fc286..1aa5ee992d715d0f93c3c00a3e5206203a9df4be 100644 (file)
@@ -712,7 +712,14 @@ fileio_write(fileio *self, PyObject *args)
         errno = 0;
         len = pbuf.len;
 #if defined(MS_WIN64) || defined(MS_WINDOWS)
-        if (len > INT_MAX)
+        if (len > 32767 && isatty(self->fd)) {
+            /* Issue #11395: the Windows console returns an error (12: not
+               enough space error) on writing into stdout if stdout mode is
+               binary and the length is greater than 66,000 bytes (or less,
+               depending on heap usage). */
+            len = 32767;
+        }
+        else if (len > INT_MAX)
             len = INT_MAX;
         n = write(self->fd, pbuf.buf, (int)len);
 #else