From 7ada08525a2dde87f4e35e24ec312cfe70b6ec95 Mon Sep 17 00:00:00 2001 From: An Long Date: Wed, 18 Nov 2026 04:50:33 +0900 Subject: [PATCH] gh-158414: Fix hang when writing incomplete UTF-8 sequence to Windows console (GH-158435) (cherry picked from commit aedb2c1d3d2bfaf60cef84c549606cc08542b63c) Co-authored-by: An Long Co-authored-by: Cody Maloney --- Lib/test/test_io/test_winconsoleio.py | 2 +- .../Windows/2026-09-30-01-12-58.gh-issue-158414.738yJa.rst | 1 + Modules/_io/winconsoleio.c | 5 ++++- 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Windows/2026-09-30-01-12-58.gh-issue-158414.738yJa.rst diff --git a/Lib/test/test_io/test_winconsoleio.py b/Lib/test/test_io/test_winconsoleio.py index 1bae884ed9ae3e..b2474007da9893 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 00000000000000..dba12665deddd9 --- /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 f9d661fb307178..0dd0d808dac792 100644 --- a/Modules/_io/winconsoleio.c +++ b/Modules/_io/winconsoleio.c @@ -1104,7 +1104,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;