Skip to content

asyncio.Task raises AttributeError when no loop is passed #154695

Description

@deadlovelll

Bug report

Bug description:

asyncio.Task(coro, eager_start=True) raises AttributeError when the loop is not passed

Reproducer:

import asyncio

async def coro():
    return 42

async def main():
    t = asyncio.Task(coro(), eager_start=True)
    print(t.done(), await t)

asyncio.run(main())

Expected:

True 42

But actually:

AttributeError: 'NoneType' object has no attribute 'is_running'

For example python implementation of the Task works correctly:

import asyncio
import asyncio.tasks


async def coro():
    return 42


async def main():
    task = asyncio.tasks._PyTask(coro(), eager_start=True)
    print(task.done(), await task)


asyncio.run(main())

Output:

True 42

Proposed fix:

From:

PyObject *res = PyObject_CallMethodNoArgs(loop, &_Py_ID(is_running));

To:

PyObject *res = PyObject_CallMethodNoArgs(self->task_loop,
                                          &_Py_ID(is_running));

I have a fix ready

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions