summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_asyncio
diff options
context:
space:
mode:
authorItamar Ostricher <itamarost@gmail.com>2023-04-29 15:20:09 (GMT)
committerGitHub <noreply@github.com>2023-04-29 15:20:09 (GMT)
commit85c7bf5bcec07beea6064976e6199195cd34329d (patch)
tree5515f9539c36a6ac1e3e231a1a297e403d9d6928 /Lib/test/test_asyncio
parentfbf3596c3edadd03b5a8c659e9f27a09e5d1a051 (diff)
downloadcpython-85c7bf5bcec07beea6064976e6199195cd34329d.zip
cpython-85c7bf5bcec07beea6064976e6199195cd34329d.tar.gz
cpython-85c7bf5bcec07beea6064976e6199195cd34329d.tar.bz2
gh-103793: Defer formatting task name (#103767)
The default task name is "Task-<counter>" (if no name is passed in during Task creation). This is initialized in `Task.__init__` (C impl) using string formatting, which can be quite slow. Actually using the task name in real world code is not very common, so this is wasted init. Let's defer this string formatting to the first time the name is read (in `get_name` impl), so we don't need to pay the string formatting cost if the task name is never read. We don't change the order in which tasks are assigned numbers (if they are) -- the number is set on task creation, as a PyLong instead of a formatted string. Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Diffstat (limited to 'Lib/test/test_asyncio')
-rw-r--r--Lib/test/test_asyncio/test_tasks.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index 31622c9..6e8a51c 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -399,6 +399,18 @@ class BaseTaskTests:
self.loop.run_until_complete(t1)
self.loop.run_until_complete(t2)
+ def test_task_set_name_pylong(self):
+ # test that setting the task name to a PyLong explicitly doesn't
+ # incorrectly trigger the deferred name formatting logic
+ async def notmuch():
+ return 123
+
+ t = self.new_task(self.loop, notmuch(), name=987654321)
+ self.assertEqual(t.get_name(), '987654321')
+ t.set_name(123456789)
+ self.assertEqual(t.get_name(), '123456789')
+ self.loop.run_until_complete(t)
+
def test_task_repr_name_not_str(self):
async def notmuch():
return 123