aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/PySide6/QtAsyncio/tasks.py
diff options
context:
space:
mode:
authorAdrian Herrmann <adrian.herrmann@qt.io>2024-07-03 18:36:20 +0200
committerAdrian Herrmann <adrian.herrmann@qt.io>2024-07-16 06:32:02 +0200
commit526bc12e42db7c6305bcc28ad8f6b7554597d725 (patch)
treea98f909fdf5c893bfe5d2937e0246acdae688dea /sources/pyside6/PySide6/QtAsyncio/tasks.py
parent32c36073e212e7ca17b1f7578d61195aefd9dbca (diff)
QtAsyncio: Add cancel count and uncancel
Implement the QAsyncioTask.uncancel() function and the associated cancel count. Note to reader: Unlike what the name suggests, the uncancel() function on its own does not undo a task cancellation. This must be performed by consuming the CancelledError exception, at which point uncancel() serves to remove the cancellation state. Pick-to: 6.7 Task-number: PYSIDE-769 Fixes: PYSIDE-2790 Change-Id: I4e817e1dd3f49179855432d20ed2f043090fd8f1 Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/pyside6/PySide6/QtAsyncio/tasks.py')
-rw-r--r--sources/pyside6/PySide6/QtAsyncio/tasks.py27
1 files changed, 18 insertions, 9 deletions
diff --git a/sources/pyside6/PySide6/QtAsyncio/tasks.py b/sources/pyside6/PySide6/QtAsyncio/tasks.py
index bd7884838..9a5698432 100644
--- a/sources/pyside6/PySide6/QtAsyncio/tasks.py
+++ b/sources/pyside6/PySide6/QtAsyncio/tasks.py
@@ -34,6 +34,7 @@ class QAsyncioTask(futures.QAsyncioFuture):
self._future_to_await: asyncio.Future | None = None
self._cancelled = False
+ self._cancel_count = 0
self._cancel_message: str | None = None
# https://docs.python.org/3/library/asyncio-extending.html#task-lifetime-support
@@ -77,6 +78,10 @@ class QAsyncioTask(futures.QAsyncioFuture):
result = None
self._future_to_await = None
+ if self._cancelled:
+ exception_or_future = asyncio.CancelledError(self._cancel_message)
+ self._cancelled = False
+
if asyncio.futures.isfuture(exception_or_future):
try:
exception_or_future.result()
@@ -135,10 +140,13 @@ class QAsyncioTask(futures.QAsyncioFuture):
asyncio._leave_task(self._loop, self) # type: ignore[arg-type]
if self._exception:
+ message = str(self._exception)
+ if message == "None":
+ message = ""
+ else:
+ message = "An exception occurred during task execution"
self._loop.call_exception_handler({
- "message": (str(self._exception) if self._exception
- else "An exception occurred during task "
- "execution"),
+ "message": message,
"exception": self._exception,
"task": self,
"future": (exception_or_future
@@ -172,6 +180,7 @@ class QAsyncioTask(futures.QAsyncioFuture):
def cancel(self, msg: str | None = None) -> bool:
if self.done():
return False
+ self._cancel_count += 1
self._cancel_message = msg
self._handle.cancel()
if self._future_to_await is not None:
@@ -181,10 +190,10 @@ class QAsyncioTask(futures.QAsyncioFuture):
self._cancelled = True
return True
- def uncancel(self) -> None:
- # TODO
- raise NotImplementedError("QtTask.uncancel is not implemented")
+ def uncancel(self) -> int:
+ if self._cancel_count > 0:
+ self._cancel_count -= 1
+ return self._cancel_count
- def cancelling(self) -> bool:
- # TODO
- raise NotImplementedError("QtTask.cancelling is not implemented")
+ def cancelling(self) -> int:
+ return self._cancel_count