[3.13] gh-133745: Fix asyncio task factory name/context kwarg breaks (#133948)

In 3.13.3 we accidentally broke the interface for custom task factory. Factory authors added workarounds.
This PR (for 3.13.4) unbreaks task factories that haven't made a workaround yet while also supporting those that have.

NOTE: The custom task factory API will change to what we accidentally released in 3.13.3.

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
This commit is contained in:
Thomas Grainger 2025-05-18 14:50:07 +01:00 committed by GitHub
parent ebe54d7ab7
commit fd6a602d04
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 32 additions and 18 deletions

View file

@ -458,18 +458,24 @@ class BaseEventLoop(events.AbstractEventLoop):
"""Create a Future object attached to the loop."""
return futures.Future(loop=self)
def create_task(self, coro, **kwargs):
def create_task(self, coro, *, name=None, context=None, **kwargs):
"""Schedule a coroutine object.
Return a task object.
"""
self._check_closed()
if self._task_factory is not None:
return self._task_factory(self, coro, **kwargs)
if context is not None:
kwargs["context"] = context
task = self._task_factory(self, coro, **kwargs)
task.set_name(name)
else:
task = tasks.Task(coro, loop=self, name=name, context=context, **kwargs)
if task._source_traceback:
del task._source_traceback[-1]
task = tasks.Task(coro, loop=self, **kwargs)
if task._source_traceback:
del task._source_traceback[-1]
try:
return task
finally: