https://github.com/python/cpython/commit/ffc67b3505cdf7c4e7fa50a7916a06accf4961ba
commit: ffc67b3505cdf7c4e7fa50a7916a06accf4961ba
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: vstinner <[email protected]>
date: 2025-10-07T20:58:35+02:00
summary:

[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 c19db1d2b8935b6f4f775a0957a076f1864fbf80)

Co-authored-by: jadonduff <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-08-21-06-31-42.gh-issue-138004.FH2Hre.rst
M Lib/test/test_threading.py
M Misc/ACKS
M Modules/_threadmodule.c

diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
index 731e1046c000af..59db91b0ffce5e 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -2254,6 +2254,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
@@ -2293,7 +2296,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()
 
@@ -2309,7 +2313,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:
@@ -2328,7 +2332,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()
diff --git a/Misc/ACKS b/Misc/ACKS
index ee3d66a04f0217..1730eb71cb86eb 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -482,6 +482,7 @@ Weilin Du
 John DuBois
 Paul Dubois
 Jacques Ducasse
+Jadon Duff
 Andrei Dorian Duma
 Graham Dumpleton
 Quinn Dunkan
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2025-08-21-06-31-42.gh-issue-138004.FH2Hre.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-08-21-06-31-42.gh-issue-138004.FH2Hre.rst
new file mode 100644
index 00000000000000..e73be998f4be7b
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-08-21-06-31-42.gh-issue-138004.FH2Hre.rst
@@ -0,0 +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.
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index 26d3c0a2266b71..1389a1ef2c1b23 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -2474,7 +2474,9 @@ _thread__get_name_impl(PyObject *module)
     }
 
 #ifdef __sun
-    return PyUnicode_DecodeUTF8(name, strlen(name), "surrogateescape");
+    // gh-138004: Decode Solaris/Illumos (e.g. OpenIndiana) thread names
+    // from ASCII, since OpenIndiana only supports ASCII names.
+    return PyUnicode_DecodeASCII(name, strlen(name), "surrogateescape");
 #else
     return PyUnicode_DecodeFSDefault(name);
 #endif
@@ -2512,8 +2514,9 @@ _thread_set_name_impl(PyObject *module, PyObject 
*name_obj)
 {
 #ifndef MS_WINDOWS
 #ifdef __sun
-    // Solaris always uses UTF-8
-    const char *encoding = "utf-8";
+    // gh-138004: Encode Solaris/Illumos thread names to ASCII,
+    // since OpenIndiana does not support non-ASCII names.
+    const char *encoding = "ascii";
 #else
     // Encode the thread name to the filesystem encoding using the "replace"
     // error handler

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to