Skip to content

Commit 744ce34

Browse files
committed
gh-154695: Fix Task(eager_start=True) with no explicit loop
1 parent 2f4cca5 commit 744ce34

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

Lib/test/test_asyncio/test_tasks.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,6 +2733,18 @@ async def main():
27332733
self.assertEqual(name, "example")
27342734
await t
27352735

2736+
def test_eager_start_true_no_loop(self):
2737+
# gh-154695: eager_start must use the resolved loop, not loop=None.
2738+
async def asyncfn():
2739+
return 42
2740+
2741+
async def main():
2742+
t = self.__class__.Task(asyncfn(), eager_start=True)
2743+
self.assertTrue(t.done())
2744+
self.assertEqual(await t, 42)
2745+
2746+
asyncio.run(main(), loop_factory=asyncio.EventLoop)
2747+
27362748
def test_eager_start_false(self):
27372749
name = None
27382750

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :class:`asyncio.Task` raising :exc:`AttributeError` when created with
2+
``eager_start=True`` and no explicit *loop* argument.

Modules/_asynciomodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2364,7 +2364,9 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
23642364
_PyObject_SetMaybeWeakref((PyObject *)self);
23652365
#endif
23662366
if (eager_start) {
2367-
PyObject *res = PyObject_CallMethodNoArgs(loop, &_Py_ID(is_running));
2367+
// gh-154695: loop may be None here, future_init() resolved it into task_loop.
2368+
PyObject *res = PyObject_CallMethodNoArgs(self->task_loop,
2369+
&_Py_ID(is_running));
23682370
if (res == NULL) {
23692371
return -1;
23702372
}

0 commit comments

Comments
 (0)