Salome HOME
updated copyright message
[modules/gui.git] / src / SalomeApp / SalomeApp_PyInterp.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 1107c03..c25920a
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
-
-//  SALOME SALOMEGUI : implementation of desktop and GUI kernel
 //  File   : SalomeApp_PyInterp.cxx
 //  Author : Nicolas REJNERI
 
 #include "SalomeApp_PyInterp.h"
-
-#include <utilities.h>
-#include <Container_init_python.hxx>
-
-#include "PyInterp.h" // this include must be first (see PyInterp_base.h)!
+#include "SUIT_ResourceMgr.h"
 
 /*!
- * constructor : multi Python interpreter, one per SALOME study.
- * calls initialize method defined in base class, which calls virtual methods
- * initstate & initcontext redefined here.
- */
-SalomeApp_PyInterp::SalomeApp_PyInterp(): 
-  PyConsole_EnhInterp(), myFirstRun( true ), myFirstInitStudy( false )
+  \brief Constructor
+*/
+SalomeApp_PyInterp::SalomeApp_PyInterp( SUIT_ResourceMgr* resMgr )
+  : myFirstRun( true ), myFirstInitStudy( false ), myResourceMgr( resMgr )
 {
 }
 
 /*!
* Destructor.
- */
 \brief Destructor.
+*/
 SalomeApp_PyInterp::~SalomeApp_PyInterp()
 {
 }
  
 /*!
-  Do nothing (we could rely on the test done in the implementation of this method in the super
-  class PyInterp_Interp, but in this context we are sure the initialization has been done in main()
-  of SALOME_Session_Server)
+ * 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.
  */
-void SalomeApp_PyInterp::initPython()
+bool SalomeApp_PyInterp::initContext()
 {
-  MESSAGE("SalomeApp_PyInterp::initPython - does nothing");
+  bool ok = PyConsole_Interp::initContext();
+  if ( ok ) {
+    int ret = PyRun_SimpleString( "import salome_iapp; salome_iapp.IN_SALOME_GUI = True" );
+    ok = ok && (ret == 0);
+  }
+  return ok;
 }
 
 /*!
-  Called before each Python command running.
+  \brief Called before each Python command running.
 */
 int SalomeApp_PyInterp::beforeRun()
 {
   if ( myFirstRun ) {
     myFirstRun = false;
+    QStringList parameters = myResourceMgr->parameters( "pythonpath" );
+    foreach ( QString parameter, parameters ) {
+      QStringList paths = myResourceMgr->stringValue( "pythonpath", parameter ).split( ";;" );
+      foreach( QString path, paths )
+        simpleRun( QString( "import sys; sys.path.append('%1')" ).arg( path ).toUtf8().constData(), false );
+    }
     int ret = simpleRun( "from Help import *", false );
     if ( ret )
       return ret;
   }
-  if( myFirstInitStudy ) {
+  if ( myFirstInitStudy ) {
     myFirstInitStudy = false;
     int ret = simpleRun( "import salome", false );
-    if (ret)
+    if ( ret )
       return ret;
-    ret = simpleRun( "salome.salome_init(0,1)", false );
-    if (ret)
+    ret = simpleRun( "salome.salome_init(embedded=True)", false );
+    if ( ret )
       return ret;
   }
-  return true;
+  return PyConsole_Interp::beforeRun();
 }
 
+/*!
+  \brief Called when study is initialized
+ */
 void SalomeApp_PyInterp::initStudy()
 {
   myFirstInitStudy = true;
 }
 
+/*!
+  \brief Called when study is closed
+*/
 void SalomeApp_PyInterp::closeContext()
 {
   myFirstInitStudy = false;