Skip to content

Commit 8ae8f64

Browse files
committed
bpo-42044: Write all bytes to the console in unbuffered mode on Windows
Note: a fix for issue11395 started to write bytes capped at 32k and mandated the caller to handle partial writes, but when the text io flush was called, it only does only a single call in unbuffered mode. This made it so that the bytes unwritten were actually lost (and not printed) in the process. The reason for that was historical to support older versions of Windows. As of Windows 10, this is no longer needed and thus given issue42044, the number of bytes written to the console is no longer capped.
1 parent c956734 commit 8ae8f64

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
When the io is flushed in unbuffered mode in Windows, all bytes are now
2+
properly written to the console.

Modules/_io/winconsoleio.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -978,16 +978,6 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, Py_buffer *b)
978978

979979
Py_BEGIN_ALLOW_THREADS
980980
wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, NULL, 0);
981-
982-
/* issue11395 there is an unspecified upper bound on how many bytes
983-
can be written at once. We cap at 32k - the caller will have to
984-
handle partial writes.
985-
Since we don't know how many input bytes are being ignored, we
986-
have to reduce and recalculate. */
987-
while (wlen > 32766 / sizeof(wchar_t)) {
988-
len /= 2;
989-
wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, NULL, 0);
990-
}
991981
Py_END_ALLOW_THREADS
992982

993983
if (!wlen)
@@ -998,6 +988,11 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, Py_buffer *b)
998988
Py_BEGIN_ALLOW_THREADS
999989
wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, wbuf, wlen);
1000990
if (wlen) {
991+
/* Note that it's possible that this would fail to print big strings
992+
* on Windows 7 (before Windows 8 LPC-based pseudo-files were used
993+
* for I/O and would be limited to a 64 KiB heap).
994+
* As Windows 7 is currently unsupported, that's Ok.
995+
*/
1001996
res = WriteConsoleW(handle, wbuf, wlen, &n, NULL);
1002997
if (res && n < wlen) {
1003998
/* Wrote fewer characters than expected, which means our

0 commit comments

Comments
 (0)