Skip to content

Commit 8d26aa9

Browse files
authored
bpo-29271: Fix Task.current_task and Task.all_tasks to accept None. (#406)
1 parent ba7e1f9 commit 8d26aa9

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

Lib/test/test_asyncio/test_tasks.py

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

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

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

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ Extension Modules
256256
Library
257257
-------
258258

259+
- bpo-29271: Fix Task.current_task and Task.all_tasks implemented in C
260+
to accept None argument as their pure Python implementation.
261+
259262
- bpo-29703: Fix asyncio to support instantiation of new event loops
260263
in child processes.
261264

Modules/_asynciomodule.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,7 @@ TaskObj_get_fut_waiter(TaskObj *task)
14141414
@classmethod
14151415
_asyncio.Task.current_task
14161416
1417-
loop: 'O' = NULL
1417+
loop: 'O' = None
14181418
14191419
Return the currently running task in an event loop or None.
14201420
@@ -1425,11 +1425,11 @@ None is returned when called not in the context of a Task.
14251425

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

1432-
if (loop == NULL) {
1432+
if (loop == Py_None) {
14331433
loop = _PyObject_CallNoArg(asyncio_get_event_loop);
14341434
if (loop == NULL) {
14351435
return NULL;
@@ -1501,7 +1501,7 @@ task_all_tasks(PyObject *loop)
15011501
@classmethod
15021502
_asyncio.Task.all_tasks
15031503
1504-
loop: 'O' = NULL
1504+
loop: 'O' = None
15051505
15061506
Return a set of all tasks for an event loop.
15071507
@@ -1510,11 +1510,11 @@ By default all tasks for the current event loop are returned.
15101510

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

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

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_ParseStackAndKeywords(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_ParseStackAndKeywords(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=07a15bbb28d03edc input=a9049054013a1b77]*/
520+
/*[clinic end generated code: output=3dfec49689cebd4c input=a9049054013a1b77]*/

0 commit comments

Comments
 (0)