Skip to content

Commit 508f24d

Browse files
committed
add test for main and encode ascii
1 parent c67e93d commit 508f24d

1 file changed

Lines changed: 10 additions & 41 deletions

File tree

Lib/test/test_threading.py

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,7 +2320,8 @@ def test_set_name(self):
23202320
tests.append(os_helper.TESTFN_UNENCODABLE)
23212321

23222322
if sys.platform.startswith("sunos"):
2323-
encoding = "utf-8"
2323+
# Use ASCII encoding on Solaris/Illumos/OpenIndiana
2324+
encoding = "ascii"
23242325
else:
23252326
encoding = sys.getfilesystemencoding()
23262327

@@ -2336,7 +2337,7 @@ def work():
23362337
if truncate is not None:
23372338
encoded = encoded[:truncate]
23382339
if sys.platform.startswith("sunos"):
2339-
expected = encoded.decode("utf-8", "surrogateescape")
2340+
expected = encoded.decode("ascii", "replace")
23402341
else:
23412342
expected = os.fsdecode(encoded)
23422343
else:
@@ -2355,49 +2356,17 @@ def work():
23552356
if '\0' in expected:
23562357
expected = expected.split('\0', 1)[0]
23572358

2358-
with self.subTest(name=name, expected=expected):
2359+
with self.subTest(name=name, expected=expected, thread="main"):
2360+
_thread.set_name(name)
2361+
self.assertEqual(_thread._get_name(), expected)
2362+
2363+
with self.subTest(name=name, expected=expected, thread="worker"):
23592364
work_name = None
23602365
thread = threading.Thread(target=work, name=name)
23612366
thread.start()
23622367
thread.join()
2363-
2364-
# Detect if running on OpenIndiana / illumos
2365-
try:
2366-
is_illumos = os.uname().version.startswith('illumos')
2367-
except AttributeError:
2368-
is_illumos = False
2369-
2370-
if is_illumos:
2371-
# illumos requires ASCII-encoded thread names
2372-
if not work_name:
2373-
# name didn't get set (set_name may have failed)
2374-
self.skipTest(
2375-
f"Platform does not support non-ASCII thread names: got empty name for {name!r}"
2376-
)
2377-
2378-
work_name_bytes = (
2379-
work_name.encode('ascii', errors='ignore')
2380-
if isinstance(work_name, str)
2381-
else work_name
2382-
)
2383-
expected_bytes = expected.encode('ascii', errors='ignore')
2384-
2385-
self.assertEqual(
2386-
work_name_bytes,
2387-
expected_bytes,
2388-
f"{len(work_name)=} and {len(expected)=}"
2389-
)
2390-
2391-
elif not name.isascii() and not work_name:
2392-
# Platform does not support non-ASCII thread names
2393-
self.skipTest(
2394-
f"Platform does not support non-ASCII thread names: got empty name for {name!r}"
2395-
)
2396-
2397-
else:
2398-
# Most platforms
2399-
self.assertEqual(work_name, expected,
2400-
f"{len(work_name)=} and {len(expected)=}")
2368+
self.assertEqual(work_name, expected,
2369+
f"{len(work_name)=} and {len(expected)=}")
24012370

24022371
@unittest.skipUnless(hasattr(_thread, 'set_name'), "missing _thread.set_name")
24032372
@unittest.skipUnless(hasattr(_thread, '_get_name'), "missing _thread._get_name")

0 commit comments

Comments
 (0)