Skip to content

Commit 0606968

Browse files
committed
implement requested changes
1 parent 1db08a7 commit 0606968

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

Lib/test/test_threading.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,6 +2241,7 @@ def __init__(self, a, *, b) -> None:
22412241

22422242
with warnings.catch_warnings(record=True) as warnings_log:
22432243
CustomRLock(1, b=2)
2244+
22442245
self.assertEqual(warnings_log, [])
22452246

22462247
class EventTests(lock_tests.EventTests):
@@ -2361,6 +2362,9 @@ def work():
23612362
try:
23622363
thread.start()
23632364
thread.join()
2365+
# If the name is non-ASCII and the result is empty, skip (platform limitation)
2366+
if any(ord(c) > 127 for c in name) and (not work_name or work_name == ""):
2367+
self.skipTest(f"Platform does not support non-ASCII thread names: got empty name for {name!r}")
23642368
self.assertEqual(work_name, expected,
23652369
f"{len(work_name)=} and {len(expected)=}")
23662370
except OSError as exc:

Modules/_threadmodule.c

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,6 +2576,27 @@ _thread.set_name
25762576
Set the name of the current thread.
25772577
[clinic start generated code]*/
25782578

2579+
// Helper to set the thread name using platform-specific APIs
2580+
static int
2581+
_set_thread_name(const char *name)
2582+
{
2583+
int rc;
2584+
#ifdef __APPLE__
2585+
rc = pthread_setname_np(name);
2586+
#elif defined(__NetBSD__)
2587+
pthread_t thread = pthread_self();
2588+
rc = pthread_setname_np(thread, "%s", (void *)name);
2589+
#elif defined(HAVE_PTHREAD_SETNAME_NP)
2590+
pthread_t thread = pthread_self();
2591+
rc = pthread_setname_np(thread, name);
2592+
#else /* defined(HAVE_PTHREAD_SET_NAME_NP) */
2593+
pthread_t thread = pthread_self();
2594+
rc = 0; /* pthread_set_name_np() returns void */
2595+
pthread_set_name_np(thread, name);
2596+
#endif
2597+
return rc;
2598+
}
2599+
25792600
static PyObject *
25802601
_thread_set_name_impl(PyObject *module, PyObject *name_obj)
25812602
/*[clinic end generated code: output=402b0c68e0c0daed input=7e7acd98261be82f]*/
@@ -2591,40 +2612,24 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
25912612
const char *encoding = interp->unicode.fs_codec.encoding;
25922613
#endif
25932614
PyObject *name_encoded;
2615+
int rc;
2616+
25942617
name_encoded = PyUnicode_AsEncodedString(name_obj, encoding, "replace");
25952618
if (name_encoded == NULL) {
25962619
return NULL;
25972620
}
2598-
25992621
#ifdef _PYTHREAD_NAME_MAXLEN
2600-
// Truncate to _PYTHREAD_NAME_MAXLEN bytes + the NUL byte if needed
26012622
if (PyBytes_GET_SIZE(name_encoded) > _PYTHREAD_NAME_MAXLEN) {
2602-
PyObject *truncated;
2603-
truncated = PyBytes_FromStringAndSize(PyBytes_AS_STRING(name_encoded),
2604-
_PYTHREAD_NAME_MAXLEN);
2623+
PyObject *truncated = PyBytes_FromStringAndSize(PyBytes_AS_STRING(name_encoded), _PYTHREAD_NAME_MAXLEN);
26052624
if (truncated == NULL) {
26062625
Py_DECREF(name_encoded);
26072626
return NULL;
26082627
}
26092628
Py_SETREF(name_encoded, truncated);
26102629
}
26112630
#endif
2612-
26132631
const char *name = PyBytes_AS_STRING(name_encoded);
2614-
int rc;
2615-
#ifdef __APPLE__
2616-
rc = pthread_setname_np(name);
2617-
#elif defined(__NetBSD__)
2618-
pthread_t thread = pthread_self();
2619-
rc = pthread_setname_np(thread, "%s", (void *)name);
2620-
#elif defined(HAVE_PTHREAD_SETNAME_NP)
2621-
pthread_t thread = pthread_self();
2622-
rc = pthread_setname_np(thread, name);
2623-
#else /* defined(HAVE_PTHREAD_SET_NAME_NP) */
2624-
pthread_t thread = pthread_self();
2625-
rc = 0; /* pthread_set_name_np() returns void */
2626-
pthread_set_name_np(thread, name);
2627-
#endif
2632+
rc = _set_thread_name(name);
26282633
Py_DECREF(name_encoded);
26292634

26302635
// Fallback: If EINVAL, try ASCII encoding with "replace"
@@ -2635,9 +2640,7 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
26352640
}
26362641
#ifdef _PYTHREAD_NAME_MAXLEN
26372642
if (PyBytes_GET_SIZE(name_encoded) > _PYTHREAD_NAME_MAXLEN) {
2638-
PyObject *truncated;
2639-
truncated = PyBytes_FromStringAndSize(PyBytes_AS_STRING(name_encoded),
2640-
_PYTHREAD_NAME_MAXLEN);
2643+
PyObject *truncated = PyBytes_FromStringAndSize(PyBytes_AS_STRING(name_encoded), _PYTHREAD_NAME_MAXLEN);
26412644
if (truncated == NULL) {
26422645
Py_DECREF(name_encoded);
26432646
return NULL;
@@ -2646,19 +2649,7 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
26462649
}
26472650
#endif
26482651
name = PyBytes_AS_STRING(name_encoded);
2649-
#ifdef __APPLE__
2650-
rc = pthread_setname_np(name);
2651-
#elif defined(__NetBSD__)
2652-
thread = pthread_self();
2653-
rc = pthread_setname_np(thread, "%s", (void *)name);
2654-
#elif defined(HAVE_PTHREAD_SETNAME_NP)
2655-
thread = pthread_self();
2656-
rc = pthread_setname_np(thread, name);
2657-
#else /* defined(HAVE_PTHREAD_SET_NAME_NP) */
2658-
thread = pthread_self();
2659-
rc = 0;
2660-
pthread_set_name_np(thread, name);
2661-
#endif
2652+
rc = _set_thread_name(name);
26622653
Py_DECREF(name_encoded);
26632654
}
26642655

0 commit comments

Comments
 (0)