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.
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
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
--- /dev/null
+Add the ``asyncio.Task.get_coro()`` method to publicly expose the tasks's
+coroutine object.
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]*/
_ASYNCIO_TASK__REPR_INFO_METHODDEF
_ASYNCIO_TASK_GET_NAME_METHODDEF
_ASYNCIO_TASK_SET_NAME_METHODDEF
+ _ASYNCIO_TASK_GET_CORO_METHODDEF
{NULL, NULL} /* Sentinel */
};
#define _ASYNCIO_TASK_SET_EXCEPTION_METHODDEF \
{"set_exception", (PyCFunction)_asyncio_Task_set_exception, METH_O, _asyncio_Task_set_exception__doc__},
+PyDoc_STRVAR(_asyncio_Task_get_coro__doc__,
+"get_coro($self, /)\n"
+"--\n"
+"\n");
+
+#define _ASYNCIO_TASK_GET_CORO_METHODDEF \
+ {"get_coro", (PyCFunction)_asyncio_Task_get_coro, METH_NOARGS, _asyncio_Task_get_coro__doc__},
+
+static PyObject *
+_asyncio_Task_get_coro_impl(TaskObj *self);
+
+static PyObject *
+_asyncio_Task_get_coro(TaskObj *self, PyObject *Py_UNUSED(ignored))
+{
+ return _asyncio_Task_get_coro_impl(self);
+}
+
PyDoc_STRVAR(_asyncio_Task_get_name__doc__,
"get_name($self, /)\n"
"--\n"
exit:
return return_value;
}
-/*[clinic end generated code: output=e3b02d96da56e80c input=a9049054013a1b77]*/
+/*[clinic end generated code: output=51c50219f6a0863a input=a9049054013a1b77]*/