Skip to content

Commit

Permalink
Fix crash in server VirtualErrorHandler on newer sip environments
Browse files Browse the repository at this point in the history
Fixes crash in tests, and adapts these VirtualErrorHandlers to the same
code used elsewhere in QGIS
  • Loading branch information
nyalldawson committed Dec 18, 2024
1 parent ad57bfa commit 6cfc34b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 21 deletions.
1 change: 0 additions & 1 deletion .ci/test_blocklist_qt6.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ PyQgsVectorLayerEditBuffer
PyQgsLayerDefinition
PyQgsSettings
PyQgsSettingsEntry
PyQgsServerApi
PyQgsServerWMSGetFeatureInfo
PyQgsServerWMSGetMap
PyQgsServerAccessControlWFSTransactional
Expand Down
42 changes: 32 additions & 10 deletions python/PyQt6/server/server.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,61 @@ ${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 );
%End


%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

40 changes: 30 additions & 10 deletions python/server/server.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,57 @@ ${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 );
%End


%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 );
Expand Down

0 comments on commit 6cfc34b

Please sign in to comment.