diff options
| author | Adrian Herrmann <adrian.herrmann@qt.io> | 2023-11-22 20:18:16 +0100 |
|---|---|---|
| committer | Adrian Herrmann <adrian.herrmann@qt.io> | 2023-11-24 19:03:49 +0100 |
| commit | 3078f67ec5f88a3a96d223798abe872eda8c5dbe (patch) | |
| tree | e4f995c2e51a2fbeb60dce4d26b9de92aa039ea4 /sources/pyside6/PySide6/QtAsyncio/tasks.py | |
| parent | 671c182dce91c387037894017be314564e054c00 (diff) | |
QtAsyncio: Handle exceptions properly in task step
Refactor the task step function to handle exceptions properly, as the
self._coro.throw() code path was not usually reachable.
Pick-to: 6.6
Task-number: PYSIDE-769
Change-Id: I31d50f700857a47bf1df5f0f02fb2fa313c1c045
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@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.py | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/sources/pyside6/PySide6/QtAsyncio/tasks.py b/sources/pyside6/PySide6/QtAsyncio/tasks.py index 7bca955b0..027615f21 100644 --- a/sources/pyside6/PySide6/QtAsyncio/tasks.py +++ b/sources/pyside6/PySide6/QtAsyncio/tasks.py @@ -62,7 +62,7 @@ class QAsyncioTask(futures.QAsyncioFuture): def _step(self, exception_or_future: typing.Union[ - Exception, futures.QAsyncioFuture, None] = None) -> None: + BaseException, futures.QAsyncioFuture, None] = None) -> None: if self.done(): return result = None @@ -72,13 +72,13 @@ class QAsyncioTask(futures.QAsyncioFuture): if exception_or_future is None: result = self._coro.send(None) elif asyncio.futures.isfuture(exception_or_future): - # If the future has an exception set by set_exception(), this will raise it. - # If the future has been cancelled, this will raise CancelledError. - # If the future's result isn't yet available, this will raise InvalidStateError. - exception_or_future.result() - exception_or_future = None - result = self._coro.send(None) - elif isinstance(exception_or_future, Exception): + try: + exception_or_future.result() + except BaseException as e: + result = self._coro.throw(e) + else: + result = self._coro.send(None) + elif isinstance(exception_or_future, BaseException): result = self._coro.throw(exception_or_future) except StopIteration as e: self._state = futures.QAsyncioFuture.FutureState.DONE_WITH_RESULT @@ -86,15 +86,18 @@ class QAsyncioTask(futures.QAsyncioFuture): except concurrent.futures.CancelledError as e: self._state = futures.QAsyncioFuture.FutureState.CANCELLED self._exception = e - except Exception as e: + except BaseException as e: self._state = futures.QAsyncioFuture.FutureState.DONE_WITH_EXCEPTION self._exception = e # type: ignore[assignment] else: if asyncio.futures.isfuture(result): result.add_done_callback( self._step, context=self._context) # type: ignore[arg-type] + elif result is None: + self._loop.call_soon(self._step, context=self._context) else: - self._loop.call_soon(self._step, exception_or_future, context=self._context) + exception = RuntimeError(f"Bad task result: {result}") + self._loop.call_soon(self._step, exception, context=self._context) finally: asyncio._leave_task(self._loop, self) # type: ignore[arg-type] if self._exception: |
