]> granicus.if.org Git - python/commitdiff
bpo-36999: Add asyncio.Task.get_coro() (GH-13680)
authorAlex Grönholm <alex.gronholm@nextday.fi>
Thu, 30 May 2019 15:30:09 +0000 (18:30 +0300)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 30 May 2019 15:30:09 +0000 (08:30 -0700)
https://bugs.python.org/issue36999

Doc/library/asyncio-task.rst
Lib/asyncio/tasks.py
Lib/test/test_asyncio/test_tasks.py
Misc/NEWS.d/next/Library/2019-05-30-13-30-46.bpo-36999.EjY_L2.rst [new file with mode: 0644]
Modules/_asynciomodule.c
Modules/clinic/_asynciomodule.c.h

index d94fa587cd3a47315596234b9ae1583683cfa3d7..1fcdcb985d88424613b5aa1db03054e55275b899 100644 (file)
@@ -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.
index 78e76003b3ac220db5ef2ab2168148e77d374176..95e85600a2e8025e858821379e92a639da6e2c34 100644 (file)
@@ -152,6 +152,9 @@ class Task(futures._PyFuture):  # Inherit Python Task implementation
     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
 
index 114dd76687cd70412f9102e6693daaf2607e799c..74ce25908a33081776824dabac21c23d1b784bac 100644 (file)
@@ -2425,6 +2425,16 @@ class BaseTaskTests:
 
         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
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 (file)
index 0000000..5c897fb
--- /dev/null
@@ -0,0 +1,2 @@
+Add the ``asyncio.Task.get_coro()`` method to publicly expose the tasks's
+coroutine object.
index d8b631b7c7a2923d72db5c1dc76b23d1ae06775a..281161b68611aecfa33f540e3f15c0b578e06790 100644 (file)
@@ -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 */
 };
 
index 87669f7c9f32eb834b79c4238ad920491f04cb61..b9daee6aae965b2ac95bc603bd6d8639df7b1245 100644 (file)
@@ -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]*/