gh-59705: Set OS thread name when Thread.name is changed (#127702)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
This commit is contained in:
Victor Stinner 2024-12-10 17:33:11 +01:00 committed by GitHub
parent 9af96f4406
commit c91ccbe4ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 43 additions and 7 deletions

View file

@ -2164,6 +2164,25 @@ class MiscTestCase(unittest.TestCase):
self.assertEqual(work_name, expected,
f"{len(work_name)=} and {len(expected)=}")
@unittest.skipUnless(hasattr(_thread, 'set_name'), "missing _thread.set_name")
@unittest.skipUnless(hasattr(_thread, '_get_name'), "missing _thread._get_name")
def test_change_name(self):
# Change the name of a thread while the thread is running
name1 = None
name2 = None
def work():
nonlocal name1, name2
name1 = _thread._get_name()
threading.current_thread().name = "new name"
name2 = _thread._get_name()
thread = threading.Thread(target=work, name="name")
thread.start()
thread.join()
self.assertEqual(name1, "name")
self.assertEqual(name2, "new name")
class InterruptMainTests(unittest.TestCase):
def check_interrupt_main_with_signal_handler(self, signum):