Skip to content
Permalink
Browse files

Followup 73733a, fix crash when running invalid python strings

  • Loading branch information
nyalldawson committed May 23, 2016
1 parent 71712ac commit 7a8c3e0d2a2aa70c9c50e15d050a28da77763422
Showing with 101 additions and 1 deletion.
  1. +2 −0 src/app/qgisapp.h
  2. +1 −1 src/python/qgspythonutilsimpl.cpp
  3. +1 −0 tests/src/app/CMakeLists.txt
  4. +97 −0 tests/src/app/testqgisapppython.cpp
@@ -1764,6 +1764,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
bool gestureEvent( QGestureEvent *event );
void tapAndHoldTriggered( QTapAndHoldGesture *gesture );
#endif

friend class TestQgisAppPython;
};

#ifdef ANDROID
@@ -307,7 +307,7 @@ bool QgsPythonUtilsImpl::runStringUnsafe( const QString& command, bool single )
// (non-unicode strings can be mangled)
PyObject* obj = PyRun_String( command.toUtf8().data(), single ? Py_single_input : Py_file_input, mMainDict, mMainDict );
bool res = nullptr == PyErr_Occurred();
Py_DECREF( obj );
Py_XDECREF( obj );

// we are done calling python API, release global interpreter lock
PyGILState_Release( gstate );
@@ -101,6 +101,7 @@ ENDMACRO (ADD_QGIS_TEST)
#############################################################
# Tests:

ADD_QGIS_TEST(apppythontest testqgisapppython.cpp)
ADD_QGIS_TEST(qgisappclipboard testqgisappclipboard.cpp)
ADD_QGIS_TEST(attributetabletest testqgsattributetable.cpp)
ADD_QGIS_TEST(fieldcalculatortest testqgsfieldcalculator.cpp)
@@ -0,0 +1,97 @@
/***************************************************************************
testqgsapppython.cpp
--------------------
Date : May 2016
Copyright : (C) 2016 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QApplication>
#include <QObject>
#include <QSplashScreen>
#include <QString>
#include <QStringList>
#include <QtTest/QtTest>

#include <qgisapp.h>
#include <qgsapplication.h>

/** \ingroup UnitTests
* This is a unit test for the QgisApp python support.
*/
class TestQgisAppPython : public QObject
{
Q_OBJECT

public:
TestQgisAppPython();

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init() {} // will be called before each testfunction is executed.
void cleanup() {} // will be called after every testfunction.

void runString();
void evalString();

private:
QgisApp * mQgisApp;
QString mTestDataDir;
};

TestQgisAppPython::TestQgisAppPython()
: mQgisApp( nullptr )
{

}

//runs before all tests
void TestQgisAppPython::initTestCase()
{
// Set up the QSettings environment
QCoreApplication::setOrganizationName( "QGIS" );
QCoreApplication::setOrganizationDomain( "qgis.org" );
QCoreApplication::setApplicationName( "QGIS-TEST" );

qDebug() << "TestQgisAppClipboard::initTestCase()";
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();
mTestDataDir = QString( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
mQgisApp = new QgisApp();
mQgisApp->loadPythonSupport();
}

//runs after all tests
void TestQgisAppPython::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgisAppPython::runString()
{
QVERIFY( mQgisApp->mPythonUtils->runString( "a=1+1" ) );
QVERIFY( !mQgisApp->mPythonUtils->runString( "x" ) );
QVERIFY( !mQgisApp->mPythonUtils->runString( "" ) );
}

void TestQgisAppPython::evalString()
{
QString result;
//good string
QVERIFY( mQgisApp->mPythonUtils->evalString( "1+1", result ) );
QCOMPARE( result, QString( "2" ) );

//bad string
QVERIFY( !mQgisApp->mPythonUtils->evalString( "1+", result ) );
}

QTEST_MAIN( TestQgisAppPython )
#include "testqgisapppython.moc"

0 comments on commit 7a8c3e0

Please sign in to comment.
You can’t perform that action at this time.