-// Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2016 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
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
//
// 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 )
+ \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()
{
int ret = simpleRun( "from Help import *", false );
if ( ret )
return ret;
- ret = simpleRun( "import salome", false );
- if (ret)
+ }
+ if ( myFirstInitStudy ) {
+ myFirstInitStudy = false;
+ int ret = simpleRun( "import salome", false );
+ if ( ret )
return ret;
ret = simpleRun( "salome.salome_init(0,1)", false );
- if (ret)
+ 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()
{
- simpleRun( "import salome", false );
- simpleRun( "salome.salome_close()", false );
+ myFirstInitStudy = false;
+ simpleRun( "import salome", false );
+ simpleRun( "salome.salome_close()", false );
}