From 66338b3d34104e9493b3705e5f456068e7b831c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Gr=C3=B6nholm?= Date: Thu, 30 May 2019 15:11:10 +0300 Subject: [PATCH 1/4] bpo-36999: Add asyncio.Task.get_coro() --- Doc/library/asyncio-task.rst | 6 ++++++ Lib/asyncio/tasks.py | 3 +++ Lib/test/test_asyncio/test_tasks.py | 12 ++++++++++++ .../2019-05-30-13-30-46.bpo-36999.EjY_L2.rst | 2 ++ Modules/_asynciomodule.c | 13 +++++++++++++ Modules/clinic/_asynciomodule.c.h | 19 ++++++++++++++++++- 6 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2019-05-30-13-30-46.bpo-36999.EjY_L2.rst diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index d94fa587cd3a47..fc94df0821796a 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -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. + + .. versionadded:: 3.8 + .. method:: get_name() Return the name of the Task. diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 78e76003b3ac22..95e85600a2e802 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -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 diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 114dd76687cd70..2ea084b1e8f62f 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -2425,6 +2425,18 @@ 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: + coro.close() + loop.close() + + def add_subclass_tests(cls): BaseTask = cls.Task diff --git a/Misc/NEWS.d/next/Library/2019-05-30-13-30-46.bpo-36999.EjY_L2.rst b/Misc/NEWS.d/next/Library/2019-05-30-13-30-46.bpo-36999.EjY_L2.rst new file mode 100644 index 00000000000000..5c897fb4dbc745 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-05-30-13-30-46.bpo-36999.EjY_L2.rst @@ -0,0 +1,2 @@ +Add the ``asyncio.Task.get_coro()`` method to publicly expose the tasks's +coroutine object. diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index d8b631b7c7a292..281161b68611ae 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -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]*/ @@ -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 */ }; diff --git a/Modules/clinic/_asynciomodule.c.h b/Modules/clinic/_asynciomodule.c.h index 87669f7c9f32eb..b9daee6aae965b 100644 --- a/Modules/clinic/_asynciomodule.c.h +++ b/Modules/clinic/_asynciomodule.c.h @@ -569,6 +569,23 @@ PyDoc_STRVAR(_asyncio_Task_set_exception__doc__, #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" @@ -815,4 +832,4 @@ _asyncio__leave_task(PyObject *module, PyObject *const *args, Py_ssize_t nargs, exit: return return_value; } -/*[clinic end generated code: output=e3b02d96da56e80c input=a9049054013a1b77]*/ +/*[clinic end generated code: output=51c50219f6a0863a input=a9049054013a1b77]*/ From e995080c37dcb489e6cacbb3425c5a3b3f9cb32a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Gr=C3=B6nholm?= Date: Thu, 30 May 2019 15:31:16 +0300 Subject: [PATCH 2/4] Removed extra blank line --- Lib/test/test_asyncio/test_tasks.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 2ea084b1e8f62f..8d9e1386a847f6 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -2437,7 +2437,6 @@ def test_get_coro(self): loop.close() - def add_subclass_tests(cls): BaseTask = cls.Task BaseFuture = cls.Future From e99f3fc4b483cc6923998b6b9a422e97b3b4e068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Gr=C3=B6nholm?= Date: Thu, 30 May 2019 15:33:00 +0300 Subject: [PATCH 3/4] Removed unnecessary coro.close() call --- Lib/test/test_asyncio/test_tasks.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 8d9e1386a847f6..74ce25908a3308 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -2433,7 +2433,6 @@ def test_get_coro(self): loop.run_until_complete(task) self.assertIs(task.get_coro(), coro) finally: - coro.close() loop.close() From 654f23fe62ef25c662ae4e6734432acea83a333b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Gr=C3=B6nholm?= Date: Thu, 30 May 2019 18:10:58 +0300 Subject: [PATCH 4/4] Changed the wording of the method documentation --- Doc/library/asyncio-task.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index fc94df0821796a..1fcdcb985d8842 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -844,7 +844,7 @@ Task Object .. method:: get_coro() - Return the coroutine object that was passed to the constructor. + Return the coroutine object wrapped by the :class:`Task`. .. versionadded:: 3.8