Skip to content

Commit 47d0f5d

Browse files
committed
Followup 73733a, fix crash when running invalid python strings
(cherry-picked from 7a8c3e0)
1 parent 707ff90 commit 47d0f5d

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

src/app/qgisapp.h

+2
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
17351735
bool gestureEvent( QGestureEvent *event );
17361736
void tapAndHoldTriggered( QTapAndHoldGesture *gesture );
17371737
#endif
1738+
1739+
friend class TestQgisAppPython;
17381740
};
17391741

17401742
#ifdef ANDROID

src/python/qgspythonutilsimpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ bool QgsPythonUtilsImpl::runStringUnsafe( const QString& command, bool single )
300300
// (non-unicode strings can be mangled)
301301
PyObject* obj = PyRun_String( command.toUtf8().data(), single ? Py_single_input : Py_file_input, mMainDict, mMainDict );
302302
bool res = nullptr == PyErr_Occurred();
303-
Py_DECREF( obj );
303+
Py_XDECREF( obj );
304304

305305
// we are done calling python API, release global interpreter lock
306306
PyGILState_Release( gstate );

tests/src/app/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ ENDMACRO (ADD_QGIS_TEST)
101101
#############################################################
102102
# Tests:
103103
104+
ADD_QGIS_TEST(apppythontest testqgisapppython.cpp)
104105
ADD_QGIS_TEST(qgisappclipboard testqgisappclipboard.cpp)
105106
ADD_QGIS_TEST(attributetabletest testqgsattributetable.cpp)
106107
ADD_QGIS_TEST(fieldcalculatortest testqgsfieldcalculator.cpp)

tests/src/app/testqgisapppython.cpp

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/***************************************************************************
2+
testqgsapppython.cpp
3+
--------------------
4+
Date : May 2016
5+
Copyright : (C) 2016 by Nyall Dawson
6+
Email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#include <QApplication>
16+
#include <QObject>
17+
#include <QSplashScreen>
18+
#include <QString>
19+
#include <QStringList>
20+
#include <QtTest/QtTest>
21+
22+
#include <qgisapp.h>
23+
#include <qgsapplication.h>
24+
25+
/** \ingroup UnitTests
26+
* This is a unit test for the QgisApp python support.
27+
*/
28+
class TestQgisAppPython : public QObject
29+
{
30+
Q_OBJECT
31+
32+
public:
33+
TestQgisAppPython();
34+
35+
private slots:
36+
void initTestCase();// will be called before the first testfunction is executed.
37+
void cleanupTestCase();// will be called after the last testfunction was executed.
38+
void init() {} // will be called before each testfunction is executed.
39+
void cleanup() {} // will be called after every testfunction.
40+
41+
void runString();
42+
void evalString();
43+
44+
private:
45+
QgisApp * mQgisApp;
46+
QString mTestDataDir;
47+
};
48+
49+
TestQgisAppPython::TestQgisAppPython()
50+
: mQgisApp( nullptr )
51+
{
52+
53+
}
54+
55+
//runs before all tests
56+
void TestQgisAppPython::initTestCase()
57+
{
58+
// Set up the QSettings environment
59+
QCoreApplication::setOrganizationName( "QGIS" );
60+
QCoreApplication::setOrganizationDomain( "qgis.org" );
61+
QCoreApplication::setApplicationName( "QGIS-TEST" );
62+
63+
qDebug() << "TestQgisAppClipboard::initTestCase()";
64+
// init QGIS's paths - true means that all path will be inited from prefix
65+
QgsApplication::init();
66+
QgsApplication::initQgis();
67+
mTestDataDir = QString( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
68+
mQgisApp = new QgisApp();
69+
mQgisApp->loadPythonSupport();
70+
}
71+
72+
//runs after all tests
73+
void TestQgisAppPython::cleanupTestCase()
74+
{
75+
QgsApplication::exitQgis();
76+
}
77+
78+
void TestQgisAppPython::runString()
79+
{
80+
QVERIFY( mQgisApp->mPythonUtils->runString( "a=1+1" ) );
81+
QVERIFY( !mQgisApp->mPythonUtils->runString( "x" ) );
82+
QVERIFY( !mQgisApp->mPythonUtils->runString( "" ) );
83+
}
84+
85+
void TestQgisAppPython::evalString()
86+
{
87+
QString result;
88+
//good string
89+
QVERIFY( mQgisApp->mPythonUtils->evalString( "1+1", result ) );
90+
QCOMPARE( result, QString( "2" ) );
91+
92+
//bad string
93+
QVERIFY( !mQgisApp->mPythonUtils->evalString( "1+", result ) );
94+
}
95+
96+
QTEST_MAIN( TestQgisAppPython )
97+
#include "testqgisapppython.moc"

0 commit comments

Comments
 (0)