Back to index

salome-gui  6.5.0
Public Member Functions | Protected Member Functions | Private Attributes
SalomeApp_PyInterp Class Reference

EDF-CCAR When SALOME uses multi Python interpreter feature, Every study has its own interpreter and thread state (_tstate = Py_NewInterpreter()) This is fine because every study has its own modules (sys.modules) stdout and stderr BUT some Python modules must be imported only once. More...

#include <SalomeApp_PyInterp.h>

Inheritance diagram for SalomeApp_PyInterp:
Inheritance graph
[legend]
Collaboration diagram for SalomeApp_PyInterp:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 SalomeApp_PyInterp ()
 constructor : multi Python interpreter, one per SALOME study.
virtual ~SalomeApp_PyInterp ()
 Destructor.
virtual void initPython ()
 Do nothing The initialization has been done in main.

Protected Member Functions

virtual bool initContext ()
virtual int beforeRun ()
 Called before each Python command running.

Private Attributes

bool myFirstRun

Detailed Description

EDF-CCAR When SALOME uses multi Python interpreter feature, Every study has its own interpreter and thread state (_tstate = Py_NewInterpreter()) This is fine because every study has its own modules (sys.modules) stdout and stderr BUT some Python modules must be imported only once.

In multi interpreter context Python modules (*.py) are imported several times. The pyqt module must be imported only once because it registers classes in a C module. It's quite the same with omniorb modules (internals and generated with omniidl) This problem is handled with "shared modules" defined in salome_shared_modules.py These "shared modules" are imported only once and only copied in all the other interpreters BUT it's not the only problem. Every interpreter has its own builtin module. That's fine but if we have copied some modules and imported others problems may arise with operations that are not allowed in restricted execution environment. So we must impose that all interpreters have identical builtin module. That's all, for the moment ...

Definition at line 32 of file SalomeApp_PyInterp.h.


Constructor & Destructor Documentation

constructor : multi Python interpreter, one per SALOME study.

calls initialize method defined in base class, which calls virtual methods initstate & initcontext redefined here.

Definition at line 39 of file SalomeApp_PyInterp.cxx.

                                      : 
  PyConsole_Interp(), myFirstRun( true )
{
}

Destructor.

Definition at line 47 of file SalomeApp_PyInterp.cxx.

{
}

Member Function Documentation

int SalomeApp_PyInterp::beforeRun ( ) [protected, virtual]

Called before each Python command running.

Definition at line 120 of file SalomeApp_PyInterp.cxx.

{
  if ( myFirstRun ) {
    myFirstRun = false;
    int ret = simpleRun( "from Help import *", false );
    if ( ret )
      return ret;
    ret = simpleRun( "import salome", false );
    if (ret)
      return ret;
    ret = simpleRun( "salome.salome_init(0,1)", false );
    if (ret)
      return ret;
  }
  return true;
}
bool SalomeApp_PyInterp::initContext ( ) [protected, virtual]

The GIL is assumed to be held It is the caller responsability caller to acquire the GIL It will still be held on initContext output

Definition at line 70 of file SalomeApp_PyInterp.cxx.

{
  if ( !PyConsole_Interp::initContext() )
    return false;

  // Import special module to change the import mechanism
  PyObjWrapper m1( PyImport_ImportModule( "import_hook" ) );
  if ( !m1 )
  {
    MESSAGE( "initContext: problem with import_hook import" );
    PyErr_Print();
    ASSERT( 0 );
    return false;
  }

  // Call init_shared_modules to initialize the shared import mechanism for modules 
  //that must not be imported twice
  PyObjWrapper m2( PyObject_CallMethod( m1, (char*)"init_shared_modules", (char*)"O", KERNEL_PYTHON::salome_shared_modules_module ) );
  if ( !m2 )
  {
    MESSAGE( "initContext: problem with init_shared_modules call" );
    PyErr_Print();
    ASSERT( 0 );
    return false;
  }

  return true;
}

Do nothing The initialization has been done in main.

Definition at line 108 of file SalomeApp_PyInterp.cxx.

{
  MESSAGE("SalomeApp_PyInterp::initPython");
  ASSERT(KERNEL_PYTHON::_gtstate); // initialisation in main
  SCRUTE(KERNEL_PYTHON::_gtstate);
  _gtstate=KERNEL_PYTHON::_gtstate;
  _interp=KERNEL_PYTHON::_interp;
}

Member Data Documentation

Definition at line 45 of file SalomeApp_PyInterp.h.


The documentation for this class was generated from the following files: