diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 1fe9743a331e36..25a650ffbb7446 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -105,7 +105,7 @@ def __init__(self, coro, *, loop=None, name=None): else: self._name = str(name) - self._num_cancels_requested = 0 + self._cancel_requested = False self._must_cancel = False self._fut_waiter = None self._coro = coro @@ -202,9 +202,9 @@ def cancel(self, msg=None): self._log_traceback = False if self.done(): return False - self._num_cancels_requested += 1 - if self._num_cancels_requested > 1: + if self._cancel_requested: return False + self._cancel_requested = True if self._fut_waiter is not None: if self._fut_waiter.cancel(msg=msg): # Leave self._fut_waiter; it may be a Task that @@ -216,13 +216,15 @@ def cancel(self, msg=None): self._cancel_message = msg return True - def cancelling(self) -> int: - return self._num_cancels_requested + def cancelling(self): + return self._cancel_requested - def uncancel(self) -> int: - if self._num_cancels_requested > 0: - self._num_cancels_requested -= 1 - return self._num_cancels_requested + def uncancel(self): + if self._cancel_requested: + self._cancel_requested = False + return True + else: + return False def __step(self, exc=None): if self.done(): diff --git a/Lib/asyncio/timeouts.py b/Lib/asyncio/timeouts.py index 9092f50b39d578..9648a06d61b317 100644 --- a/Lib/asyncio/timeouts.py +++ b/Lib/asyncio/timeouts.py @@ -32,8 +32,7 @@ def __init__(self, deadline: Optional[float]) -> None: self._task: Optional[tasks.Task[Any]] = None self._deadline = deadline - @property - def deadline(self) -> Optional[float]: + def when(self) -> Optional[float]: return self._deadline def reschedule(self, deadline: Optional[float]) -> None: diff --git a/Lib/test/test_asyncio/test_timeouts.py b/Lib/test/test_asyncio/test_timeouts.py index cd1cb531c660a3..d3f7d8f9721276 100644 --- a/Lib/test/test_asyncio/test_timeouts.py +++ b/Lib/test/test_asyncio/test_timeouts.py @@ -42,7 +42,7 @@ async def test_timeout_at_basic(self): async with asyncio.timeout_at(deadline) as cm: await asyncio.sleep(10) self.assertTrue(cm.expired()) - self.assertEqual(deadline, cm.deadline) + self.assertEqual(deadline, cm.when()) async def test_nested_timeouts(self): cancel = False @@ -89,7 +89,7 @@ async def test_timeout_disabled(self): t1 = loop.time() self.assertFalse(cm.expired()) - self.assertIsNone(cm.deadline) + self.assertIsNone(cm.when()) # finised fast. Very busy CI box requires high enough limit, # that's why 0.01 cannot be used self.assertLess(t1-t0, 2) @@ -102,7 +102,7 @@ async def test_timeout_at_disabled(self): t1 = loop.time() self.assertFalse(cm.expired()) - self.assertIsNone(cm.deadline) + self.assertIsNone(cm.when()) # finised fast. Very busy CI box requires high enough limit, # that's why 0.01 cannot be used self.assertLess(t1-t0, 2) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 51e1e083f44ae5..6725e2eba79bc2 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -91,7 +91,7 @@ typedef struct { PyObject *task_context; int task_must_cancel; int task_log_destroy_pending; - int task_num_cancels_requested; + int task_cancel_requested; } TaskObj; typedef struct { @@ -2040,7 +2040,7 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop, Py_CLEAR(self->task_fut_waiter); self->task_must_cancel = 0; self->task_log_destroy_pending = 1; - self->task_num_cancels_requested = 0; + self->task_cancel_requested = 0; Py_INCREF(coro); Py_XSETREF(self->task_coro, coro); @@ -2207,10 +2207,10 @@ _asyncio_Task_cancel_impl(TaskObj *self, PyObject *msg) Py_RETURN_FALSE; } - self->task_num_cancels_requested += 1; - if (self->task_num_cancels_requested > 1) { + if (self->task_cancel_requested) { Py_RETURN_FALSE; } + self->task_cancel_requested = 1; if (self->task_fut_waiter) { PyObject *res; @@ -2256,7 +2256,12 @@ _asyncio_Task_cancelling_impl(TaskObj *self) /*[clinic end generated code: output=803b3af96f917d7e input=c50e50f9c3ca4676]*/ /*[clinic end generated code]*/ { - return PyLong_FromLong(self->task_num_cancels_requested); + if (self->task_cancel_requested) { + Py_RETURN_TRUE; + } + else { + Py_RETURN_FALSE; + } } /*[clinic input] @@ -2275,10 +2280,13 @@ _asyncio_Task_uncancel_impl(TaskObj *self) /*[clinic end generated code: output=58184d236a817d3c input=5db95e28fcb6f7cd]*/ /*[clinic end generated code]*/ { - if (self->task_num_cancels_requested > 0) { - self->task_num_cancels_requested -= 1; + if (self->task_cancel_requested) { + self->task_cancel_requested = 0; + Py_RETURN_TRUE; + } + else { + Py_RETURN_FALSE; } - return PyLong_FromLong(self->task_num_cancels_requested); } /*[clinic input]