Salome HOME
INT PAL 0052660: Plot2D Viewer: Plot2d_Curve can't be selected
[modules/gui.git] / src / PyInterp / PyInterp_Interp.cxx
index 4664009ef4654096d475ba44d56903535f55d53f..48c6a8eb5b5a2e415f0d7a90e9031ce9f788a40d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -278,6 +278,10 @@ bool PyInterp_Interp::initRun()
 
 /*!
  * Initialize context dictionaries. GIL is held already.
+ * The code executed in an embedded interpreter is expected to be run at the module
+ * level, in which case local and global context have to be the same dictionary.
+ * See: http://stackoverflow.com/questions/12265756/c-python-running-python-code-within-a-context
+ * for an explanation.
  */
 bool PyInterp_Interp::initContext()
 {
@@ -288,8 +292,11 @@ bool PyInterp_Interp::initContext()
   }
   _global_context = PyModule_GetDict(m);          // get interpreter global variable context
   Py_INCREF(_global_context);
-  _local_context = PyDict_New();
-  return true;
+  _local_context = _global_context;
+
+  int ret = PyRun_SimpleString("import salome_iapp;salome_iapp.IN_SALOME_GUI=True");
+
+  return ret == 0;
 }
 
 /*!
@@ -298,7 +305,8 @@ bool PyInterp_Interp::initContext()
 void PyInterp_Interp::closeContext()
 {
   Py_XDECREF(_global_context);
-  Py_XDECREF(_local_context);
+  // both _global and _local point to the same Python object:
+  // Py_XDECREF(_local_context);
 }
 
 /*!
@@ -407,7 +415,9 @@ static int compile_command(const char *command, PyObject * global_ctxt, PyObject
 int PyInterp_Interp::run(const char *command)
 {
   beforeRun();
-  return simpleRun(command);
+  int ret = simpleRun(command);
+  afterRun();
+  return ret;
 }
 
 /**
@@ -419,6 +429,15 @@ int PyInterp_Interp::beforeRun()
   return 0;
 }
 
+/**
+ * Called after a command is run (when calling run() method). Not thread-safe. Caller's responsability
+ * to acquire GIL if needed.
+ */
+int PyInterp_Interp::afterRun()
+{
+  return 0;
+}
+
 /*!
   \brief Run Python command (used internally). Not thread-safe. GIL acquisition is caller's responsability.
   \param command Python command