Skip to content

Commit d5e9399

Browse files
committed
make _threadmodule.c clearer, remove unrelated change, add specific test case for openindiana in test_threading.py
1 parent 84daea4 commit d5e9399

2 files changed

Lines changed: 46 additions & 16 deletions

File tree

Lib/test/test_threading.py

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

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

22472246
class EventTests(lock_tests.EventTests):
@@ -2361,10 +2360,44 @@ def work():
23612360
thread = threading.Thread(target=work, name=name)
23622361
thread.start()
23632362
thread.join()
2364-
if not name.isascii() and not work_name:
2365-
self.skipTest(f"Platform does not support non-ASCII thread names: got empty name for {name!r}")
2366-
self.assertEqual(work_name, expected,
2367-
f"{len(work_name)=} and {len(expected)=}")
2363+
2364+
# Detect if running on OpenIndiana / illumos
2365+
try:
2366+
is_illumos = os.uname().version.startswith('illumos')
2367+
except AttributeError:
2368+
is_illumos = False
2369+
2370+
if is_illumos:
2371+
# illumos requires ASCII-encoded thread names
2372+
if not work_name:
2373+
# name didn't get set (set_name may have failed)
2374+
self.skipTest(
2375+
f"Platform does not support non-ASCII thread names: got empty name for {name!r}"
2376+
)
2377+
2378+
work_name_bytes = (
2379+
work_name.encode('ascii', errors='ignore')
2380+
if isinstance(work_name, str)
2381+
else work_name
2382+
)
2383+
expected_bytes = expected.encode('ascii', errors='ignore')
2384+
2385+
self.assertEqual(
2386+
work_name_bytes,
2387+
expected_bytes,
2388+
f"{len(work_name)=} and {len(expected)=}"
2389+
)
2390+
2391+
elif not name.isascii() and not work_name:
2392+
# Platform does not support non-ASCII thread names
2393+
self.skipTest(
2394+
f"Platform does not support non-ASCII thread names: got empty name for {name!r}"
2395+
)
2396+
2397+
else:
2398+
# Most platforms
2399+
self.assertEqual(work_name, expected,
2400+
f"{len(work_name)=} and {len(expected)=}")
23682401

23692402
@unittest.skipUnless(hasattr(_thread, 'set_name'), "missing _thread.set_name")
23702403
@unittest.skipUnless(hasattr(_thread, '_get_name'), "missing _thread._get_name")

Modules/_threadmodule.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,18 +2655,15 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
26552655
return NULL;
26562656
}
26572657

2658-
if (rc) {
2659-
/* If native API refused (EINVAL) and we didn't try ASCII, retry with ASCII. */
2660-
if (rc == EINVAL && strcmp(encoding, "ascii") != 0) {
2661-
rc = set_encoded_thread_name(name_obj, "ascii");
2662-
if (rc == -1 && PyErr_Occurred()) {
2663-
return NULL;
2664-
}
2665-
if (rc == 0) {
2666-
Py_RETURN_NONE;
2667-
}
2668-
/* fall through to raise errno below */
2658+
/* If native API refused (EINVAL) and we didn't try ASCII, retry with ASCII. */
2659+
if (rc == EINVAL && strcmp(encoding, "ascii") != 0) {
2660+
rc = set_encoded_thread_name(name_obj, "ascii");
2661+
if (rc == -1 && PyErr_Occurred()) {
2662+
return NULL;
26692663
}
2664+
/* fall through to raise errno below */
2665+
}
2666+
if (rc) {
26702667
errno = rc;
26712668
return PyErr_SetFromErrno(PyExc_OSError);
26722669
}

0 commit comments

Comments
 (0)