소스 검색

Merge branch 'python-manager-add-isinitialized'

* python-manager-add-isinitialized:
  Add missing test file ctkAbstractPythonManagerTest1
  ctkAbstractPythonManager - Add ExecuteStringMode to executeString()
  ctkAbstractPythonManager - Add 'const' qualifier to isPythonInitialized
Jean-Christophe Fillion-Robin 14 년 전
부모
커밋
1a62b7b3b6

+ 65 - 0
Libs/Scripting/Python/Core/Testing/Cpp/ctkAbstractPythonManagerTest1.cpp

@@ -0,0 +1,65 @@
+
+// CTK includes
+#include "ctkAbstractPythonManager.h"
+
+// PythonQt includes
+#include <PythonQt.h>
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkAbstractPythonManagerTest1(int argc, char * argv [] )
+{
+  Q_UNUSED(argc);
+  Q_UNUSED(argv);
+
+  ctkAbstractPythonManager pythonManager;
+
+  if (pythonManager.isPythonInitialized())
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with isPythonInitialized()" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  pythonManager.mainContext();
+
+  if (!pythonManager.isPythonInitialized())
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with isPythonInitialized()" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  pythonManager.executeString("a = 6542");
+  if (pythonManager.getVariable("a").toInt() != 6542)
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with executeString()" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  // Expected to return an empty value
+  QVariant result = pythonManager.executeString("6542");
+  if (!result.isNull())
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with executeString()" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  // Expected to fail
+  result = pythonManager.executeString("b = 6542", ctkAbstractPythonManager::EvalInput);
+  if (!result.isNull())
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with executeString()" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  result = pythonManager.executeString("7", ctkAbstractPythonManager::EvalInput);
+  if (result.toInt() != 7)
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with executeString()" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  return EXIT_SUCCESS;
+}

+ 13 - 3
Libs/Scripting/Python/Core/ctkAbstractPythonManager.cpp

@@ -114,7 +114,7 @@ void ctkAbstractPythonManager::initPythonQt()
 }
 
 //-----------------------------------------------------------------------------
-bool ctkAbstractPythonManager::isPythonInitialized()
+bool ctkAbstractPythonManager::isPythonInitialized()const
 {
   return PythonQt::self() != 0;
 }
@@ -154,13 +154,23 @@ void ctkAbstractPythonManager::registerCPPClassForPythonQt(const char* name)
 }
 
 //-----------------------------------------------------------------------------
-QVariant ctkAbstractPythonManager::executeString(const QString& code)
+QVariant ctkAbstractPythonManager::executeString(const QString& code,
+                                                 ctkAbstractPythonManager::ExecuteStringMode mode)
 {
+  int start = -1;
+  switch(mode)
+    {
+    case ctkAbstractPythonManager::FileInput: start = Py_file_input; break;
+    case ctkAbstractPythonManager::SingleInput: start = Py_single_input; break;
+    case ctkAbstractPythonManager::EvalInput:
+    default: start = Py_eval_input; break;
+    }
+
   QVariant ret;
   PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
   if (main)
     {
-    ret = main.evalScript(code, Py_file_input);
+    ret = main.evalScript(code, start);
     }
   return ret;
 }

+ 13 - 2
Libs/Scripting/Python/Core/ctkAbstractPythonManager.h

@@ -46,9 +46,20 @@ public:
   void registerClassForPythonQt(const QMetaObject* metaobject);
   void registerCPPClassForPythonQt(const char* name);
 
+  /// This enum maps to Py_eval_input, Py_file_input and Py_single_input
+  /// \see http://docs.python.org/c-api/veryhigh.html#Py_eval_input
+  /// \see http://docs.python.org/c-api/veryhigh.html#Py_file_input
+  /// \see http://docs.python.org/c-api/veryhigh.html#Py_single_input
+  enum ExecuteStringMode
+    {
+    EvalInput = 0,
+    FileInput,
+    SingleInput
+    };
+
   /// Execute a python of python code (can be multiple lines separated with newline)
   /// and return the result as a QVariant.
-  QVariant executeString(const QString& code);
+  QVariant executeString(const QString& code, ExecuteStringMode mode = FileInput);
 
   /// Gets the value of the variable looking in the __main__ module.
   /// If the variable is not found returns a default initialized QVariant.
@@ -71,7 +82,7 @@ public:
 
   /// Returns True if python is initialized
   /// \sa pythonInitialized
-  bool isPythonInitialized();
+  bool isPythonInitialized()const;
 
 signals: