Skip to content

Commit

Permalink
BUG: ctkAbstractPythonManager: Handle case when PythonQt is not initi…
Browse files Browse the repository at this point in the history
…alized
  • Loading branch information
jcfr committed May 21, 2016
1 parent 785369f commit 2511b3e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Expand Up @@ -28,10 +28,14 @@ class ctkAbstractPythonManagerTester: public QObject

private Q_SLOTS:

void testDefaults();

void testIsPythonInitialized();

void testSetInitializationFlags();

void testSetSystemExitExceptionHandlerEnabled();

void testPythonErrorOccured();
void testPythonErrorOccured_data();

Expand All @@ -53,6 +57,18 @@ private Q_SLOTS:
//void testPythonAttributes(); // TODO
};

// ----------------------------------------------------------------------------
void ctkAbstractPythonManagerTester::testDefaults()
{
QCOMPARE(this->PythonManager.pythonErrorOccured(), false);

this->PythonManager.resetErrorFlag();
this->PythonManager.registerPythonQtDecorator(0);
this->PythonManager.registerClassForPythonQt(0);
this->PythonManager.registerCPPClassForPythonQt(0);
this->PythonManager.addWrapperFactory(0);
}

// ----------------------------------------------------------------------------
void ctkAbstractPythonManagerTester::testIsPythonInitialized()
{
Expand Down Expand Up @@ -81,16 +97,28 @@ void ctkAbstractPythonManagerTester::testSetInitializationFlags()
QCOMPARE(this->PythonManager.isPythonInitialized(), true);
}

// ----------------------------------------------------------------------------
void ctkAbstractPythonManagerTester::testSetSystemExitExceptionHandlerEnabled()
{
QCOMPARE(this->PythonManager.systemExitExceptionHandlerEnabled(), false);
this->PythonManager.setSystemExitExceptionHandlerEnabled(true);
QCOMPARE(this->PythonManager.systemExitExceptionHandlerEnabled(), true);
}

// ----------------------------------------------------------------------------
void ctkAbstractPythonManagerTester::testInitialize()
{
QVERIFY(this->PythonManager.initialize());

this->testDefaults();
}

// ----------------------------------------------------------------------------
void ctkAbstractPythonManagerTester::testMainContext()
{
QVERIFY(this->PythonManager.mainContext());

this->testDefaults();
}

// ----------------------------------------------------------------------------
Expand All @@ -102,6 +130,11 @@ void ctkAbstractPythonManagerTester::testPythonErrorOccured()
this->PythonManager.executeString(pythonCode);

QCOMPARE(this->PythonManager.pythonErrorOccured(), errorOccured);

if(errorOccured)
{
this->PythonManager.resetErrorFlag();
}
}

// ----------------------------------------------------------------------------
Expand All @@ -123,6 +156,7 @@ void ctkAbstractPythonManagerTester::testAddObjectToPythonMain()
this->PythonManager.addObjectToPythonMain("testAddObjectToPythonMain", object);
QVariant returnValue = this->PythonManager.executeString("testAddObjectToPythonMain.happy",
ctkAbstractPythonManager::EvalInput);
this->PythonManager.resetErrorFlag();
QCOMPARE(returnValue, QVariant(true));
}

Expand All @@ -143,6 +177,7 @@ void ctkAbstractPythonManagerTester::testExecuteString()
QCOMPARE(this->PythonManager.pythonErrorOccured(), errorOccured);
if (errorOccured)
{
this->PythonManager.resetErrorFlag();
return;
}
QCOMPARE(returnValue, expectedReturnValue);
Expand Down
40 changes: 40 additions & 0 deletions Libs/Scripting/Python/Core/ctkAbstractPythonManager.cpp
Expand Up @@ -191,12 +191,22 @@ bool ctkAbstractPythonManager::isPythonInitialized()const
//-----------------------------------------------------------------------------
bool ctkAbstractPythonManager::pythonErrorOccured()const
{
if (!PythonQt::self())
{
qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
return false;
}
return PythonQt::self()->hadError();
}

//-----------------------------------------------------------------------------
void ctkAbstractPythonManager::resetErrorFlag()
{
if (!PythonQt::self())
{
qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
return;
}
PythonQt::self()->clearError();
}

Expand All @@ -219,30 +229,55 @@ void ctkAbstractPythonManager::executeInitializationScripts()
//-----------------------------------------------------------------------------
void ctkAbstractPythonManager::registerPythonQtDecorator(QObject* decorator)
{
if (!PythonQt::self())
{
qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
return;
}
PythonQt::self()->addDecorators(decorator);
}

//-----------------------------------------------------------------------------
void ctkAbstractPythonManager::registerClassForPythonQt(const QMetaObject* metaobject)
{
if (!PythonQt::self())
{
qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
return;
}
PythonQt::self()->registerClass(metaobject);
}

//-----------------------------------------------------------------------------
void ctkAbstractPythonManager::registerCPPClassForPythonQt(const char* name)
{
if (!PythonQt::self())
{
qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
return;
}
PythonQt::self()->registerCPPClass(name);
}

//-----------------------------------------------------------------------------
bool ctkAbstractPythonManager::systemExitExceptionHandlerEnabled()const
{
if (!PythonQt::self())
{
qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
return false;
}
return PythonQt::self()->systemExitExceptionHandlerEnabled();
}

//-----------------------------------------------------------------------------
void ctkAbstractPythonManager::setSystemExitExceptionHandlerEnabled(bool value)
{
if (!PythonQt::self())
{
qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
return;
}
PythonQt::self()->setSystemExitExceptionHandlerEnabled(value);
}

Expand Down Expand Up @@ -406,6 +441,11 @@ void ctkAbstractPythonManager::addObjectToPythonMain(const QString& name, QObjec
//-----------------------------------------------------------------------------
void ctkAbstractPythonManager::addWrapperFactory(PythonQtForeignWrapperFactory* factory)
{
if (!PythonQt::self())
{
qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
return;
}
PythonQt::self()->addWrapperFactory(factory);
}

Expand Down

0 comments on commit 2511b3e

Please sign in to comment.