Преглед на файлове

BUG: ctkAbstractPythonManager: Handle case when PythonQt is not initialized

Jean-Christophe Fillion-Robin преди 9 години
родител
ревизия
2511b3efc0

+ 35 - 0
Libs/Scripting/Python/Core/Testing/Cpp/ctkAbstractPythonManagerTest.cpp

@@ -28,10 +28,14 @@ private:
 
 private Q_SLOTS:
 
+  void testDefaults();
+
   void testIsPythonInitialized();
 
   void testSetInitializationFlags();
 
+  void testSetSystemExitExceptionHandlerEnabled();
+
   void testPythonErrorOccured();
   void testPythonErrorOccured_data();
 
@@ -54,6 +58,18 @@ private Q_SLOTS:
 };
 
 // ----------------------------------------------------------------------------
+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()
 {
   QCOMPARE(this->PythonManager.isPythonInitialized(), false);
@@ -82,15 +98,27 @@ void ctkAbstractPythonManagerTester::testSetInitializationFlags()
 }
 
 // ----------------------------------------------------------------------------
+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();
 }
 
 // ----------------------------------------------------------------------------
@@ -102,6 +130,11 @@ void ctkAbstractPythonManagerTester::testPythonErrorOccured()
   this->PythonManager.executeString(pythonCode);
 
   QCOMPARE(this->PythonManager.pythonErrorOccured(), errorOccured);
+
+  if(errorOccured)
+    {
+    this->PythonManager.resetErrorFlag();
+    }
 }
 
 // ----------------------------------------------------------------------------
@@ -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));
 }
 
@@ -143,6 +177,7 @@ void ctkAbstractPythonManagerTester::testExecuteString()
   QCOMPARE(this->PythonManager.pythonErrorOccured(), errorOccured);
   if (errorOccured)
     {
+    this->PythonManager.resetErrorFlag();
     return;
     }
   QCOMPARE(returnValue, expectedReturnValue);

+ 40 - 0
Libs/Scripting/Python/Core/ctkAbstractPythonManager.cpp

@@ -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();
 }
 
@@ -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);
 }
 
@@ -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);
 }