Skip to content

Commit 5def2e9

Browse files
jadonduffserhiy-storchaka
authored andcommitted
gh-138004: Fix setting a thread name on OpenIndiana (GH-138017)
Encode Solaris/Illumos thread names to ASCII, since OpenIndiana does not support non-ASCII names. Add tests for setting non-ASCII name for the main thread. (cherry picked from commit c19db1d) Co-authored-by: jadonduff <[email protected]> Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 5ec6d56 commit 5def2e9

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

Lib/test/test_threading.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,6 +2230,9 @@ def test__all__(self):
22302230
@unittest.skipUnless(hasattr(_thread, 'set_name'), "missing _thread.set_name")
22312231
@unittest.skipUnless(hasattr(_thread, '_get_name'), "missing _thread._get_name")
22322232
def test_set_name(self):
2233+
# Ensure main thread name is restored after test
2234+
self.addCleanup(_thread.set_name, _thread._get_name())
2235+
22332236
# set_name() limit in bytes
22342237
truncate = getattr(_thread, "_NAME_MAXLEN", None)
22352238
limit = truncate or 100
@@ -2269,7 +2272,8 @@ def test_set_name(self):
22692272
tests.append(os_helper.TESTFN_UNENCODABLE)
22702273

22712274
if sys.platform.startswith("sunos"):
2272-
encoding = "utf-8"
2275+
# Use ASCII encoding on Solaris/Illumos/OpenIndiana
2276+
encoding = "ascii"
22732277
else:
22742278
encoding = sys.getfilesystemencoding()
22752279

@@ -2285,7 +2289,7 @@ def work():
22852289
if truncate is not None:
22862290
encoded = encoded[:truncate]
22872291
if sys.platform.startswith("sunos"):
2288-
expected = encoded.decode("utf-8", "surrogateescape")
2292+
expected = encoded.decode("ascii", "surrogateescape")
22892293
else:
22902294
expected = os.fsdecode(encoded)
22912295
else:
@@ -2304,7 +2308,11 @@ def work():
23042308
if '\0' in expected:
23052309
expected = expected.split('\0', 1)[0]
23062310

2307-
with self.subTest(name=name, expected=expected):
2311+
with self.subTest(name=name, expected=expected, thread="main"):
2312+
_thread.set_name(name)
2313+
self.assertEqual(_thread._get_name(), expected)
2314+
2315+
with self.subTest(name=name, expected=expected, thread="worker"):
23082316
work_name = None
23092317
thread = threading.Thread(target=work, name=name)
23102318
thread.start()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ Weilin Du
482482
John DuBois
483483
Paul Dubois
484484
Jacques Ducasse
485+
Jadon Duff
485486
Andrei Dorian Duma
486487
Graham Dumpleton
487488
Quinn Dunkan
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
On Solaris/Illumos platforms, thread names are now encoded as ASCII to avoid errors on systems (e.g. OpenIndiana) that don't support non-ASCII names.

Modules/_threadmodule.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,7 +2471,9 @@ _thread__get_name_impl(PyObject *module)
24712471
}
24722472

24732473
#ifdef __sun
2474-
return PyUnicode_DecodeUTF8(name, strlen(name), "surrogateescape");
2474+
// gh-138004: Decode Solaris/Illumos (e.g. OpenIndiana) thread names
2475+
// from ASCII, since OpenIndiana only supports ASCII names.
2476+
return PyUnicode_DecodeASCII(name, strlen(name), "surrogateescape");
24752477
#else
24762478
return PyUnicode_DecodeFSDefault(name);
24772479
#endif
@@ -2509,8 +2511,9 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
25092511
{
25102512
#ifndef MS_WINDOWS
25112513
#ifdef __sun
2512-
// Solaris always uses UTF-8
2513-
const char *encoding = "utf-8";
2514+
// gh-138004: Encode Solaris/Illumos thread names to ASCII,
2515+
// since OpenIndiana does not support non-ASCII names.
2516+
const char *encoding = "ascii";
25142517
#else
25152518
// Encode the thread name to the filesystem encoding using the "replace"
25162519
// error handler

0 commit comments

Comments
 (0)