Skip to content

Commit 5874f08

Browse files
Revert "[3.13] pythongh-128308: pass **kwargs to asyncio task_factory (pythonGH-128768) (python#130084)"
This reverts commit 7b0543e.
1 parent 9718880 commit 5874f08

File tree

6 files changed

+21
-39
lines changed

6 files changed

+21
-39
lines changed

Doc/library/asyncio-eventloop.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,9 @@ Creating Futures and Tasks
384384

385385
If *factory* is ``None`` the default task factory will be set.
386386
Otherwise, *factory* must be a *callable* with the signature matching
387-
``(loop, coro, **kwargs)``, where *loop* is a reference to the active
387+
``(loop, coro, context=None)``, where *loop* is a reference to the active
388388
event loop, and *coro* is a coroutine object. The callable
389-
must pass on all *kwargs*, and return a :class:`asyncio.Task`-compatible object.
389+
must return a :class:`asyncio.Future`-compatible object.
390390

391391
.. method:: loop.get_task_factory()
392392

Lib/asyncio/base_events.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -458,18 +458,25 @@ def create_future(self):
458458
"""Create a Future object attached to the loop."""
459459
return futures.Future(loop=self)
460460

461-
def create_task(self, coro, **kwargs):
461+
def create_task(self, coro, *, name=None, context=None):
462462
"""Schedule a coroutine object.
463463
464464
Return a task object.
465465
"""
466466
self._check_closed()
467-
if self._task_factory is not None:
468-
return self._task_factory(self, coro, **kwargs)
467+
if self._task_factory is None:
468+
task = tasks.Task(coro, loop=self, name=name, context=context)
469+
if task._source_traceback:
470+
del task._source_traceback[-1]
471+
else:
472+
if context is None:
473+
# Use legacy API if context is not needed
474+
task = self._task_factory(self, coro)
475+
else:
476+
task = self._task_factory(self, coro, context=context)
477+
478+
task.set_name(name)
469479

470-
task = tasks.Task(coro, loop=self, **kwargs)
471-
if task._source_traceback:
472-
del task._source_traceback[-1]
473480
try:
474481
return task
475482
finally:
@@ -483,10 +490,9 @@ def set_task_factory(self, factory):
483490
If factory is None the default task factory will be set.
484491
485492
If factory is a callable, it should have a signature matching
486-
'(loop, coro, **kwargs)', where 'loop' will be a reference to the active
487-
event loop, 'coro' will be a coroutine object, and **kwargs will be
488-
arbitrary keyword arguments that should be passed on to Task.
489-
The callable must return a Task.
493+
'(loop, coro)', where 'loop' will be a reference to the active
494+
event loop, 'coro' will be a coroutine object. The callable
495+
must return a Future.
490496
"""
491497
if factory is not None and not callable(factory):
492498
raise TypeError('task factory must be a callable or None')

Lib/asyncio/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def create_future(self):
292292

293293
# Method scheduling a coroutine object: create a task.
294294

295-
def create_task(self, coro, **kwargs):
295+
def create_task(self, coro, *, name=None, context=None):
296296
raise NotImplementedError
297297

298298
# Methods for interacting with threads.

Lib/test/test_asyncio/test_base_events.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,8 @@ async def test():
838838
loop.close()
839839

840840
def test_create_named_task_with_custom_factory(self):
841-
def task_factory(loop, coro, **kwargs):
842-
return asyncio.Task(coro, loop=loop, **kwargs)
841+
def task_factory(loop, coro):
842+
return asyncio.Task(coro, loop=loop)
843843

844844
async def test():
845845
pass

Lib/test/test_asyncio/test_eager_task_factory.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,18 +302,6 @@ async def run():
302302

303303
self.run_coro(run())
304304

305-
def test_name(self):
306-
name = None
307-
async def coro():
308-
nonlocal name
309-
name = asyncio.current_task().get_name()
310-
311-
async def main():
312-
task = self.loop.create_task(coro(), name="test name")
313-
self.assertEqual(name, "test name")
314-
await task
315-
316-
self.run_coro(coro())
317305

318306
class AsyncTaskCounter:
319307
def __init__(self, loop, *, task_class, eager):

Lib/test/test_asyncio/test_taskgroups.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,18 +1081,6 @@ async def throw_error():
10811081
# cancellation happens here and error is more understandable
10821082
await asyncio.sleep(0)
10831083

1084-
async def test_name(self):
1085-
name = None
1086-
1087-
async def asyncfn():
1088-
nonlocal name
1089-
name = asyncio.current_task().get_name()
1090-
1091-
async with asyncio.TaskGroup() as tg:
1092-
tg.create_task(asyncfn(), name="example name")
1093-
1094-
self.assertEqual(name, "example name")
1095-
10961084

10971085
class TestTaskGroup(BaseTestTaskGroup, unittest.IsolatedAsyncioTestCase):
10981086
loop_factory = asyncio.EventLoop

0 commit comments

Comments
 (0)