Skip to content

Commit 46721bb

Browse files
committed
Catch UnicodeEncodeError when seting the name
Refactor also tests.
1 parent 6ea7e5a commit 46721bb

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

Lib/test/test_threading.py

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,57 +2114,52 @@ def work():
21142114
nonlocal work_name
21152115
work_name = get_name()
21162116

2117-
# name not too long to fit into Linux 15 bytes limit
2118-
name = "CustomName"
2119-
tests = [(name, name)]
2120-
2121-
# Test non-ASCII short name
2122-
name = "namé€"
2123-
try:
2124-
os.fsencode(name)
2125-
except UnicodeEncodeError:
2126-
# name cannot be encoded to the filesystem encoding
2127-
pass
2128-
else:
2129-
tests.append((name, name))
2130-
2117+
# On Linux and macOS, set_name() truncates the name to,
2118+
# respectively, 15 and 63 bytes.
21312119
if sys.platform == "linux":
2132-
limit = 15
2120+
truncate = 15
21332121
elif sys.platform == "darwin":
2134-
limit = 63
2122+
truncate = 63
21352123
else:
2136-
limit = None
2137-
2138-
if limit is not None:
2139-
# On Linux and macOS, set_name() truncates the name to,
2140-
# respectively, 15 and 63 bytes.
2124+
truncate = None
2125+
limit = truncate or 100
21412126

2142-
# Test ASCII name
2143-
name = "x" * 100
2144-
tests.append((name, name[:limit]))
2145-
2146-
# Test non-ASCII name
2147-
name = "x" * (limit - 1) + "é€"
2127+
def create_test(name):
21482128
try:
21492129
encoded = os.fsencode(name)
21502130
except UnicodeEncodeError:
2151-
# name cannot be encoded to the filesystem encoding
2152-
pass
2131+
expected = None
21532132
else:
2154-
expected = os.fsdecode(encoded[:limit])
2155-
tests.append((name, expected))
2156-
else:
2157-
# Test long name
2158-
name = "x" * 100
2159-
tests.append((name, name))
2133+
if truncate is not None:
2134+
expected = os.fsdecode(encoded[:truncate])
2135+
else:
2136+
expected = name
2137+
return (name, expected)
2138+
2139+
tests = [
2140+
# test short ASCII name
2141+
create_test("CustomName"),
2142+
2143+
# test short non-ASCII name
2144+
create_test("namé€"),
21602145

2146+
# Test long ASCII names (not truncated)
2147+
create_test("x" * limit),
2148+
2149+
# Test long ASCII names (truncated)
2150+
create_test("x" * (limit + 10)),
2151+
2152+
# Test long non-ASCII name (truncated)
2153+
create_test("x" * (limit - 1) + "é€"),
2154+
]
21612155
for name, expected in tests:
21622156
with self.subTest(name=name, expected=expected):
21632157
work_name = None
21642158
thread = threading.Thread(target=work, name=name)
21652159
thread.start()
21662160
thread.join()
2167-
self.assertEqual(work_name, expected)
2161+
if expected is not None:
2162+
self.assertEqual(work_name, expected)
21682163

21692164

21702165
class InterruptMainTests(unittest.TestCase):

Lib/threading.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,12 @@ def _bootstrap_inner(self):
10321032
if _HAVE_THREAD_NATIVE_ID:
10331033
self._set_native_id()
10341034
if _set_name is not None and self._name:
1035-
_set_name(self._name)
1035+
try:
1036+
_set_name(self._name)
1037+
except UnicodeEncodeError:
1038+
# cannot set the name if the name cannot be encoded
1039+
# to the filesystem encoding
1040+
pass
10361041
self._started.set()
10371042
with _active_limbo_lock:
10381043
_active[self._ident] = self

0 commit comments

Comments
 (0)