Navigation Menu

Skip to content

Commit

Permalink
Make calls to python api safer by ensuring we have global interpreter…
Browse files Browse the repository at this point in the history
… lock

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14434 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Oct 25, 2010
1 parent bdcca91 commit 1b71ee0
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions src/python/qgspythonutilsimpl.cpp
Expand Up @@ -139,11 +139,21 @@ void QgsPythonUtilsImpl::uninstallErrorHook()

bool QgsPythonUtilsImpl::runStringUnsafe( const QString& command, bool single )
{
// acquire global interpreter lock to ensure we are in a consistent state
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

// TODO: convert special characters from unicode strings u"..." to \uXXXX
// so that they're not mangled to utf-8
// (non-unicode strings can be mangled)
PyRun_String( command.toUtf8().data(), single ? Py_single_input : Py_file_input, mMainDict, mMainDict );
return ( PyErr_Occurred() == 0 );

bool res = ( PyErr_Occurred() == 0 );

// we are done calling python API, release global interpreter lock
PyGILState_Release( gstate );

return res;
}

bool QgsPythonUtilsImpl::runString( const QString& command, QString msgOnError )
Expand Down Expand Up @@ -184,6 +194,9 @@ QString QgsPythonUtilsImpl::getTraceback()
{
#define TRACEBACK_FETCH_ERROR(what) {errMsg = what; goto done;}

// acquire global interpreter lock to ensure we are in a consistent state
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

QString errMsg;
QString result;
Expand Down Expand Up @@ -249,6 +262,9 @@ QString QgsPythonUtilsImpl::getTraceback()
Py_XDECREF( traceback );
Py_XDECREF( type );

// we are done calling python API, release global interpreter lock
PyGILState_Release( gstate );

return result;
}

Expand Down Expand Up @@ -276,8 +292,15 @@ QString QgsPythonUtilsImpl::getTypeAsString( PyObject* obj )

bool QgsPythonUtilsImpl::getError( QString& errorClassName, QString& errorText )
{
// acquire global interpreter lock to ensure we are in a consistent state
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

if ( !PyErr_Occurred() )
{
PyGILState_Release( gstate );
return false;
}

PyObject* err_type;
PyObject* err_value;
Expand All @@ -302,6 +325,9 @@ bool QgsPythonUtilsImpl::getError( QString& errorClassName, QString& errorText )
Py_XDECREF( err_value );
Py_XDECREF( err_tb );

// we are done calling python API, release global interpreter lock
PyGILState_Release( gstate );

return true;
}

Expand Down Expand Up @@ -369,16 +395,22 @@ QString QgsPythonUtilsImpl::PyObjectToQString( PyObject* obj )

bool QgsPythonUtilsImpl::evalString( const QString& command, QString& result )
{
// acquire global interpreter lock to ensure we are in a consistent state
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

PyObject* res = PyRun_String( command.toUtf8().data(), Py_eval_input, mMainDict, mMainDict );
bool success = ( res != NULL );

// TODO: error handling

if ( res != NULL )
{
if ( success )
result = PyObjectToQString( res );
return true;
}
return false;

// we are done calling python API, release global interpreter lock
PyGILState_Release( gstate );

return success;
}


Expand Down

0 comments on commit 1b71ee0

Please sign in to comment.