Skip to content

Commit

Permalink
Merge pull request #5542 from nyalldawson/auth_manager_instance
Browse files Browse the repository at this point in the history
Fix crashes when no QgsApplication instance is available
  • Loading branch information
nyalldawson authored Nov 7, 2017
2 parents e3f3499 + 5b9283f commit edea38f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
37 changes: 32 additions & 5 deletions src/core/qgsapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,8 @@ void QgsApplication::initQgis()
// set the provider plugin path (this creates provider registry)
QgsProviderRegistry::instance( pluginPath() );

instance()->mDataItemProviderRegistry = new QgsDataItemProviderRegistry();
// create data item provider registry
( void )QgsApplication::dataItemProviderRegistry();

// create project instance if doesn't exist
QgsProject::instance();
Expand All @@ -889,11 +890,22 @@ void QgsApplication::initQgis()

QgsAuthManager *QgsApplication::authManager()
{
if ( ! instance()->mAuthManager )
if ( instance() )
{
instance()->mAuthManager = QgsAuthManager::instance();
if ( !instance()->mAuthManager )
{
instance()->mAuthManager = QgsAuthManager::instance();
}
return instance()->mAuthManager;
}
else
{
// no QgsApplication instance
static QgsAuthManager *sAuthManager = nullptr;
if ( !sAuthManager )
sAuthManager = QgsAuthManager::instance();
return sAuthManager;
}
return instance()->mAuthManager;
}


Expand Down Expand Up @@ -1548,7 +1560,22 @@ QgsRasterRendererRegistry *QgsApplication::rasterRendererRegistry()

QgsDataItemProviderRegistry *QgsApplication::dataItemProviderRegistry()
{
return instance()->mDataItemProviderRegistry;
if ( instance() )
{
if ( !instance()->mDataItemProviderRegistry )
{
instance()->mDataItemProviderRegistry = new QgsDataItemProviderRegistry();
}
return instance()->mDataItemProviderRegistry;
}
else
{
// no QgsApplication instance
static QgsDataItemProviderRegistry *sDataItemProviderRegistry = nullptr;
if ( !sDataItemProviderRegistry )
sDataItemProviderRegistry = new QgsDataItemProviderRegistry();
return sDataItemProviderRegistry;
}
}

QgsSvgCache *QgsApplication::svgCache()
Expand Down
17 changes: 16 additions & 1 deletion tests/src/python/test_qgsnoapplication.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
__revision__ = '$Format:%H$'

import qgis # NOQA

import sys
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsApplication)
from qgis.testing import unittest

Expand All @@ -28,6 +29,8 @@
QgsApplication instance!
"""

app = QCoreApplication(sys.argv)


class TestQgsNoApplication(unittest.TestCase):

Expand All @@ -53,6 +56,18 @@ def testNullRepresentation(self):
QgsApplication.setNullRepresentation(nr)
self.assertEqual(QgsApplication.nullRepresentation(), nr)

def testAuthManager(self):
self.assertTrue(QgsApplication.authManager())

def testDataItemProviderRegistry(self):
self.assertTrue(QgsApplication.dataItemProviderRegistry())

def testInit(self):
"""
Test calling QgsApplication.initQgis() without QgsApplication instance
"""
QgsApplication.initQgis()


if __name__ == '__main__':
unittest.main()

0 comments on commit edea38f

Please sign in to comment.