8000 gh-59705: Implement _thread.set_name() on Windows by vstinner · Pull Request #128675 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-59705: Implement _thread.set_name() on Windows #128675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix trailing surrogate characters
  • Loading branch information
vstinner committed Jan 16, 2025
commit 8f2ead733d5dc9bbf7a927e0bb55f97331edfb05
9 changes: 9 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -2130,6 +2130,11 @@ def test_set_name(self):

# Test long non-ASCII name (truncated)
"x" * (limit - 1) + "é€",

# Test long non-BMP names (truncated) creating surrogate pairs
# on Windows
"x" * (limit - 1) + "\U0010FFFF",
"x" * (limit - 2) + "\U0010FFFF" * 2,
]
if os_helper.FS_NONASCII:
tests.append(f"nonascii:{os_helper.FS_NONASCII}")
Expand Down Expand Up @@ -2158,6 +2163,10 @@ def work():
expected = os.fsdecode(encoded)
else:
expected = name[:truncate]
if ord(expected[-1]) > 0xFFFF:
# truncate the last non-BMP character to omit a lone
# surrogate character
expected = expected[:-1]
if '\0' in expected:
expected = expected.split('\0', 1)[0]

Expand Down
8 changes: 7 additions & 1 deletion Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2488,7 +2488,13 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)

if (len > PYTHREAD_NAME_MAXLEN) {
// Truncate the name
name[PYTHREAD_NAME_MAXLEN] = 0;
Py_UCS4 ch = name[PYTHREAD_NAME_MAXLEN-1];
if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) {
name[PYTHREAD_NAME_MAXLEN-1] = 0;
}
else {
name[PYTHREAD_NAME_MAXLEN] = 0;
}
}

HRESULT hr = pSetThreadDescription(GetCurrentThread(), name);
Expand Down
Loading
0