Skip to content

Commit 6ea7e5a

Browse files
committed
macOS is limited to 63 bytes
1 parent dcf13f4 commit 6ea7e5a

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

Lib/test/test_threading.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,21 +2129,29 @@ def work():
21292129
tests.append((name, name))
21302130

21312131
if sys.platform == "linux":
2132-
# On Linux, set_name() truncates the name to 15 bytes.
2132+
limit = 15
2133+
elif sys.platform == "darwin":
2134+
limit = 63
2135+
else:
2136+
limit = None
2137+
2138+
if limit is not None:
2139+
# On Linux and macOS, set_name() truncates the name to,
2140+
# respectively, 15 and 63 bytes.
21332141

21342142
# Test ASCII name
21352143
name = "x" * 100
2136-
tests.append((name, name[:15]))
2144+
tests.append((name, name[:limit]))
21372145

21382146
# Test non-ASCII name
2139-
name = "x" * 14 + "é€"
2147+
name = "x" * (limit - 1) + "é€"
21402148
try:
21412149
encoded = os.fsencode(name)
21422150
except UnicodeEncodeError:
21432151
# name cannot be encoded to the filesystem encoding
21442152
pass
21452153
else:
2146-
expected = os.fsdecode(encoded[:15])
2154+
expected = os.fsdecode(encoded[:limit])
21472155
tests.append((name, expected))
21482156
else:
21492157
# Test long name

Modules/_threadmodule.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,7 +2375,8 @@ static PyObject *
23752375
_thread__get_name_impl(PyObject *module)
23762376
/*[clinic end generated code: output=20026e7ee3da3dd7 input=35cec676833d04c8]*/
23772377
{
2378-
char name[17];
2378+
// Linux and macOS are limited to respectively 16 and 64 bytes
2379+
char name[100];
23792380
size_t size = Py_ARRAY_LENGTH(name) - 1;
23802381
pthread_t thread = pthread_self();
23812382
int rc = pthread_getname_np(thread, name, size);
@@ -2405,20 +2406,25 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
24052406
{
24062407
const char *name = PyBytes_AS_STRING(name_obj);
24072408
#ifdef __APPLE__
2408-
int rc = pthread_setname_np(name);
2409-
#else
2409+
# define NAME_LIMIT 63
2410+
#elif defined(__linux__)
2411+
# define NAME_LIMIT 15
2412+
#endif
24102413

2411-
#if defined(__linux__)
2412-
// Truncate to 16 bytes including the NUL byte
2413-
char buffer[16];
2414+
#ifdef NAME_LIMIT
2415+
// Truncate to NAME_LIMIT bytes + the NUL byte
2416+
char buffer[NAME_LIMIT + 1];
24142417
size_t len = strlen(name);
2415-
if (len > 15) {
2416-
memcpy(buffer, name, 15);
2417-
buffer[15] = 0;
2418+
if (len > NAME_LIMIT) {
2419+
memcpy(buffer, name, NAME_LIMIT);
2420+
buffer[NAME_LIMIT] = 0;
24182421
name = buffer;
24192422
}
24202423
#endif
24212424

2425+
#ifdef __APPLE__
2426+
int rc = pthread_setname_np(name);
2427+
#else
24222428
pthread_t thread = pthread_self();
24232429
int rc = pthread_setname_np(thread, name);
24242430
#endif
@@ -2427,6 +2433,8 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
24272433
return PyErr_SetFromErrno(PyExc_OSError);
24282434
}
24292435
Py_RETURN_NONE;
2436+
2437+
#undef NAME_LIMIT
24302438
}
24312439
#endif // HAVE_PTHREAD_SETNAME_NP
24322440

0 commit comments

Comments
 (0)