Skip to content
Permalink
Browse files

Merge pull request #5542 from nyalldawson/auth_manager_instance

Fix crashes when no QgsApplication instance is available
  • Loading branch information
nyalldawson committed Nov 7, 2017
2 parents e3f3499 + 5b9283f commit edea38f7c7251ec446399c91d60672159e63017a
Showing with 48 additions and 6 deletions.
  1. +32 −5 src/core/qgsapplication.cpp
  2. +16 −1 tests/src/python/test_qgsnoapplication.py
@@ -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();
@@ -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;
}


@@ -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()
@@ -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

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

app = QCoreApplication(sys.argv)


class TestQgsNoApplication(unittest.TestCase):

@@ -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.
You can’t perform that action at this time.