Skip to content

Commit 004e64e

Browse files
authoredJul 2, 2020
bpo-40967: Remove deprecated asyncio.Task.current_task() and asyncio.Task.all_tasks() (pythonGH-20874)
1 parent 666ecfb commit 004e64e

File tree

7 files changed

+9
-270
lines changed

7 files changed

+9
-270
lines changed
 

‎Doc/library/asyncio-task.rst

-25
Original file line numberDiff line numberDiff line change
@@ -962,31 +962,6 @@ Task Object
962962

963963
.. versionadded:: 3.8
964964

965-
.. classmethod:: all_tasks(loop=None)
966-
967-
Return a set of all tasks for an event loop.
968-
969-
By default all tasks for the current event loop are returned.
970-
If *loop* is ``None``, the :func:`get_event_loop` function
971-
is used to get the current loop.
972-
973-
.. deprecated-removed:: 3.7 3.9
974-
975-
Do not call this as a task method. Use the :func:`asyncio.all_tasks`
976-
function instead.
977-
978-
.. classmethod:: current_task(loop=None)
979-
980-
Return the currently running task or ``None``.
981-
982-
If *loop* is ``None``, the :func:`get_event_loop` function
983-
is used to get the current loop.
984-
985-
.. deprecated-removed:: 3.7 3.9
986-
987-
Do not call this as a task method. Use the
988-
:func:`asyncio.current_task` function instead.
989-
990965

991966
.. _asyncio_generator_based_coro:
992967

‎Doc/whatsnew/3.9.rst

+5
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,11 @@ Removed
878878
deprecated since 2006, and only returning ``False`` when it's called.
879879
(Contributed by Batuhan Taskaya in :issue:`40208`)
880880

881+
* The :meth:`asyncio.Task.current_task` and :meth:`asyncio.Task.all_tasks` have
882+
have been removed. They were deprecated since Python 3.7 and you can use
883+
:func:`asyncio.current_task` and :func:`asyncio.all_tasks` instead.
884+
(Contributed by Rémi Lapeyre in :issue:`40967`)
885+
881886

882887
Porting to Python 3.9
883888
=====================

‎Lib/asyncio/tasks.py

-28
Original file line numberDiff line numberDiff line change
@@ -113,34 +113,6 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
113113
# status is still pending
114114
_log_destroy_pending = True
115115

116-
@classmethod
117-
def current_task(cls, loop=None):
118-
"""Return the currently running task in an event loop or None.
119-
120-
By default the current task for the current event loop is returned.
121-
122-
None is returned when called not in the context of a Task.
123-
"""
124-
warnings.warn("Task.current_task() is deprecated since Python 3.7, "
125-
"use asyncio.current_task() instead",
126-
DeprecationWarning,
127-
stacklevel=2)
128-
if loop is None:
129-
loop = events.get_event_loop()
130-
return current_task(loop)
131-
132-
@classmethod
133-
def all_tasks(cls, loop=None):
134-
"""Return a set of all tasks for an event loop.
135-
136-
By default all tasks for the current event loop are returned.
137-
"""
138-
warnings.warn("Task.all_tasks() is deprecated since Python 3.7, "
139-
"use asyncio.all_tasks() instead",
140-
DeprecationWarning,
141-
stacklevel=2)
142-
return _all_tasks_compat(loop)
143-
144116
def __init__(self, coro, *, loop=None, name=None):
145117
super().__init__(loop=loop)
146118
if self._source_traceback:

‎Lib/test/test_asyncio/test_tasks.py

+1-47
Original file line numberDiff line numberDiff line change
@@ -1943,32 +1943,6 @@ async def coro():
19431943
self.assertEqual(res, 'test')
19441944
self.assertIsNone(t2.result())
19451945

1946-
1947-
def test_current_task_deprecated(self):
1948-
Task = self.__class__.Task
1949-
1950-
with self.assertWarns(DeprecationWarning):
1951-
self.assertIsNone(Task.current_task(loop=self.loop))
1952-
1953-
async def coro(loop):
1954-
with self.assertWarns(DeprecationWarning):
1955-
self.assertIs(Task.current_task(loop=loop), task)
1956-
1957-
# See http://bugs.python.org/issue29271 for details:
1958-
asyncio.set_event_loop(loop)
1959-
try:
1960-
with self.assertWarns(DeprecationWarning):
1961-
self.assertIs(Task.current_task(None), task)
1962-
with self.assertWarns(DeprecationWarning):
1963-
self.assertIs(Task.current_task(), task)
1964-
finally:
1965-
asyncio.set_event_loop(None)
1966-
1967-
task = self.new_task(self.loop, coro(self.loop))
1968-
self.loop.run_until_complete(task)
1969-
with self.assertWarns(DeprecationWarning):
1970-
self.assertIsNone(Task.current_task(loop=self.loop))
1971-
19721946
def test_current_task(self):
19731947
self.assertIsNone(asyncio.current_task(loop=self.loop))
19741948

@@ -2305,16 +2279,6 @@ def foo():
23052279
self.assertIsInstance(exception, Exception)
23062280
self.assertEqual(exception.args, ("foo", ))
23072281

2308-
def test_all_tasks_deprecated(self):
2309-
Task = self.__class__.Task
2310-
2311-
async def coro():
2312-
with self.assertWarns(DeprecationWarning):
2313-
assert Task.all_tasks(self.loop) == {t}
2314-
2315-
t = self.new_task(self.loop, coro())
2316-
self.loop.run_until_complete(t)
2317-
23182282
def test_log_destroyed_pending_task(self):
23192283
Task = self.__class__.Task
23202284

@@ -2337,15 +2301,7 @@ def kill_me(loop):
23372301

23382302
self.assertEqual(asyncio.all_tasks(loop=self.loop), {task})
23392303

2340-
# See http://bugs.python.org/issue29271 for details:
2341-
asyncio.set_event_loop(self.loop)
2342-
try:
2343-
with self.assertWarns(DeprecationWarning):
2344-
self.assertEqual(Task.all_tasks(), {task})
2345-
with self.assertWarns(DeprecationWarning):
2346-
self.assertEqual(Task.all_tasks(None), {task})
2347-
finally:
2348-
asyncio.set_event_loop(None)
2304+
asyncio.set_event_loop(None)
23492305

23502306
# execute the task so it waits for future
23512307
self.loop._run_once()
@@ -3043,8 +2999,6 @@ def done(self):
30432999
self.assertEqual(asyncio.all_tasks(loop), set())
30443000
self._register_task(task)
30453001
self.assertEqual(asyncio.all_tasks(loop), set())
3046-
with self.assertWarns(DeprecationWarning):
3047-
self.assertEqual(asyncio.Task.all_tasks(loop), {task})
30483002
self._unregister_task(task)
30493003

30503004
def test__enter_task(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Removed :meth:`asyncio.Task.current_task` and
2+
:meth:`asyncio.Task.all_tasks`. Patch contributed by Rémi Lapeyre.

‎Modules/_asynciomodule.c

-89
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ module _asyncio
1313
_Py_IDENTIFIER(__asyncio_running_event_loop__);
1414
_Py_IDENTIFIER(_asyncio_future_blocking);
1515
_Py_IDENTIFIER(add_done_callback);
16-
_Py_IDENTIFIER(_all_tasks_compat);
1716
_Py_IDENTIFIER(call_soon);
1817
_Py_IDENTIFIER(cancel);
19-
_Py_IDENTIFIER(current_task);
2018
_Py_IDENTIFIER(get_event_loop);
2119
_Py_IDENTIFIER(send);
2220
_Py_IDENTIFIER(throw);
@@ -2182,91 +2180,6 @@ TaskObj_get_fut_waiter(TaskObj *task, void *Py_UNUSED(ignored))
21822180
Py_RETURN_NONE;
21832181
}
21842182

2185-
/*[clinic input]
2186-
@classmethod
2187-
_asyncio.Task.current_task
2188-
2189-
loop: object = None
2190-
2191-
Return the currently running task in an event loop or None.
2192-
2193-
By default the current task for the current event loop is returned.
2194-
2195-
None is returned when called not in the context of a Task.
2196-
[clinic start generated code]*/
2197-
2198-
static PyObject *
2199-
_asyncio_Task_current_task_impl(PyTypeObject *type, PyObject *loop)
2200-
/*[clinic end generated code: output=99fbe7332c516e03 input=cd14770c5b79c7eb]*/
2201-
{
2202-
PyObject *ret;
2203-
PyObject *current_task_func;
2204-
2205-
if (PyErr_WarnEx(PyExc_DeprecationWarning,
2206-
"Task.current_task() is deprecated, " \
2207-
"use asyncio.current_task() instead",
2208-
1) < 0) {
2209-
return NULL;
2210-
}
2211-
2212-
current_task_func = _PyObject_GetAttrId(asyncio_mod, &PyId_current_task);
2213-
if (current_task_func == NULL) {
2214-
return NULL;
2215-
}
2216-
2217-
if (loop == Py_None) {
2218-
loop = get_event_loop();
2219-
if (loop == NULL) {
2220-
Py_DECREF(current_task_func);
2221-
return NULL;
2222-
}
2223-
ret = PyObject_CallOneArg(current_task_func, loop);
2224-
Py_DECREF(current_task_func);
2225-
Py_DECREF(loop);
2226-
return ret;
2227-
}
2228-
else {
2229-
ret = PyObject_CallOneArg(current_task_func, loop);
2230-
Py_DECREF(current_task_func);
2231-
return ret;
2232-
}
2233-
}
2234-
2235-
/*[clinic input]
2236-
@classmethod
2237-
_asyncio.Task.all_tasks
2238-
2239-
loop: object = None
2240-
2241-
Return a set of all tasks for an event loop.
2242-
2243-
By default all tasks for the current event loop are returned.
2244-
[clinic start generated code]*/
2245-
2246-
static PyObject *
2247-
_asyncio_Task_all_tasks_impl(PyTypeObject *type, PyObject *loop)
2248-
/*[clinic end generated code: output=11f9b20749ccca5d input=497f80bc9ce726b5]*/
2249-
{
2250-
PyObject *res;
2251-
PyObject *all_tasks_func;
2252-
2253-
if (PyErr_WarnEx(PyExc_DeprecationWarning,
2254-
"Task.all_tasks() is deprecated, " \
2255-
"use asyncio.all_tasks() instead",
2256-
1) < 0) {
2257-
return NULL;
2258-
}
2259-
2260-
all_tasks_func = _PyObject_GetAttrId(asyncio_mod, &PyId__all_tasks_compat);
2261-
if (all_tasks_func == NULL) {
2262-
return NULL;
2263-
}
2264-
2265-
res = PyObject_CallOneArg(all_tasks_func, loop);
2266-
Py_DECREF(all_tasks_func);
2267-
return res;
2268-
}
2269-
22702183
/*[clinic input]
22712184
_asyncio.Task._make_cancelled_error
22722185
@@ -2587,8 +2500,6 @@ static PyMethodDef TaskType_methods[] = {
25872500
_ASYNCIO_FUTURE_DONE_METHODDEF
25882501
_ASYNCIO_TASK_SET_RESULT_METHODDEF
25892502
_ASYNCIO_TASK_SET_EXCEPTION_METHODDEF
2590-
_ASYNCIO_TASK_CURRENT_TASK_METHODDEF
2591-
_ASYNCIO_TASK_ALL_TASKS_METHODDEF
25922503
_ASYNCIO_TASK_CANCEL_METHODDEF
25932504
_ASYNCIO_TASK_GET_STACK_METHODDEF
25942505
_ASYNCIO_TASK_PRINT_STACK_METHODDEF

‎Modules/clinic/_asynciomodule.c.h

+1-81
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)