aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/PySide6
diff options
context:
space:
mode:
authorLoan Guilbaud <guilbaudl.dev@gmail.com>2025-02-27 22:04:44 +0100
committerLoan Guilbaud <guilbaudl.dev@gmail.com>2025-03-04 11:40:33 +0100
commitc253dcda92cc2efb45197190beaccb926afb120c (patch)
treea11d78340886b5ec0b355f3578ce693964673983 /sources/pyside6/PySide6
parent934e4db8ea95843f1585c81b28613c97eca7e2ce (diff)
QtAsyncio: Improve logging when an Exception occurs in a task
When an exception occurs in a task, the logging is not helpful since it prints out nothing specific about the error that occurred. Catching the traceback and printing it out with the task in which it happened brings a lot of details to resolve the exception faster. Task-number: PYSIDE-3001 Pick-to: 6.8 Change-Id: If199da28a37406779ac5bec178fe756d1757b08c Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/pyside6/PySide6')
-rw-r--r--sources/pyside6/PySide6/QtAsyncio/events.py4
-rw-r--r--sources/pyside6/PySide6/QtAsyncio/tasks.py7
2 files changed, 9 insertions, 2 deletions
diff --git a/sources/pyside6/PySide6/QtAsyncio/events.py b/sources/pyside6/PySide6/QtAsyncio/events.py
index 36c7fea97..f99b601f7 100644
--- a/sources/pyside6/PySide6/QtAsyncio/events.py
+++ b/sources/pyside6/PySide6/QtAsyncio/events.py
@@ -598,7 +598,9 @@ class QAsyncioEventLoop(asyncio.BaseEventLoop, QObject):
def default_exception_handler(self, context: dict[str, Any]) -> None:
# TODO
if context["message"]:
- print(context["message"])
+ print(f"{context["message"]} from task {context["task"]._name},"+
+ "read the following traceback:")
+ print(context["traceback"])
def call_exception_handler(self, context: dict[str, Any]) -> None:
if self._exception_handler is not None:
diff --git a/sources/pyside6/PySide6/QtAsyncio/tasks.py b/sources/pyside6/PySide6/QtAsyncio/tasks.py
index be1809d5c..deabf690d 100644
--- a/sources/pyside6/PySide6/QtAsyncio/tasks.py
+++ b/sources/pyside6/PySide6/QtAsyncio/tasks.py
@@ -4,6 +4,7 @@ from __future__ import annotations
from . import events
from . import futures
+import traceback
from typing import Any
@@ -38,6 +39,8 @@ class QAsyncioTask(futures.QAsyncioFuture):
self._cancelled = False # PYSIDE-2644; see _step
self._cancel_count = 0
self._cancel_message: str | None = None
+ # Store traceback in case of Exception. Useful when exception happens in coroutine
+ self._tb: str = None
# https://docs.python.org/3/library/asyncio-extending.html#task-lifetime-support
asyncio._register_task(self) # type: ignore[arg-type]
@@ -113,6 +116,7 @@ class QAsyncioTask(futures.QAsyncioFuture):
except BaseException as e:
self._state = futures.QAsyncioFuture.FutureState.DONE_WITH_EXCEPTION
self._exception = e
+ self._tb = traceback.format_exc()
else:
if asyncio.futures.isfuture(result):
# If the coroutine yields a future, the task will await its
@@ -159,7 +163,8 @@ class QAsyncioTask(futures.QAsyncioFuture):
"task": self,
"future": (exception_or_future
if asyncio.futures.isfuture(exception_or_future)
- else None)
+ else None),
+ "traceback": self._tb
})
if self.done():