Skip to content

Commit ffc67b3

Browse files
miss-islingtonjadonduffserhiy-storchaka
authored
[3.14] gh-138004: Fix setting a thread name on OpenIndiana (GH-138017) (#138384)
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 a893270 commit ffc67b3

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
@@ -2254,6 +2254,9 @@ def test__all__(self):
22542254
@unittest.skipUnless(hasattr(_thread, 'set_name'), "missing _thread.set_name")
22552255
@unittest.skipUnless(hasattr(_thread, '_get_name'), "missing _thread._get_name")
22562256
def test_set_name(self):
2257+
# Ensure main thread name is restored after test
2258+
self.addCleanup(_thread.set_name, _thread._get_name())
2259+
22572260
# set_name() limit in bytes
22582261
truncate = getattr(_thread, "_NAME_MAXLEN", None)
22592262
limit = truncate or 100
@@ -2293,7 +2296,8 @@ def test_set_name(self):
22932296
tests.append(os_helper.TESTFN_UNENCODABLE)
22942297

22952298
if sys.platform.startswith("sunos"):
2296-
encoding = "utf-8"
2299+
# Use ASCII encoding on Solaris/Illumos/OpenIndiana
2300+
encoding = "ascii"
22972301
else:
22982302
encoding = sys.getfilesystemencoding()
22992303

@@ -2309,7 +2313,7 @@ def work():
23092313
if truncate is not None:
23102314
encoded = encoded[:truncate]
23112315
if sys.platform.startswith("sunos"):
2312-
expected = encoded.decode("utf-8", "surrogateescape")
2316+
expected = encoded.decode("ascii", "surrogateescape")
23132317
else:
23142318
expected = os.fsdecode(encoded)
23152319
else:
@@ -2328,7 +2332,11 @@ def work():
23282332
if '\0' in expected:
23292333
expected = expected.split('\0', 1)[0]
23302334

2331-
with self.subTest(name=name, expected=expected):
2335+
with self.subTest(name=name, expected=expected, thread="main"):
2336+
_thread.set_name(name)
2337+
self.assertEqual(_thread._get_name(), expected)
2338+
2339+
with self.subTest(name=name, expected=expected, thread="worker"):
23322340
work_name = None
23332341
thread = threading.Thread(target=work, name=name)
23342342
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
@@ -2474,7 +2474,9 @@ _thread__get_name_impl(PyObject *module)
24742474
}
24752475

24762476
#ifdef __sun
2477-
return PyUnicode_DecodeUTF8(name, strlen(name), "surrogateescape");
2477+
// gh-138004: Decode Solaris/Illumos (e.g. OpenIndiana) thread names
2478+
// from ASCII, since OpenIndiana only supports ASCII names.
2479+
return PyUnicode_DecodeASCII(name, strlen(name), "surrogateescape");
24782480
#else
24792481
return PyUnicode_DecodeFSDefault(name);
24802482
#endif
@@ -2512,8 +2514,9 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
25122514
{
25132515
#ifndef MS_WINDOWS
25142516
#ifdef __sun
2515-
// Solaris always uses UTF-8
2516-
const char *encoding = "utf-8";
2517+
// gh-138004: Encode Solaris/Illumos thread names to ASCII,
2518+
// since OpenIndiana does not support non-ASCII names.
2519+
const char *encoding = "ascii";
25172520
#else
25182521
// Encode the thread name to the filesystem encoding using the "replace"
25192522
// error handler

0 commit comments

Comments
 (0)