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.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
jadonduff 2025-09-02 11:26:25 -04:00 committed by GitHub
parent 8d5c3341c5
commit c19db1d2b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 19 additions and 6 deletions

View file

@ -2281,6 +2281,9 @@ def test__all__(self):
@unittest.skipUnless(hasattr(_thread, 'set_name'), "missing _thread.set_name")
@unittest.skipUnless(hasattr(_thread, '_get_name'), "missing _thread._get_name")
def test_set_name(self):
# Ensure main thread name is restored after test
self.addCleanup(_thread.set_name, _thread._get_name())
# set_name() limit in bytes
truncate = getattr(_thread, "_NAME_MAXLEN", None)
limit = truncate or 100
@ -2320,7 +2323,8 @@ def test_set_name(self):
tests.append(os_helper.TESTFN_UNENCODABLE)
if sys.platform.startswith("sunos"):
encoding = "utf-8"
# Use ASCII encoding on Solaris/Illumos/OpenIndiana
encoding = "ascii"
else:
encoding = sys.getfilesystemencoding()
@ -2336,7 +2340,7 @@ def work():
if truncate is not None:
encoded = encoded[:truncate]
if sys.platform.startswith("sunos"):
expected = encoded.decode("utf-8", "surrogateescape")
expected = encoded.decode("ascii", "surrogateescape")
else:
expected = os.fsdecode(encoded)
else:
@ -2355,7 +2359,11 @@ def work():
if '\0' in expected:
expected = expected.split('\0', 1)[0]
with self.subTest(name=name, expected=expected):
with self.subTest(name=name, expected=expected, thread="main"):
_thread.set_name(name)
self.assertEqual(_thread._get_name(), expected)
with self.subTest(name=name, expected=expected, thread="worker"):
work_name = None
thread = threading.Thread(target=work, name=name)
thread.start()