mirror of
https://github.com/python/cpython.git
synced 2025-11-02 22:51:25 +00:00
bpo-41833: threading.Thread now uses the target name (GH-22357)
This commit is contained in:
parent
2e4dd336e5
commit
98c16c991d
4 changed files with 55 additions and 10 deletions
|
|
@ -20,6 +20,7 @@
|
|||
import signal
|
||||
import textwrap
|
||||
|
||||
from unittest import mock
|
||||
from test import lock_tests
|
||||
from test import support
|
||||
|
||||
|
|
@ -86,6 +87,33 @@ def tearDown(self):
|
|||
|
||||
class ThreadTests(BaseTestCase):
|
||||
|
||||
@cpython_only
|
||||
def test_name(self):
|
||||
def func(): pass
|
||||
|
||||
thread = threading.Thread(name="myname1")
|
||||
self.assertEqual(thread.name, "myname1")
|
||||
|
||||
# Convert int name to str
|
||||
thread = threading.Thread(name=123)
|
||||
self.assertEqual(thread.name, "123")
|
||||
|
||||
# target name is ignored if name is specified
|
||||
thread = threading.Thread(target=func, name="myname2")
|
||||
self.assertEqual(thread.name, "myname2")
|
||||
|
||||
with mock.patch.object(threading, '_counter', return_value=2):
|
||||
thread = threading.Thread(name="")
|
||||
self.assertEqual(thread.name, "Thread-2")
|
||||
|
||||
with mock.patch.object(threading, '_counter', return_value=3):
|
||||
thread = threading.Thread()
|
||||
self.assertEqual(thread.name, "Thread-3")
|
||||
|
||||
with mock.patch.object(threading, '_counter', return_value=5):
|
||||
thread = threading.Thread(target=func)
|
||||
self.assertEqual(thread.name, "Thread-5 (func)")
|
||||
|
||||
# Create a bunch of threads, let each do some work, wait until all are
|
||||
# done.
|
||||
def test_various_ops(self):
|
||||
|
|
@ -531,7 +559,7 @@ def test_main_thread_after_fork_from_nonmain_thread(self):
|
|||
import os, threading, sys
|
||||
from test import support
|
||||
|
||||
def f():
|
||||
def func():
|
||||
pid = os.fork()
|
||||
if pid == 0:
|
||||
main = threading.main_thread()
|
||||
|
|
@ -544,14 +572,14 @@ def f():
|
|||
else:
|
||||
support.wait_process(pid, exitcode=0)
|
||||
|
||||
th = threading.Thread(target=f)
|
||||
th = threading.Thread(target=func)
|
||||
th.start()
|
||||
th.join()
|
||||
"""
|
||||
_, out, err = assert_python_ok("-c", code)
|
||||
data = out.decode().replace('\r', '')
|
||||
self.assertEqual(err, b"")
|
||||
self.assertEqual(data, "Thread-1\nTrue\nTrue\n")
|
||||
self.assertEqual(data, "Thread-1 (func)\nTrue\nTrue\n")
|
||||
|
||||
def test_main_thread_during_shutdown(self):
|
||||
# bpo-31516: current_thread() should still point to the main thread
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue