mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
gh-128308: pass **kwargs
to asyncio task_factory (#128768)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This commit is contained in:
parent
6c914bf85c
commit
38a9956876
8 changed files with 48 additions and 29 deletions
|
@ -833,8 +833,8 @@ class BaseEventLoopTests(test_utils.TestCase):
|
|||
loop.close()
|
||||
|
||||
def test_create_named_task_with_custom_factory(self):
|
||||
def task_factory(loop, coro):
|
||||
return asyncio.Task(coro, loop=loop)
|
||||
def task_factory(loop, coro, **kwargs):
|
||||
return asyncio.Task(coro, loop=loop, **kwargs)
|
||||
|
||||
async def test():
|
||||
pass
|
||||
|
|
|
@ -302,6 +302,18 @@ class CEagerTaskFactoryLoopTests(EagerTaskFactoryLoopTests, test_utils.TestCase)
|
|||
|
||||
self.run_coro(run())
|
||||
|
||||
def test_name(self):
|
||||
name = None
|
||||
async def coro():
|
||||
nonlocal name
|
||||
name = asyncio.current_task().get_name()
|
||||
|
||||
async def main():
|
||||
task = self.loop.create_task(coro(), name="test name")
|
||||
self.assertEqual(name, "test name")
|
||||
await task
|
||||
|
||||
self.run_coro(coro())
|
||||
|
||||
class AsyncTaskCounter:
|
||||
def __init__(self, loop, *, task_class, eager):
|
||||
|
|
|
@ -112,8 +112,8 @@ class TestPyFreeThreading(TestFreeThreading, TestCase):
|
|||
all_tasks = staticmethod(asyncio.tasks._py_all_tasks)
|
||||
current_task = staticmethod(asyncio.tasks._py_current_task)
|
||||
|
||||
def factory(self, loop, coro, context=None):
|
||||
return asyncio.tasks._PyTask(coro, loop=loop, context=context)
|
||||
def factory(self, loop, coro, **kwargs):
|
||||
return asyncio.tasks._PyTask(coro, loop=loop, **kwargs)
|
||||
|
||||
|
||||
@unittest.skipUnless(hasattr(asyncio.tasks, "_c_all_tasks"), "requires _asyncio")
|
||||
|
@ -121,16 +121,16 @@ class TestCFreeThreading(TestFreeThreading, TestCase):
|
|||
all_tasks = staticmethod(getattr(asyncio.tasks, "_c_all_tasks", None))
|
||||
current_task = staticmethod(getattr(asyncio.tasks, "_c_current_task", None))
|
||||
|
||||
def factory(self, loop, coro, context=None):
|
||||
return asyncio.tasks._CTask(coro, loop=loop, context=context)
|
||||
def factory(self, loop, coro, **kwargs):
|
||||
return asyncio.tasks._CTask(coro, loop=loop, **kwargs)
|
||||
|
||||
|
||||
class TestEagerPyFreeThreading(TestPyFreeThreading):
|
||||
def factory(self, loop, coro, context=None):
|
||||
return asyncio.tasks._PyTask(coro, loop=loop, context=context, eager_start=True)
|
||||
def factory(self, loop, coro, eager_start=True, **kwargs):
|
||||
return asyncio.tasks._PyTask(coro, loop=loop, **kwargs, eager_start=eager_start)
|
||||
|
||||
|
||||
@unittest.skipUnless(hasattr(asyncio.tasks, "_c_all_tasks"), "requires _asyncio")
|
||||
class TestEagerCFreeThreading(TestCFreeThreading, TestCase):
|
||||
def factory(self, loop, coro, context=None):
|
||||
return asyncio.tasks._CTask(coro, loop=loop, context=context, eager_start=True)
|
||||
def factory(self, loop, coro, eager_start=True, **kwargs):
|
||||
return asyncio.tasks._CTask(coro, loop=loop, **kwargs, eager_start=eager_start)
|
||||
|
|
|
@ -1040,6 +1040,18 @@ class BaseTestTaskGroup:
|
|||
self.assertIsNotNone(exc)
|
||||
self.assertListEqual(gc.get_referrers(exc), no_other_refs())
|
||||
|
||||
async def test_name(self):
|
||||
name = None
|
||||
|
||||
async def asyncfn():
|
||||
nonlocal name
|
||||
name = asyncio.current_task().get_name()
|
||||
|
||||
async with asyncio.TaskGroup() as tg:
|
||||
tg.create_task(asyncfn(), name="example name")
|
||||
|
||||
self.assertEqual(name, "example name")
|
||||
|
||||
|
||||
class TestTaskGroup(BaseTestTaskGroup, unittest.IsolatedAsyncioTestCase):
|
||||
loop_factory = asyncio.EventLoop
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue