Skip to content

Commit 97b8bf1

Browse files
committed
gh-158414: Fix hang when writing incomplete UTF-8 sequence to Windows console
1 parent 5c20517 commit 97b8bf1

3 files changed

Lines changed: 6 additions & 2 deletions

File tree

‎Lib/test/test_winconsoleio.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def test_write(self):
160160
testcases.append(data + b'\xff')
161161
# incomplete multibyte sequence
162162
with self.subTest(data=data):
163-
self.assertEqual(f.write(data), len(a))
163+
self.assertEqual(f.write(data), len(a or data))
164164
for data in testcases:
165165
with self.subTest(data=data):
166166
self.assertEqual(f.write(data), len(data))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a hang when writing an incomplete UTF-8 sequence to the Windows console.

‎Modules/_io/winconsoleio.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,10 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, PyTypeObject *cls,
10931093
len = Py_MIN(len, max_wlen * 3);
10941094
while (1) {
10951095
/* Fix for github issues gh-110913 and gh-82052. */
1096-
len = _find_last_utf8_boundary(b->buf, len);
1096+
DWORD boundary = _find_last_utf8_boundary(b->buf, len);
1097+
if (boundary != 0) {
1098+
len = boundary;
1099+
}
10971100
wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, NULL, 0);
10981101
if (wlen <= max_wlen) {
10991102
break;

0 commit comments

Comments
 (0)