(self)
| 2380 | @unittest.skipUnless(hasattr(_thread, 'set_name'), "missing _thread.set_name") |
| 2381 | @unittest.skipUnless(hasattr(_thread, '_get_name'), "missing _thread._get_name") |
| 2382 | def test_set_name(self): |
| 2383 | # Ensure main thread name is restored after test |
| 2384 | self.addCleanup(_thread.set_name, _thread._get_name()) |
| 2385 | |
| 2386 | # set_name() limit in bytes |
| 2387 | truncate = getattr(_thread, "_NAME_MAXLEN", None) |
| 2388 | limit = truncate or 100 |
| 2389 | |
| 2390 | tests = [ |
| 2391 | # test short ASCII name |
| 2392 | "CustomName", |
| 2393 | |
| 2394 | # test short non-ASCII name |
| 2395 | "namé€", |
| 2396 | |
| 2397 | # embedded null character: name is truncated |
| 2398 | # at the first null character |
| 2399 | "embed\0null", |
| 2400 | |
| 2401 | # Test long ASCII names (not truncated) |
| 2402 | "x" * limit, |
| 2403 | |
| 2404 | # Test long ASCII names (truncated) |
| 2405 | "x" * (limit + 10), |
| 2406 | |
| 2407 | # Test long non-ASCII name (truncated) |
| 2408 | "x" * (limit - 1) + "é€", |
| 2409 | |
| 2410 | # Test long non-BMP names (truncated) creating surrogate pairs |
| 2411 | # on Windows |
| 2412 | "x" * (limit - 1) + "\U0010FFFF", |
| 2413 | "x" * (limit - 2) + "\U0010FFFF" * 2, |
| 2414 | "x" + "\U0001f40d" * limit, |
| 2415 | "xx" + "\U0001f40d" * limit, |
| 2416 | "xxx" + "\U0001f40d" * limit, |
| 2417 | "xxxx" + "\U0001f40d" * limit, |
| 2418 | ] |
| 2419 | if os_helper.FS_NONASCII: |
| 2420 | tests.append(f"nonascii:{os_helper.FS_NONASCII}") |
| 2421 | if os_helper.TESTFN_UNENCODABLE: |
| 2422 | tests.append(os_helper.TESTFN_UNENCODABLE) |
| 2423 | |
| 2424 | if sys.platform.startswith("sunos"): |
| 2425 | # Use ASCII encoding on Solaris/Illumos/OpenIndiana |
| 2426 | encoding = "ascii" |
| 2427 | else: |
| 2428 | encoding = sys.getfilesystemencoding() |
| 2429 | |
| 2430 | def work(): |
| 2431 | nonlocal work_name |
| 2432 | work_name = _thread._get_name() |
| 2433 | |
| 2434 | for name in tests: |
| 2435 | if not support.MS_WINDOWS: |
| 2436 | encoded = name.encode(encoding, "replace") |
| 2437 | if b'\0' in encoded: |
| 2438 | encoded = encoded.split(b'\0', 1)[0] |
| 2439 | if truncate is not None: |
nothing calls this directly
no test coverage detected