]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112622: Pass name to loop create_task method (#112623)
authorJamie <101677823+ordinary-jamie@users.noreply.github.com>
Wed, 13 Dec 2023 01:26:40 +0000 (12:26 +1100)
committerGitHub <noreply@github.com>
Wed, 13 Dec 2023 01:26:40 +0000 (17:26 -0800)
This affects task creation through either `asyncio.create_task()` or `TaskGroup.create_task()` -- the redundant call to `task.set_name()` is skipped. We still call `set_name()` when a task factory is involved, because the task factory call signature (unfortunately) doesn't take a `name` argument.

Lib/asyncio/taskgroups.py
Lib/asyncio/tasks.py
Misc/NEWS.d/next/Library/2023-12-03-01-01-52.gh-issue-112622.1Z8cpx.rst [new file with mode: 0644]

index 91be0decc41c4265df5dfab0fc4c907c1617663f..cb9c1ce4d7d1d29f2f259c877446d277e1060fd1 100644 (file)
@@ -158,10 +158,10 @@ class TaskGroup:
         if self._aborting:
             raise RuntimeError(f"TaskGroup {self!r} is shutting down")
         if context is None:
-            task = self._loop.create_task(coro)
+            task = self._loop.create_task(coro, name=name)
         else:
-            task = self._loop.create_task(coro, context=context)
-        task.set_name(name)
+            task = self._loop.create_task(coro, name=name, context=context)
+
         # optimization: Immediately call the done callback if the task is
         # already done (e.g. if the coro was able to complete eagerly),
         # and skip scheduling a done callback
index e84b21390557beb07d0e0d9db2137a00e73ca0b2..fafee3e738f6aa36512ea36ceffee46b54f46127 100644 (file)
@@ -404,11 +404,10 @@ def create_task(coro, *, name=None, context=None):
     loop = events.get_running_loop()
     if context is None:
         # Use legacy API if context is not needed
-        task = loop.create_task(coro)
+        task = loop.create_task(coro, name=name)
     else:
-        task = loop.create_task(coro, context=context)
+        task = loop.create_task(coro, name=name, context=context)
 
-    task.set_name(name)
     return task
 
 
diff --git a/Misc/NEWS.d/next/Library/2023-12-03-01-01-52.gh-issue-112622.1Z8cpx.rst b/Misc/NEWS.d/next/Library/2023-12-03-01-01-52.gh-issue-112622.1Z8cpx.rst
new file mode 100644 (file)
index 0000000..91c88ba
--- /dev/null
@@ -0,0 +1,2 @@
+Ensure ``name`` parameter is passed to event loop in
+:func:`asyncio.create_task`.