Skip to content

Commit 9cf8f90

Browse files
committed
pythongh-95097: support custom tasks with missing uncancel methods
1 parent a6daaf2 commit 9cf8f90

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

Lib/asyncio/runners.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def run(self, coro, *, context=None):
118118
events.set_event_loop(self._loop)
119119
return self._loop.run_until_complete(task)
120120
except exceptions.CancelledError:
121-
if self._interrupt_count > 0 and task.uncancel() == 0:
121+
if self._interrupt_count > 0 and tasks._uncancel(task) == 0:
122122
raise KeyboardInterrupt()
123123
else:
124124
raise # CancelledError

Lib/asyncio/taskgroups.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async def __aexit__(self, et, exc, tb):
6363

6464
if et is not None:
6565
if et is exceptions.CancelledError:
66-
if self._parent_cancel_requested and not self._parent_task.uncancel():
66+
if self._parent_cancel_requested and tasks._uncancel(self._parent_task) == 0:
6767
# Do nothing, i.e. swallow the error.
6868
pass
6969
else:

Lib/asyncio/tasks.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,30 @@ def _set_task_name(task, name):
6767
try:
6868
set_name = task.set_name
6969
except AttributeError:
70-
warnings.warn("Task.set_name() was added in Python 3.8, "
70+
warnings._deprecated(
71+
name="missing asyncio.Task.set_name()",
72+
message="Task.set_name() was added in Python 3.8, "
7173
"the method support will be mandatory for third-party "
72-
"task implementations since 3.13.",
73-
DeprecationWarning, stacklevel=3)
74+
"task implementations since 3.13.", remove=(3, 13))
7475
else:
7576
set_name(name)
7677

7778

79+
def _uncancel(task):
80+
try:
81+
uncancel = task.uncancel
82+
except AttributeError:
83+
warnings._deprecated(
84+
name="missing asyncio.Task.uncancel()",
85+
message="Task.uncancel() was added in Python 3.11, "
86+
"the method support will be mandatory for third-party "
87+
"task implementations since 3.16.",
88+
remove=(3, 16))
89+
return -1
90+
else:
91+
return uncancel()
92+
93+
7894
class Task(futures._PyFuture): # Inherit Python Task implementation
7995
# from a Python Future implementation.
8096

Lib/asyncio/timeouts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async def __aexit__(
9292
if self._state is _State.EXPIRING:
9393
self._state = _State.EXPIRED
9494

95-
if self._task.uncancel() == 0 and exc_type is exceptions.CancelledError:
95+
if tasks._uncancel(self._task) == 0 and exc_type is exceptions.CancelledError:
9696
# Since there are no outstanding cancel requests, we're
9797
# handling this.
9898
raise TimeoutError

0 commit comments

Comments
 (0)