Skip to content

Commit 8f2ead7

Browse files
committed
Fix trailing surrogate characters
1 parent d588103 commit 8f2ead7

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Lib/test/test_threading.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,6 +2130,11 @@ def test_set_name(self):
21302130

21312131
# Test long non-ASCII name (truncated)
21322132
"x" * (limit - 1) + "é€",
2133+
2134+
# Test long non-BMP names (truncated) creating surrogate pairs
2135+
# on Windows
2136+
"x" * (limit - 1) + "\U0010FFFF",
2137+
"x" * (limit - 2) + "\U0010FFFF" * 2,
21332138
]
21342139
if os_helper.FS_NONASCII:
21352140
tests.append(f"nonascii:{os_helper.FS_NONASCII}")
@@ -2158,6 +2163,10 @@ def work():
21582163
expected = os.fsdecode(encoded)
21592164
else:
21602165
expected = name[:truncate]
2166+
if ord(expected[-1]) > 0xFFFF:
2167+
# truncate the last non-BMP character to omit a lone
2168+
# surrogate character
2169+
expected = expected[:-1]
21612170
if '\0' in expected:
21622171
expected = expected.split('\0', 1)[0]
21632172

Modules/_threadmodule.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2488,7 +2488,13 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
24882488

24892489
if (len > PYTHREAD_NAME_MAXLEN) {
24902490
// Truncate the name
2491-
name[PYTHREAD_NAME_MAXLEN] = 0;
2491+
Py_UCS4 ch = name[PYTHREAD_NAME_MAXLEN-1];
2492+
if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) {
2493+
name[PYTHREAD_NAME_MAXLEN-1] = 0;
2494+
}
2495+
else {
2496+
name[PYTHREAD_NAME_MAXLEN] = 0;
2497+
}
24922498
}
24932499

24942500
HRESULT hr = pSetThreadDescription(GetCurrentThread(), name);

0 commit comments

Comments
 (0)