Skip to content
Permalink
Browse files

Allow python to be initialised without interface or error hook

  • Loading branch information
nyalldawson committed Feb 28, 2019
1 parent 28df1b4 commit a079b66e3547e36bfba68d4e1f8fb822a123ec38
Showing with 29 additions and 14 deletions.
  1. +1 −1 src/app/qgisapp.cpp
  2. +9 −2 src/python/qgspythonutils.h
  3. +13 −8 src/python/qgspythonutilsimpl.cpp
  4. +6 −3 src/python/qgspythonutilsimpl.h
@@ -10511,7 +10511,7 @@ void QgisApp::loadPythonSupport()
mPythonUtils = pythonlib_inst();
if ( mPythonUtils )
{
mPythonUtils->initPython( mQgisInterface );
mPythonUtils->initPython( mQgisInterface, true );
}

if ( mPythonUtils && mPythonUtils->isEnabled() )
@@ -50,8 +50,15 @@ class PYTHON_EXPORT QgsPythonUtils
//! returns TRUE if Python support is ready to use (must be inited first)
virtual bool isEnabled() = 0;

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

#ifdef HAVE_SERVER_PYTHON_PLUGINS
//! initialize Python and import server bindings
@@ -39,9 +39,6 @@ PyThreadState *_mainState = nullptr;

QgsPythonUtilsImpl::QgsPythonUtilsImpl()
{
mMainModule = nullptr;
mMainDict = nullptr;
mPythonEnabled = false;
}

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

void QgsPythonUtilsImpl::initPython( QgisInterface *interface )
void QgsPythonUtilsImpl::initPython( QgisInterface *interface, const bool installErrorHook )
{
init();
if ( !checkSystemImports() )
{
exitPython();
return;
}
// initialize 'iface' object
runString( "qgis.utils.initInterface(" + QString::number( ( quint64 ) interface ) + ')' );

if ( interface )
{
// initialize 'iface' object
runString( "qgis.utils.initInterface(" + QString::number( ( quint64 ) interface ) + ')' );
}

if ( !checkQgisUser() )
{
exitPython();
return;
}
doCustomImports();
installErrorHook();
if ( installErrorHook )
QgsPythonUtilsImpl::installErrorHook();
finish();
}

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

void QgsPythonUtilsImpl::exitPython()
{
uninstallErrorHook();
if ( mErrorHookInstalled )
uninstallErrorHook();
Py_Finalize();
mMainModule = nullptr;
mMainDict = nullptr;
@@ -281,6 +285,7 @@ bool QgsPythonUtilsImpl::isEnabled()
void QgsPythonUtilsImpl::installErrorHook()
{
runString( QStringLiteral( "qgis.utils.installErrorHook()" ) );
mErrorHookInstalled = true;
}

void QgsPythonUtilsImpl::uninstallErrorHook()
@@ -36,8 +36,7 @@ class QgsPythonUtilsImpl : public QgsPythonUtils

/* general purpose functions */

//! initialize Python and import bindings
void initPython( QgisInterface *interface ) override;
void initPython( QgisInterface *interface, bool installErrorHook ) override;

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

//! flag determining that Python support is enabled
bool mPythonEnabled;
bool mPythonEnabled = false;

private:

bool mErrorHookInstalled = false;
};

#endif

0 comments on commit a079b66

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