Explorar o código

ctkAbstractPythonManager - Add ExecuteStringMode to executeString()

Jean-Christophe Fillion-Robin %!s(int64=14) %!d(string=hai) anos
pai
achega
801220660b

+ 12 - 2
Libs/Scripting/Python/Core/ctkAbstractPythonManager.cpp

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

+ 12 - 1
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.