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 = nullptr != res; // TODO: error handling if ( success ) result = PyObjectToQString( res ); // we are done calling python API, release global interpreter lock PyGILState_Release( gstate ); return success; }
QString QgsPythonUtilsImpl::getTypeAsString( PyObject* obj ) { if ( obj == NULL ) return NULL; if ( PyClass_Check( obj ) ) { QgsDebugMsg( "got class" ); return QString( PyString_AsString((( PyClassObject* )obj )->cl_name ) ); } else if ( PyType_Check( obj ) ) { QgsDebugMsg( "got type" ); return QString((( PyTypeObject* )obj )->tp_name ); } else { QgsDebugMsg( "got object" ); return PyObjectToQString( 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; PyObject* err_tb; // get the exception information and clear error PyErr_Fetch( &err_type, &err_value, &err_tb ); // get exception's class name errorClassName = getTypeAsString( err_type ); // get exception's text if ( nullptr != err_value && err_value != Py_None ) { errorText = PyObjectToQString( err_value ); } else errorText.clear(); // cleanup Py_XDECREF( err_type ); Py_XDECREF( err_value ); Py_XDECREF( err_tb ); // we are done calling python API, release global interpreter lock PyGILState_Release( gstate ); return true; }
QString QgsPythonUtilsImpl::getTypeAsString( PyObject* obj ) { if ( !obj ) return nullptr; #if (PY_VERSION_HEX < 0x03000000) if ( PyClass_Check( obj ) ) { QgsDebugMsg( "got class" ); return QString( PyString_AsString((( PyClassObject* )obj )->cl_name ) ); } else #endif if ( PyType_Check( obj ) ) { QgsDebugMsg( "got type" ); return QString((( PyTypeObject* )obj )->tp_name ); } else { QgsDebugMsg( "got object" ); return PyObjectToQString( obj ); } }