Skip to content

Commit 48e1d1c

Browse files
jadonduffserhiy-storchaka
authored andcommitted
pythongh-138004: Fix setting a thread name on OpenIndiana (pythonGH-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. Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 79efc60 commit 48e1d1c

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
@@ -2281,6 +2281,9 @@ def test__all__(self):
22812281
@unittest.skipUnless(hasattr(_thread, 'set_name'), "missing _thread.set_name")
22822282
@unittest.skipUnless(hasattr(_thread, '_get_name'), "missing _thread._get_name")
22832283
def test_set_name(self):
2284+
# Ensure main thread name is restored after test
2285+
self.addCleanup(_thread.set_name, _thread._get_name())
2286+
22842287
# set_name() limit in bytes
22852288
truncate = getattr(_thread, "_NAME_MAXLEN", None)
22862289
limit = truncate or 100
@@ -2320,7 +2323,8 @@ def test_set_name(self):
23202323
tests.append(os_helper.TESTFN_UNENCODABLE)
23212324

23222325
if sys.platform.startswith("sunos"):
2323-
encoding = "utf-8"
2326+
# Use ASCII encoding on Solaris/Illumos/OpenIndiana
2327+
encoding = "ascii"
23242328
else:
23252329
encoding = sys.getfilesystemencoding()
23262330

@@ -2336,7 +2340,7 @@ def work():
23362340
if truncate is not None:
23372341
encoded = encoded[:truncate]
23382342
if sys.platform.startswith("sunos"):
2339-
expected = encoded.decode("utf-8", "surrogateescape")
2343+
expected = encoded.decode("ascii", "surrogateescape")
23402344
else:
23412345
expected = os.fsdecode(encoded)
23422346
else:
@@ -2355,7 +2359,11 @@ def work():
23552359
if '\0' in expected:
23562360
expected = expected.split('\0', 1)[0]
23572361

2358-
with self.subTest(name=name, expected=expected):
2362+
with self.subTest(name=name, expected=expected, thread="main"):
2363+
_thread.set_name(name)
2364+
self.assertEqual(_thread._get_name(), expected)
2365+
2366+
with self.subTest(name=name, expected=expected, thread="worker"):
23592367
work_name = None
23602368
thread = threading.Thread(target=work, name=name)
23612369
thread.start()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ Weilin Du
483483
John DuBois
484484
Paul Dubois
485485
Jacques Ducasse
486+
Jadon Duff
486487
Andrei Dorian Duma
487488
Graham Dumpleton
488489
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
@@ -2523,7 +2523,9 @@ _thread__get_name_impl(PyObject *module)
25232523
}
25242524

25252525
#ifdef __sun
2526-
return PyUnicode_DecodeUTF8(name, strlen(name), "surrogateescape");
2526+
// gh-138004: Decode Solaris/Illumos (e.g. OpenIndiana) thread names
2527+
// from ASCII, since OpenIndiana only supports ASCII names.
2528+
return PyUnicode_DecodeASCII(name, strlen(name), "surrogateescape");
25272529
#else
25282530
return PyUnicode_DecodeFSDefault(name);
25292531
#endif
@@ -2561,8 +2563,9 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
25612563
{
25622564
#ifndef MS_WINDOWS
25632565
#ifdef __sun
2564-
// Solaris always uses UTF-8
2565-
const char *encoding = "utf-8";
2566+
// gh-138004: Encode Solaris/Illumos thread names to ASCII,
2567+
// since OpenIndiana does not support non-ASCII names.
2568+
const char *encoding = "ascii";
25662569
#else
25672570
// Encode the thread name to the filesystem encoding using the "replace"
25682571
// error handler

0 commit comments

Comments
 (0)