diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2025-04-17 14:12:16 +0200 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2025-05-07 21:10:38 +0200 |
| commit | cffe2bc71d8e5d88efb879e6fe68c730e547fc4d (patch) | |
| tree | 9c489c16cffc3d5cbac0b2b5bf945a7fd494fa6b | |
| parent | be29ee033a6c8e3438fd40cace545f8a26353bf5 (diff) | |
libshiboken: Use PyErr_SetRaisedException() for unraisable exceptions
Complements baedbe8353ee20eb7ac98e67b7985eb80959f14f.
Task-number: PYSIDE-3067
Task-number: PYSIDE-2310
Task-number: PYSIDE-2321
Task-number: PYSIDE-2335
Change-Id: Ie55a89eab0743b25095cab56d38ce321ce5a0131
Reviewed-by: Christian Tismer <tismer@stackless.com>
| -rw-r--r-- | sources/shiboken6/libshiboken/sbkerrors.cpp | 49 |
1 files changed, 38 insertions, 11 deletions
diff --git a/sources/shiboken6/libshiboken/sbkerrors.cpp b/sources/shiboken6/libshiboken/sbkerrors.cpp index 8dc3c639c..247fbefc4 100644 --- a/sources/shiboken6/libshiboken/sbkerrors.cpp +++ b/sources/shiboken6/libshiboken/sbkerrors.cpp @@ -129,13 +129,42 @@ static bool prependToExceptionMessage(PyObject *exc, const char *context) return true; } -struct ErrorStore { - PyObject *type; - PyObject *exc; - PyObject *traceback; +struct ErrorStore +{ + operator bool() const { return exc != nullptr; } + + PyObject *exc = nullptr; +#ifdef PEP_OLD_ERR_API + PyObject *traceback = nullptr; + PyObject *type = nullptr; +#endif }; -static thread_local ErrorStore savedError{}; +static void fetchError(ErrorStore &s) +{ +#ifdef PEP_OLD_ERR_API + PyErr_Fetch(&s.type, &s.exc, &s.traceback); +#else + s.exc = PyErr_GetRaisedException(); +#endif +} + +static void restoreError(ErrorStore &s) +{ +#ifdef PEP_OLD_ERR_API + PyErr_Restore(s.type, s.exc, s.traceback); + s.type = s.exc = s.traceback = nullptr; +#else + if (s.exc) { + PyErr_SetRaisedException(s.exc); + s.exc = nullptr; + } else { + PyErr_Clear(); + } +#endif +} + +static thread_local ErrorStore savedError; static bool hasPythonContext() { @@ -148,7 +177,7 @@ void storeErrorOrPrint() // Therefore, we handle the error when we are error checking, anyway. // But we do that only when we know that an error handler can pick it up. if (hasPythonContext()) - PyErr_Fetch(&savedError.type, &savedError.exc, &savedError.traceback); + fetchError(savedError); else PyErr_Print(); } @@ -158,7 +187,7 @@ void storeErrorOrPrint() static void storeErrorOrPrintWithContext(const char *context) { if (hasPythonContext()) { - PyErr_Fetch(&savedError.type, &savedError.exc, &savedError.traceback); + fetchError(savedError); prependToExceptionMessage(savedError.exc, context); } else { std::fputs(context, stderr); @@ -175,10 +204,8 @@ void storePythonOverrideErrorOrPrint(const char *className, const char *funcName PyObject *occurred() { - if (savedError.type) { - PyErr_Restore(savedError.type, savedError.exc, savedError.traceback); - savedError.type = nullptr; - } + if (savedError) + restoreError(savedError); return PyErr_Occurred(); } |
