From 6cfc34b96d9a54bad04a5fbf2b13a3d0baf6cda1 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Fri, 13 Dec 2024 08:21:11 +1000 Subject: [PATCH] Fix crash in server VirtualErrorHandler on newer sip environments Fixes crash in tests, and adapts these VirtualErrorHandlers to the same code used elsewhere in QGIS --- .ci/test_blocklist_qt6.txt | 1 - python/PyQt6/server/server.sip.in | 42 +++++++++++++++++++++++-------- python/server/server.sip.in | 40 +++++++++++++++++++++-------- 3 files changed, 62 insertions(+), 21 deletions(-) diff --git a/.ci/test_blocklist_qt6.txt b/.ci/test_blocklist_qt6.txt index c1cb435c5b0b..c064728657ea 100644 --- a/.ci/test_blocklist_qt6.txt +++ b/.ci/test_blocklist_qt6.txt @@ -58,7 +58,6 @@ PyQgsVectorLayerEditBuffer PyQgsLayerDefinition PyQgsSettings PyQgsSettingsEntry -PyQgsServerApi PyQgsServerWMSGetFeatureInfo PyQgsServerWMSGetMap PyQgsServerAccessControlWFSTransactional diff --git a/python/PyQt6/server/server.sip.in b/python/PyQt6/server/server.sip.in index a32d92d1cb72..2032ffca677f 100644 --- a/python/PyQt6/server/server.sip.in +++ b/python/PyQt6/server/server.sip.in @@ -15,19 +15,30 @@ ${DEFAULTDOCSTRINGSIGNATURE} %Include server_auto.sip + %VirtualErrorHandler serverapi_badrequest_exception_handler - PyObject *type, *exception, *traceback, *pyStrException; + PyObject *type, *exception, *traceback; PyErr_Fetch(&type, &exception, &traceback); - pyStrException = PyObject_Str(exception); - Py_DECREF(pyStrException); + // check whether the object is already a unicode string + QString pyStrException; + if ( PyUnicode_Check( exception) ) + { + pyStrException = QString::fromUtf8( PyUnicode_AsUTF8( exception ) ); + } + else + { + PyObject* str = PyObject_Str( exception ); + pyStrException = QString::fromUtf8( PyUnicode_AsUTF8( str ) ); + Py_XDECREF( str ); + } SIP_RELEASE_GIL( sipGILState ); QString strException = "API bad request error"; - if ( pyStrException && PyUnicode_Check(pyStrException) ) + if ( !pyStrException.isEmpty() ) { - strException = QString::fromUtf8( PyUnicode_AsUTF8(pyStrException) ); + strException = pyStrException; } throw QgsServerApiBadRequestException( strException ); @@ -35,19 +46,30 @@ ${DEFAULTDOCSTRINGSIGNATURE} %VirtualErrorHandler server_exception_handler - PyObject *type, *exception, *traceback, *pyStrException; + PyObject *type, *exception, *traceback; PyErr_Fetch(&type, &exception, &traceback); - pyStrException = PyObject_Str(exception); - Py_DECREF(pyStrException); + // check whether the object is already a unicode string + QString pyStrException; + if ( PyUnicode_Check( exception) ) + { + pyStrException = QString::fromUtf8( PyUnicode_AsUTF8( exception ) ); + } + else + { + PyObject* str = PyObject_Str( exception ); + pyStrException = QString::fromUtf8( PyUnicode_AsUTF8( str ) ); + Py_XDECREF( str ); + } SIP_RELEASE_GIL( sipGILState ); QString strException = "Server internal error"; - if ( pyStrException && PyUnicode_Check(pyStrException) ) + if ( !pyStrException.isEmpty() ) { - strException = QString::fromUtf8( PyUnicode_AsUTF8(pyStrException) ); + strException = pyStrException; } throw QgsServerException( strException ); %End + diff --git a/python/server/server.sip.in b/python/server/server.sip.in index a32d92d1cb72..69491f1d2f07 100644 --- a/python/server/server.sip.in +++ b/python/server/server.sip.in @@ -16,18 +16,28 @@ ${DEFAULTDOCSTRINGSIGNATURE} %VirtualErrorHandler serverapi_badrequest_exception_handler - PyObject *type, *exception, *traceback, *pyStrException; + PyObject *type, *exception, *traceback; PyErr_Fetch(&type, &exception, &traceback); - pyStrException = PyObject_Str(exception); - Py_DECREF(pyStrException); + // check whether the object is already a unicode string + QString pyStrException; + if ( PyUnicode_Check( exception) ) + { + pyStrException = QString::fromUtf8( PyUnicode_AsUTF8( exception ) ); + } + else + { + PyObject* str = PyObject_Str( exception ); + pyStrException = QString::fromUtf8( PyUnicode_AsUTF8( str ) ); + Py_XDECREF( str ); + } SIP_RELEASE_GIL( sipGILState ); QString strException = "API bad request error"; - if ( pyStrException && PyUnicode_Check(pyStrException) ) + if ( !pyStrException.isEmpty() ) { - strException = QString::fromUtf8( PyUnicode_AsUTF8(pyStrException) ); + strException = pyStrException; } throw QgsServerApiBadRequestException( strException ); @@ -35,18 +45,28 @@ ${DEFAULTDOCSTRINGSIGNATURE} %VirtualErrorHandler server_exception_handler - PyObject *type, *exception, *traceback, *pyStrException; + PyObject *type, *exception, *traceback; PyErr_Fetch(&type, &exception, &traceback); - pyStrException = PyObject_Str(exception); - Py_DECREF(pyStrException); + // check whether the object is already a unicode string + QString pyStrException; + if ( PyUnicode_Check( exception) ) + { + pyStrException = QString::fromUtf8( PyUnicode_AsUTF8( exception ) ); + } + else + { + PyObject* str = PyObject_Str( exception ); + pyStrException = QString::fromUtf8( PyUnicode_AsUTF8( str ) ); + Py_XDECREF( str ); + } SIP_RELEASE_GIL( sipGILState ); QString strException = "Server internal error"; - if ( pyStrException && PyUnicode_Check(pyStrException) ) + if ( !pyStrException.isEmpty() ) { - strException = QString::fromUtf8( PyUnicode_AsUTF8(pyStrException) ); + strException = pyStrException; } throw QgsServerException( strException );