Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 that was passed to the constructor.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike mentioning constructor here.

Above we have the text:

   Use the high-level :func:`asyncio.create_task` function to create
   Tasks, or the low-level :meth:`loop.create_task` or
   :func:`ensure_future` functions.  Manual instantiation of Tasks
   is discouraged.

Maybe rephrase this line to Return the coroutine object that was used for the task creation. or something like this?
Sorry, I'm not a native speaker. Don't insist on the exact wording but you've got my idea.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just say this: "Return the coroutine object wrapped by the Task".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Return the coroutine object that was passed to the constructor.
Return the coroutine object wrapped by the :class:`Task`.

@1st1 like this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it's great. If you have time just merge Alex's PR and we can fix this ourselves. Up to you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the wording now as requested.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!


.. 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.