Skip to content

Commit a079b66

Browse files
committed
Allow python to be initialised without interface or error hook
1 parent 28df1b4 commit a079b66

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

src/app/qgisapp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10511,7 +10511,7 @@ void QgisApp::loadPythonSupport()
1051110511
mPythonUtils = pythonlib_inst();
1051210512
if ( mPythonUtils )
1051310513
{
10514-
mPythonUtils->initPython( mQgisInterface );
10514+
mPythonUtils->initPython( mQgisInterface, true );
1051510515
}
1051610516

1051710517
if ( mPythonUtils && mPythonUtils->isEnabled() )

src/python/qgspythonutils.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,15 @@ class PYTHON_EXPORT QgsPythonUtils
5050
//! returns TRUE if Python support is ready to use (must be inited first)
5151
virtual bool isEnabled() = 0;
5252

53-
//! initialize Python and import bindings
54-
virtual void initPython( QgisInterface *iface ) = 0;
53+
/**
54+
* Initialize Python and import bindings.
55+
*
56+
* The \a iface argument should be set to an instance of the QGIS interface, or
57+
* NULLPTR if no interface is available.
58+
*
59+
* If \a installErrorHook is true then the custom QGIS GUI error hook will be used.
60+
*/
61+
virtual void initPython( QgisInterface *iface, bool installErrorHook ) = 0;
5562

5663
#ifdef HAVE_SERVER_PYTHON_PLUGINS
5764
//! initialize Python and import server bindings

src/python/qgspythonutilsimpl.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ PyThreadState *_mainState = nullptr;
3939

4040
QgsPythonUtilsImpl::QgsPythonUtilsImpl()
4141
{
42-
mMainModule = nullptr;
43-
mMainDict = nullptr;
44-
mPythonEnabled = false;
4542
}
4643

4744
QgsPythonUtilsImpl::~QgsPythonUtilsImpl()
@@ -208,23 +205,29 @@ void QgsPythonUtilsImpl::doCustomImports()
208205
}
209206
}
210207

211-
void QgsPythonUtilsImpl::initPython( QgisInterface *interface )
208+
void QgsPythonUtilsImpl::initPython( QgisInterface *interface, const bool installErrorHook )
212209
{
213210
init();
214211
if ( !checkSystemImports() )
215212
{
216213
exitPython();
217214
return;
218215
}
219-
// initialize 'iface' object
220-
runString( "qgis.utils.initInterface(" + QString::number( ( quint64 ) interface ) + ')' );
216+
217+
if ( interface )
218+
{
219+
// initialize 'iface' object
220+
runString( "qgis.utils.initInterface(" + QString::number( ( quint64 ) interface ) + ')' );
221+
}
222+
221223
if ( !checkQgisUser() )
222224
{
223225
exitPython();
224226
return;
225227
}
226228
doCustomImports();
227-
installErrorHook();
229+
if ( installErrorHook )
230+
QgsPythonUtilsImpl::installErrorHook();
228231
finish();
229232
}
230233

@@ -265,7 +268,8 @@ bool QgsPythonUtilsImpl::startServerPlugin( QString packageName )
265268

266269
void QgsPythonUtilsImpl::exitPython()
267270
{
268-
uninstallErrorHook();
271+
if ( mErrorHookInstalled )
272+
uninstallErrorHook();
269273
Py_Finalize();
270274
mMainModule = nullptr;
271275
mMainDict = nullptr;
@@ -281,6 +285,7 @@ bool QgsPythonUtilsImpl::isEnabled()
281285
void QgsPythonUtilsImpl::installErrorHook()
282286
{
283287
runString( QStringLiteral( "qgis.utils.installErrorHook()" ) );
288+
mErrorHookInstalled = true;
284289
}
285290

286291
void QgsPythonUtilsImpl::uninstallErrorHook()

src/python/qgspythonutilsimpl.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
3636

3737
/* general purpose functions */
3838

39-
//! initialize Python and import bindings
40-
void initPython( QgisInterface *interface ) override;
39+
void initPython( QgisInterface *interface, bool installErrorHook ) override;
4140

4241
#ifdef HAVE_SERVER_PYTHON_PLUGINS
4342
//! initialize Python for server and import bindings
@@ -156,7 +155,11 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
156155
PyObject *mMainDict = nullptr;
157156

158157
//! flag determining that Python support is enabled
159-
bool mPythonEnabled;
158+
bool mPythonEnabled = false;
159+
160+
private:
161+
162+
bool mErrorHookInstalled = false;
160163
};
161164

162165
#endif

0 commit comments

Comments
 (0)