Skip to content

Commit 99ac297

Browse files
deadlovelllmiss-islington
authored andcommitted
gh-154695: fix asyncio.Task eager_start=True with no explicit loop (GH-154697)
(cherry picked from commit f5f5059) Co-authored-by: Timofei <128279579+deadlovelll@users.noreply.github.com>
1 parent 1667a6c commit 99ac297

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
@@ -2752,6 +2752,18 @@ async def main():
27522752
self.assertEqual(name, "example")
27532753
await t
27542754

2755+
def test_eager_start_true_no_loop(self):
2756+
# gh-154695: eager_start must use the resolved loop, not loop=None.
2757+
async def asyncfn():
2758+
return 42
2759+
2760+
async def main():
2761+
t = self.__class__.Task(asyncfn(), eager_start=True)
2762+
self.assertTrue(t.done())
2763+
self.assertEqual(await t, 42)
2764+
2765+
asyncio.run(main(), loop_factory=asyncio.EventLoop)
2766+
27552767
def test_eager_start_false(self):
27562768
name = None
27572769

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
@@ -2372,7 +2372,9 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
23722372
_PyObject_SetMaybeWeakref((PyObject *)self);
23732373
#endif
23742374
if (eager_start) {
2375-
PyObject *res = PyObject_CallMethodNoArgs(loop, &_Py_ID(is_running));
2375+
// gh-154695: loop may be None here, future_init() resolved it into task_loop.
2376+
PyObject *res = PyObject_CallMethodNoArgs(self->task_loop,
2377+
&_Py_ID(is_running));
23762378
if (res == NULL) {
23772379
return -1;
23782380
}

0 commit comments

Comments
 (0)