aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/threading.py
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-09-23 23:21:19 +0200
committerGitHub <noreply@github.com>2020-09-23 23:21:19 +0200
commit98c16c991d6e70a48f4280a7cd464d807bdd9f2b (patch)
treefce606ea267df285b5c1149fea97340389701b8b /Lib/threading.py
parent2e4dd336e5b50fd30947fdecb605ddcd71f7f6f5 (diff)
downloadcpython-98c16c991d6e70a48f4280a7cd464d807bdd9f2b.tar.gz
cpython-98c16c991d6e70a48f4280a7cd464d807bdd9f2b.zip
bpo-41833: threading.Thread now uses the target name (GH-22357)
Diffstat (limited to 'Lib/threading.py')
-rw-r--r--Lib/threading.py20
1 files changed, 15 insertions, 5 deletions
diff --git a/Lib/threading.py b/Lib/threading.py
index ab29db77a74..06c77f70fe7 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -745,10 +745,9 @@ class BrokenBarrierError(RuntimeError):
# Helper to generate new thread names
-_counter = _count().__next__
-_counter() # Consume 0 so first non-main thread has id 1.
-def _newname(template="Thread-%d"):
- return template % _counter()
+_counter = _count(1).__next__
+def _newname(name_template):
+ return name_template % _counter()
# Active thread administration
_active_limbo_lock = _allocate_lock()
@@ -800,8 +799,19 @@ class Thread:
assert group is None, "group argument must be None for now"
if kwargs is None:
kwargs = {}
+ if name:
+ name = str(name)
+ else:
+ name = _newname("Thread-%d")
+ if target is not None:
+ try:
+ target_name = target.__name__
+ name += f" ({target_name})"
+ except AttributeError:
+ pass
+
self._target = target
- self._name = str(name or _newname())
+ self._name = name
self._args = args
self._kwargs = kwargs
if daemon is not None: