Skip to content

Commit c41af80

Browse files
aiskcmaloney
authored andcommitted
gh-158414: Fix hang when writing incomplete UTF-8 sequence to Windows console (GH-158435)
(cherry picked from commit aedb2c1) Co-authored-by: An Long <aisk@users.noreply.github.com> Co-authored-by: Cody Maloney <cmaloney@theoreticalchaos.com>
1 parent aeea95a commit c41af80

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
@@ -1100,7 +1100,10 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, PyTypeObject *cls,
11001100
len = Py_MIN(len, max_wlen * 3);
11011101
while (1) {
11021102
/* Fix for github issues gh-110913 and gh-82052. */
1103-
len = _find_last_utf8_boundary(b->buf, len);
1103+
DWORD boundary = _find_last_utf8_boundary(b->buf, len);
1104+
if (boundary != 0) {
1105+
len = boundary;
1106+
}
11041107
wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, NULL, 0);
11051108
if (wlen <= max_wlen) {
11061109
break;

0 commit comments

Comments
 (0)