Skip to content

bpo-36999: Add asyncio.Task.get_coro() #13680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/asyncio-task.rst
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,12 @@ Task Object
The *file* argument is an I/O stream to which the output
is written; by default output is written to :data:`sys.stderr`.

.. method:: get_coro()

Return the coroutine object wrapped by the :class:`Task`.

.. versionadded:: 3.8

.. method:: get_name()

Return the name of the Task.
Expand Down
3 changes: 3 additions & 0 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ def __del__(self):
def _repr_info(self):
return base_tasks._task_repr_info(self)

def get_coro(self):
return self._coro

def get_name(self):
return self._name

Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2425,6 +2425,16 @@ async def main():

self.assertEqual(cvar.get(), -1)

def test_get_coro(self):
loop = asyncio.new_event_loop()
coro = coroutine_function()
try:
task = self.new_task(loop, coro)
loop.run_until_complete(task)
self.assertIs(task.get_coro(), coro)
finally:
loop.close()


def add_subclass_tests(cls):
BaseTask = cls.Task
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add the ``asyncio.Task.get_coro()`` method to publicly expose the tasks's
coroutine object.
13 changes: 13 additions & 0 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2313,6 +2313,18 @@ _asyncio_Task_set_exception(TaskObj *self, PyObject *exception)
return NULL;
}

/*[clinic input]
_asyncio.Task.get_coro
[clinic start generated code]*/

static PyObject *
_asyncio_Task_get_coro_impl(TaskObj *self)
/*[clinic end generated code: output=bcac27c8cc6c8073 input=d2e8606c42a7b403]*/
{
Py_INCREF(self->task_coro);
return self->task_coro;
}

/*[clinic input]
_asyncio.Task.get_name
[clinic start generated code]*/
Expand Down Expand Up @@ -2439,6 +2451,7 @@ static PyMethodDef TaskType_methods[] = {
_ASYNCIO_TASK__REPR_INFO_METHODDEF
_ASYNCIO_TASK_GET_NAME_METHODDEF
_ASYNCIO_TASK_SET_NAME_METHODDEF
_ASYNCIO_TASK_GET_CORO_METHODDEF
{NULL, NULL} /* Sentinel */
};

Expand Down
19 changes: 18 additions & 1 deletion Modules/clinic/_asynciomodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.