8000 Issue #11395: io.FileIO().write() clamps the data length to 32,767 by… · python/cpython@e0daff1 · GitHub
[go: up one dir, main page]

Skip to content

Commit e0daff1

Browse files
author
Victor Stinner
committed
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).
1 parent 9c4efe5 commit e0daff1

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

Lib/test/test_os.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@ def test_write(self):
9090
self.assertEqual(fobj.read().splitlines(),
9191
[b"bacon", b"eggs", b"spam"])
9292

93+
def write_windows_console(self, *args):
94+
retcode = subprocess.call(args,
95+
# use a new console to not flood the test output
96+
creationflags=subprocess.CREATE_NEW_CONSOLE,
97+
# use a shell to hide the console window (SW_HIDE)
98+
shell=True)
99+
self.assertEqual(retcode, 0)
100+
101+
@unittest.skipUnless(sys.platform == 'win32',
102+
'test specific to the Windows console')
103+
def test_write_windows_console(self):
104+
# Issue #11395: the Windows console returns an error (12: not enough
105+
# space error) on writing into stdout if stdout mode is binary and the
106+
# length is greater than 66,000 bytes (or less, depending on heap
107+
# usage).
108+
code = "print('x' * 100000)"
109+
self.write_windows_console(sys.executable, "-c", code)
110+
self.write_windows_console(sys.executable, "-u", "-c", code)
111+
93112

94113
class TemporaryFileTests(unittest.TestCase):
95114
def setUp(self):

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ What's New in Python 3.2.1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #11395: io.FileIO().write() clamps the data length to 32,767 bytes on
14+
Windows if the file is a TTY to workaround a Windows bug. The Windows console
15+
returns an error (12: not enough space error) on writing into stdout if
16+
stdout mode is binary and the length is greater than 66,000 bytes (or less,
17+
depending on heap usage).
18+
1319
- Issue #11320: fix bogus memory management in Modules/getpath.c, leading to
1420
a possible crash when calling Py_SetPath().
1521

Modules/_io/fileio.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,14 @@ fileio_write(fileio *self, PyObject *args)
712712
errno = 0;
713713
len = pbuf.len;
714714
#if defined(MS_WIN64) || defined(MS_WINDOWS)
715-
if (len > INT_MAX)
715+
if (len > 32767 && isatty(self->fd)) {
716+
/* Issue #11395: the Windows console returns an error (12: not
717+
enough space error) on writing into stdout if stdout mode is
718+
binary and the length is greater than 66,000 bytes (or less,
719+
depending on heap usage). */
720+
len = 32767;
721+
}
722+
else if (len > INT_MAX)
716723
len = INT_MAX;
717724
n = write(self->fd, pbuf.buf, (int)len);
718725
#else

0 commit comments

Comments
 (0)
0