Skip to content

gh-59705: Add _thread.set_name() function #127338

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 31 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c6d324d
gh-59705: Add _thread.set_name() function
vstinner Nov 27, 2024
63b5d52
Port to macOS
vstinner Nov 27, 2024
9f6a8ab
Add tests
vstinner Nov 27, 2024
d79e7af
Try to fix macOS _get_name()
vstinner Nov 27, 2024
ebd9752
Truncate to 15 bytes; add error handling
vstinner Nov 28, 2024
a7f5651
Address review
vstinner Nov 28, 2024
97ea645
Add test on non-ASCII name truncation
vstinner Nov 28, 2024
78a9ab9
Add test on non-ASCII name
vstinner Nov 28, 2024
dcf13f4
Test long name on non-Linux platforms
vstinner Nov 28, 2024
6ea7e5a
macOS is limited to 63 bytes
vstinner Nov 28, 2024
46721bb
Catch UnicodeEncodeError when seting the name
vstinner Nov 28, 2024
6962116
Add tests
vstinner Nov 28, 2024
5d27da0
Use "replace" error handler
vstinner Nov 28, 2024
b713910
Address review
vstinner Nov 29, 2024
5c20ea1
Use PyInterpreterState filesystem encoding
vstinner Nov 29, 2024
6088b37
FreeBSD truncates to 98 bytes silently
vstinner Dec 2, 2024
3c12d17
Merge branch 'main' into thread_set_name
vstinner Dec 3, 2024
bde935f
Fix test_threading on iOS
vstinner Dec 3, 2024
0584ca3
Fix create_test(): always encode using "replace"
vstinner Dec 3, 2024
7508b6c
Solaris truncates to 31 bytes
vstinner Dec 3, 2024
f4a9f40
Solaris always uses UTF-8
vstinner Dec 4, 2024
ac6d726
Add PYTHREAD_NAME_MAXLEN macro
vstinner Dec 4, 2024
08e5922
Fix configure.ac if PYTHREAD_NAME_MAXLEN is not set
vstinner Dec 4, 2024
681624e
Address Serhiy's review
vstinner Dec 5, 2024
f57339f
Solaris always use UTF-8
vstinner Dec 6, 2024
3941123
Simplify tests
vstinner Dec 6, 2024
2e27043
Use @unittest.skipUnless
vstinner Dec 6, 2024
6ab2944
Solaris uses UTF-8
vstinner Dec 6, 2024
b370c49
Update Lib/test/test_threading.py
vstinner Dec 6, 2024
beeae59
add test on embedded null character
vstinner Dec 6, 2024
ae956a0
Optimize code truncating the name
vstinner Dec 6, 2024
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
6 changes: 6 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,10 @@ def test_set_name(self):
# test short non-ASCII name
"namé€",

# embedded null character: name is truncated
# at the first null character
"embed\0null",

# Test long ASCII names (not truncated)
"x" * limit,

Expand All @@ -2142,6 +2146,8 @@ def work():

for name in tests:
encoded = name.encode(encoding, "replace")
if b'\0' in encoded:
encoded = encoded.split(b'\0', 1)[0]
if truncate is not None:
encoded = encoded[:truncate]
if sys.platform.startswith("solaris"):
Expand Down
9 changes: 5 additions & 4 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2421,21 +2421,22 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
return NULL;
}

const char *name = PyBytes_AS_STRING(name_encoded);
#ifdef PYTHREAD_NAME_MAXLEN
// Truncate to PYTHREAD_NAME_MAXLEN bytes + the NUL byte if needed
size_t len = strlen(name);
size_t len = PyBytes_GET_SIZE(name_encoded);
if (len > PYTHREAD_NAME_MAXLEN) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also inline len. It is only used here.

PyObject *truncated = PyBytes_FromStringAndSize(name, PYTHREAD_NAME_MAXLEN);
PyObject *truncated;
truncated = PyBytes_FromStringAndSize(PyBytes_AS_STRING(name_encoded),
PYTHREAD_NAME_MAXLEN);
if (truncated == NULL) {
Py_DECREF(name_encoded);
return NULL;
}
Py_SETREF(name_encoded, truncated);
name = PyBytes_AS_STRING(name_encoded);
}
#endif

const char *name = PyBytes_AS_STRING(name_encoded);
#ifdef __APPLE__
int rc = pthread_setname_np(name);
#else
Expand Down
Loading