Add QgsPythonUtilsImpl::initGDAL() and run it from QGIS app

fixes #57344
This commit is contained in:
Even Rouault 2024-05-20 16:26:01 +02:00 committed by Nyall Dawson
parent 07067d37b5
commit 79dab4461e
4 changed files with 16 additions and 0 deletions

View File

@ -12523,6 +12523,7 @@ void QgisApp::loadPythonSupport()
// init python runner
QgsPythonRunner::setInstance( new QgsPythonRunnerImpl( mPythonUtils ) );
mPythonUtils->initGDAL();
// QgsMessageLog::logMessage( tr( "Python support ENABLED :-) " ), QString(), Qgis::MessageLevel::Info );
}
#endif

View File

@ -218,6 +218,11 @@ class PYTHON_EXPORT QgsPythonUtils
*/
virtual bool unloadPlugin( const QString &packageName ) = 0;
/**
* Initialize GDAL Python, turning on its exceptions.
* \since QGIS 3.38
*/
virtual void initGDAL() = 0;
};
#endif

View File

@ -755,3 +755,12 @@ QStringList QgsPythonUtilsImpl::listActivePlugins()
evalString( QStringLiteral( "'\\n'.join(qgis.utils.active_plugins)" ), output );
return output.split( QChar( '\n' ), Qt::SkipEmptyParts );
}
void QgsPythonUtilsImpl::initGDAL()
{
runString("from osgeo import gdal, ogr, osr");
// To avoid FutureWarning with GDAL >= 3.7.0
runString("gdal.UseExceptions()");
runString("ogr.UseExceptions()");
runString("osr.UseExceptions()");
}

View File

@ -92,6 +92,7 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
bool canUninstallPlugin( const QString &packageName ) final;
bool unloadPlugin( const QString &packageName ) final;
bool isPluginEnabled( const QString &packageName ) const final;
void initGDAL() final;
protected: