]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-138004: Fix setting a thread name on OpenIndiana (GH-138017)
authorjadonduff <jadon_duff@icloud.com>
Tue, 2 Sep 2025 15:26:25 +0000 (11:26 -0400)
committerGitHub <noreply@github.com>
Tue, 2 Sep 2025 15:26:25 +0000 (15:26 +0000)
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>
Lib/test/test_threading.py
Misc/ACKS
Misc/NEWS.d/next/Core_and_Builtins/2025-08-21-06-31-42.gh-issue-138004.FH2Hre.rst [new file with mode: 0644]
Modules/_threadmodule.c

index 0ba78b9a1807d2a2d221a6442a05936e1b4de242..6e612b06281ca21d3becf4ffe21562b5eb84bac7 100644 (file)
@@ -2281,6 +2281,9 @@ class MiscTestCase(unittest.TestCase):
     @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 @@ class MiscTestCase(unittest.TestCase):
             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 @@ class MiscTestCase(unittest.TestCase):
                 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 @@ class MiscTestCase(unittest.TestCase):
                 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()
index dc28ccf8f57edae05244c3a8f2bb57072ce138ae..37b7988606fa99f6ae26ba0bb0c4d2a0575bcad2 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -483,6 +483,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 (file)
index 0000000..e73be99
--- /dev/null
@@ -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.
index 1a64289ea01fdbe7f4e3d6ffa77b6c9c1cc2fd8a..a436a553db980246243a8e6d174e6b5a3392e4ad 100644 (file)
@@ -2523,7 +2523,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
@@ -2561,8 +2563,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