changeset: 68305:9e3b1d77b0f8 tag: tip user: Victor Stinner date: Mon Mar 07 12:05:14 2011 +0100 files: Lib/test/test_os.py Misc/NEWS Modules/_io/fileio.c description: Issue #11395: FileIO().write() clamps the length to 32,767 bytes 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 (python -u) and the length is greater than 66,000 bytes (or less, depending on heap usage). diff -r c6a8e7debbe5 -r 9e3b1d77b0f8 Lib/test/test_os.py --- a/Lib/test/test_os.py Sun Mar 06 09:06:34 2011 -0600 +++ b/Lib/test/test_os.py Mon Mar 07 12:05:14 2011 +0100 @@ -97,6 +97,25 @@ 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 (python + # -u) 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): diff -r c6a8e7debbe5 -r 9e3b1d77b0f8 Misc/NEWS --- a/Misc/NEWS Sun Mar 06 09:06:34 2011 -0600 +++ b/Misc/NEWS Mon Mar 07 12:05:14 2011 +0100 @@ -23,6 +23,12 @@ - Issue #10829: Refactor PyUnicode_FromFormat(), use the same function to parse the format string in the 3 steps, fix crashs on invalid format strings. +- 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 (python -u) and the length is greater than 66,000 bytes + (or less, depending on heap usage). + - Issue #11246: Fix PyUnicode_FromFormat("%V") to decode the byte string from UTF-8 (with replace error handler) instead of ISO-8859-1 (in strict mode). Patch written by Ray Allen. diff -r c6a8e7debbe5 -r 9e3b1d77b0f8 Modules/_io/fileio.c --- a/Modules/_io/fileio.c Sun Mar 06 09:06:34 2011 -0600 +++ b/Modules/_io/fileio.c Mon Mar 07 12:05:14 2011 +0100 @@ -712,7 +712,14 @@ 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 + (python -u) 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