From 329c274ffa6c163a8cd1af02dabbded80a3882a6 Mon Sep 17 00:00:00 2001 From: Adrian Herrmann Date: Fri, 24 Nov 2023 16:35:53 +0100 Subject: QtAsyncio: Do not raise exception at handle cancel Do not raise an exception when cancelling a handle. The exception should be raised later, when retrieving the future's result. Task-number: PYSIDE-769 Change-Id: I8243cf16e8be5afe167d69313054e97e9aafc75c Reviewed-by: Friedemann Kleint Reviewed-by: Shyamnath Premnadh (cherry picked from commit 476dea383d64d528263ff12d795901b10bc7ed48) Reviewed-by: Qt Cherry-pick Bot --- sources/pyside6/PySide6/QtAsyncio/events.py | 9 +-------- sources/pyside6/PySide6/QtAsyncio/futures.py | 5 ++++- sources/pyside6/PySide6/QtAsyncio/tasks.py | 2 -- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/sources/pyside6/PySide6/QtAsyncio/events.py b/sources/pyside6/PySide6/QtAsyncio/events.py index d64d7d22b..7e578e547 100644 --- a/sources/pyside6/PySide6/QtAsyncio/events.py +++ b/sources/pyside6/PySide6/QtAsyncio/events.py @@ -466,8 +466,6 @@ class QAsyncioHandle(): def __init__(self, callback: typing.Callable, args: typing.Tuple, loop: QAsyncioEventLoop, context: typing.Optional[contextvars.Context]) -> None: - self._cancel_exception_msg: typing.Optional[str] = None - self._callback = callback self._args = args self._loop = loop @@ -494,15 +492,10 @@ class QAsyncioHandle(): self._callback(*self._args) self._state = QAsyncioHandle.HandleState.DONE - @Slot() - def _cancel_exception(self) -> None: - raise asyncio.CancelledError(self._cancel_exception_msg) - def cancel(self) -> None: if self._state == QAsyncioHandle.HandleState.PENDING: - self._state = QAsyncioHandle.HandleState.CANCELLED # The old timer that was created in _start will still trigger but _cb won't do anything. - self._schedule_event(0, lambda: self._cancel_exception()) + self._state = QAsyncioHandle.HandleState.CANCELLED def cancelled(self) -> bool: return self._state == QAsyncioHandle.HandleState.CANCELLED diff --git a/sources/pyside6/PySide6/QtAsyncio/futures.py b/sources/pyside6/PySide6/QtAsyncio/futures.py index 580f34db6..7ed8bcb64 100644 --- a/sources/pyside6/PySide6/QtAsyncio/futures.py +++ b/sources/pyside6/PySide6/QtAsyncio/futures.py @@ -61,7 +61,10 @@ class QAsyncioFuture(): if self._state == QAsyncioFuture.FutureState.DONE_WITH_EXCEPTION and self._exception: raise self._exception if self._state == QAsyncioFuture.FutureState.CANCELLED: - raise asyncio.CancelledError + if self._cancel_message: + raise asyncio.CancelledError(self._cancel_message) + else: + raise asyncio.CancelledError raise asyncio.InvalidStateError def set_result(self, result: typing.Any) -> None: diff --git a/sources/pyside6/PySide6/QtAsyncio/tasks.py b/sources/pyside6/PySide6/QtAsyncio/tasks.py index 027615f21..78f9dfb0c 100644 --- a/sources/pyside6/PySide6/QtAsyncio/tasks.py +++ b/sources/pyside6/PySide6/QtAsyncio/tasks.py @@ -135,8 +135,6 @@ class QAsyncioTask(futures.QAsyncioFuture): def cancel(self, msg: typing.Optional[str] = None) -> bool: if self.done(): return False - if (isinstance(self._handle, events.QAsyncioHandle)): - self._handle._cancel_exception_msg = msg self._cancel_message = msg self._handle.cancel() self._state = futures.QAsyncioFuture.FutureState.CANCELLED -- cgit v1.2.3