Skip to content

Commit 13802a3

Browse files
committed
bpo-29271: Fix Task.current_task and Task.all_tasks to accept None. (#406)
1 parent dea5101 commit 13802a3

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

Lib/test/test_asyncio/test_tasks.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,14 @@ def test_current_task(self):
14621462
def coro(loop):
14631463
self.assertTrue(Task.current_task(loop=loop) is task)
14641464

1465+
# See http://bugs.python.org/issue29271 for details:
1466+
asyncio.set_event_loop(loop)
1467+
try:
1468+
self.assertIs(Task.current_task(None), task)
1469+
self.assertIs(Task.current_task(), task)
1470+
finally:
1471+
asyncio.set_event_loop(None)
1472+
14651473
task = self.new_task(self.loop, coro(self.loop))
14661474
self.loop.run_until_complete(task)
14671475
self.assertIsNone(Task.current_task(loop=self.loop))
@@ -1806,8 +1814,17 @@ def kill_me(loop):
18061814
# schedule the task
18071815
coro = kill_me(self.loop)
18081816
task = asyncio.ensure_future(coro, loop=self.loop)
1817+
18091818
self.assertEqual(Task.all_tasks(loop=self.loop), {task})
18101819

1820+
# See http://bugs.python.org/issue29271 for details:
1821+
asyncio.set_event_loop(self.loop)
1822+
try:
1823+
self.assertEqual(Task.all_tasks(), {task})
1824+
self.assertEqual(Task.all_tasks(None), {task})
1825+
finally:
1826+
asyncio.set_event_loop(None)
1827+
18111828
# execute the task so it waits for future
18121829
self.loop._run_once()
18131830
self.assertEqual(len(self.loop._ready), 0)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ Extension Modules
8282
Library
8383
-------
8484

85+
- bpo-29271: Fix Task.current_task and Task.all_tasks implemented in C
86+
to accept None argument as their pure Python implementation.
87+
8588
- bpo-29703: Fix asyncio to support instantiation of new event loops
8689
in child processes.
8790

Modules/_asynciomodule.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ TaskObj_get_fut_waiter(TaskObj *task)
14131413
@classmethod
14141414
_asyncio.Task.current_task
14151415
1416-
loop: 'O' = NULL
1416+
loop: 'O' = None
14171417
14181418
Return the currently running task in an event loop or None.
14191419
@@ -1424,12 +1424,12 @@ None is returned when called not in the context of a Task.
14241424

14251425
static PyObject *
14261426
_asyncio_Task_current_task_impl(PyTypeObject *type, PyObject *loop)
1427-
/*[clinic end generated code: output=99fbe7332c516e03 input=cd784537f02cf833]*/
1427+
/*[clinic end generated code: output=99fbe7332c516e03 input=a0d6cdf2e3b243e1]*/
14281428
{
14291429
PyObject *res;
14301430

1431-
if (loop == NULL) {
1432-
loop = PyObject_CallObject(asyncio_get_event_loop, NULL);
1431+
if (loop == Py_None) {
1432+
loop = _PyObject_CallNoArg(asyncio_get_event_loop);
14331433
if (loop == NULL) {
14341434
return NULL;
14351435
}
@@ -1500,7 +1500,7 @@ task_all_tasks(PyObject *loop)
15001500
@classmethod
15011501
_asyncio.Task.all_tasks
15021502
1503-
loop: 'O' = NULL
1503+
loop: 'O' = None
15041504
15051505
Return a set of all tasks for an event loop.
15061506
@@ -1509,12 +1509,12 @@ By default all tasks for the current event loop are returned.
15091509

15101510
static PyObject *
15111511
_asyncio_Task_all_tasks_impl(PyTypeObject *type, PyObject *loop)
1512-
/*[clinic end generated code: output=11f9b20749ccca5d input=cd64aa5f88bd5c49]*/
1512+
/*[clinic end generated code: output=11f9b20749ccca5d input=c6f5b53bd487488f]*/
15131513
{
15141514
PyObject *res;
15151515

1516-
if (loop == NULL) {
1517-
loop = PyObject_CallObject(asyncio_get_event_loop, NULL);
1516+
if (loop == Py_None) {
1517+
loop = _PyObject_CallNoArg(asyncio_get_event_loop);
15181518
if (loop == NULL) {
15191519
return NULL;
15201520
}

Modules/clinic/_asynciomodule.c.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ _asyncio_Task_current_task(PyTypeObject *type, PyObject **args, Py_ssize_t nargs
278278
PyObject *return_value = NULL;
279279
static const char * const _keywords[] = {"loop", NULL};
280280
static _PyArg_Parser _parser = {"|O:current_task", _keywords, 0};
281-
PyObject *loop = NULL;
281+
PyObject *loop = Py_None;
282282

283283
if (!_PyArg_ParseStack(args, nargs, kwnames, &_parser,
284284
&loop)) {
@@ -310,7 +310,7 @@ _asyncio_Task_all_tasks(PyTypeObject *type, PyObject **args, Py_ssize_t nargs, P
310310
PyObject *return_value = NULL;
311311
static const char * const _keywords[] = {"loop", NULL};
312312
static _PyArg_Parser _parser = {"|O:all_tasks", _keywords, 0};
313-
PyObject *loop = NULL;
313+
PyObject *loop = Py_None;
314314

315315
if (!_PyArg_ParseStack(args, nargs, kwnames, &_parser,
316316
&loop)) {
@@ -517,4 +517,4 @@ _asyncio_Task__wakeup(TaskObj *self, PyObject **args, Py_ssize_t nargs, PyObject
517517
exit:
518518
return return_value;
519519
}
520-
/*[clinic end generated code: output=8f036321bb083066 input=a9049054013a1b77]*/
520+
/*[clinic end generated code: output=40ca6c9da517da73 input=a9049054013a1b77]*/

0 commit comments

Comments
 (0)