https://github.com/python/cpython/commit/aedb2c1d3d2bfaf60cef84c549606cc08542b63c
commit: aedb2c1d3d2bfaf60cef84c549606cc08542b63c
branch: main
author: An Long <[email protected]>
committer: cmaloney <[email protected]>
date: 2026-11-17T11:50:33-08:00
summary:

gh-158414: Fix hang when writing incomplete UTF-8 sequence to Windows console 
(#158435)

Co-authored-by: Cody Maloney <[email protected]>

files:
A Misc/NEWS.d/next/Windows/2026-09-30-01-12-58.gh-issue-158414.738yJa.rst
M Lib/test/test_io/test_winconsoleio.py
M Modules/_io/winconsoleio.c

diff --git a/Lib/test/test_io/test_winconsoleio.py 
b/Lib/test/test_io/test_winconsoleio.py
index 1bae884ed9ae3ea..b2474007da98935 100644
--- a/Lib/test/test_io/test_winconsoleio.py
+++ b/Lib/test/test_io/test_winconsoleio.py
@@ -160,7 +160,7 @@ def test_write(self):
                         testcases.append(data + b'\xff')
                         # incomplete multibyte sequence
                         with self.subTest(data=data):
-                            self.assertEqual(f.write(data), len(a))
+                            self.assertEqual(f.write(data), len(a or data))
             for data in testcases:
                 with self.subTest(data=data):
                     self.assertEqual(f.write(data), len(data))
diff --git 
a/Misc/NEWS.d/next/Windows/2026-09-30-01-12-58.gh-issue-158414.738yJa.rst 
b/Misc/NEWS.d/next/Windows/2026-09-30-01-12-58.gh-issue-158414.738yJa.rst
new file mode 100644
index 000000000000000..dba12665deddd93
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2026-09-30-01-12-58.gh-issue-158414.738yJa.rst
@@ -0,0 +1 @@
+Fix a hang when writing an incomplete UTF-8 sequence to the Windows console.
diff --git a/Modules/_io/winconsoleio.c b/Modules/_io/winconsoleio.c
index 3da4f1df1708a78..183506adc678ea2 100644
--- a/Modules/_io/winconsoleio.c
+++ b/Modules/_io/winconsoleio.c
@@ -1097,7 +1097,10 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, 
PyTypeObject *cls,
     len = Py_MIN(len, max_wlen * 3);
     while (1) {
         /* Fix for github issues gh-110913 and gh-82052. */
-        len = _find_last_utf8_boundary(b->buf, len);
+        DWORD boundary = _find_last_utf8_boundary(b->buf, len);
+        if (boundary != 0) {
+            len = boundary;
+        }
         wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, NULL, 0);
         if (wlen <= max_wlen) {
             break;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to