From d8ca812879ad70cd2aa914ea76cd529786deadc6 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Thu, 17 Apr 2025 14:49:57 +0200 Subject: libshiboken: Add utility class for stashing Python errors It encapsulates fetching/restoring errors and uses the old or new exception API depending on version. Task-number: PYSIDE-3067 Change-Id: I6e39d92c7e79fed864b364a90c5bd5b474a41ed6 Reviewed-by: Christian Tismer --- sources/pyside6/libpysideqml/pysideqmlmetacallerror.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'sources/pyside6/libpysideqml/pysideqmlmetacallerror.cpp') diff --git a/sources/pyside6/libpysideqml/pysideqmlmetacallerror.cpp b/sources/pyside6/libpysideqml/pysideqmlmetacallerror.cpp index a3d2664c4..4e0afa3b2 100644 --- a/sources/pyside6/libpysideqml/pysideqmlmetacallerror.cpp +++ b/sources/pyside6/libpysideqml/pysideqmlmetacallerror.cpp @@ -5,6 +5,7 @@ #include #include +#include #include // Remove deprecated MACRO of copysign for MSVC #86286 @@ -40,17 +41,17 @@ std::optional qmlMetaCallErrorHandler(QObject *object) if (engine->currentStackFrame == nullptr) return {}; - PyObject *errType{}; - PyObject *errValue{}; - PyObject *errTraceback{}; - PyErr_Fetch(&errType, &errValue, &errTraceback); + Shiboken::Errors::Stash errorStash; + PyObject *errValue = errorStash.getException(); // PYSIDE-464: The error is only valid before PyErr_Restore, // PYSIDE-464: therefore we take local copies. Shiboken::AutoDecRef objStr(PyObject_Str(errValue)); const QString errString = QString::fromUtf8(Shiboken::String::toCString(objStr)); - const bool isSyntaxError = errType == PyExc_SyntaxError; - const bool isTypeError = errType == PyExc_TypeError; - PyErr_Restore(errType, errValue, errTraceback); + const bool isSyntaxError = errValue != nullptr + && PyErr_GivenExceptionMatches(errValue, PyExc_SyntaxError); + const bool isTypeError = errValue != nullptr + && PyErr_GivenExceptionMatches(errValue, PyExc_TypeError); + errorStash.restore(); PyErr_Print(); // Note: PyErr_Print clears the error. -- cgit v1.2.3