//
// 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)!
-
/*!
- * 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()
+ : myFirstRun( true ), myFirstInitStudy( false )
{
}
/*!
- * 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 ( 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)
+ if ( ret )
return ret;
}
- return PyConsole_EnhInterp::beforeRun();
+ 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;