Version: 6.3.1
Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes

PyConsole_Interp Class Reference

!!! WARNING !!! THIS INCLUDE MUST BE VERY FIRST !!! More...

#include <PyConsole_Interp.h>

Inheritance diagram for PyConsole_Interp:
Inheritance graph

Public Member Functions

 PyConsole_Interp ()
 Constructor.
 ~PyConsole_Interp ()
 Destructor.
void initialize ()
 Initialize embedded interpreter.
virtual int run (const char *command)
 Run Python command.
PyLockWrapper GetLockWrapper ()
 Get Python GIL wrapper.
std::string getbanner ()
 Get embedded Python interpreter banner.
void setverrcb (PyOutChanged *, void *)
 Set Python standard error device hook.
void setvoutcb (PyOutChanged *, void *)
 Set Python standard output device hook.
const char * getPrevious ()
 Get previous command in the commands history.
const char * getNext ()
 Get next command in the commands history.

Static Public Attributes

static int _argc = 1
static char * _argv [] = {(char*)""}
static PyObject * builtinmodule = NULL
static PyThreadState * _gtstate = NULL
static PyInterpreterState * _interp = NULL

Protected Member Functions

virtual bool initState ()
 Initialize internal Python interpreter state.
virtual bool initContext ()
 Initialize python interpeter context.
virtual int beforeRun ()
int simpleRun (const char *command, const bool addToHistory=true)
 Run Python command (used internally).
virtual bool initRun ()
 Initialize run command.
virtual void initPython ()
 Initialize Python interpreter.

Protected Attributes

PyThreadState * _tstate
PyObject * _vout
PyObject * _verr
PyObject * _g
PyObject * _codeop
std::list< std::string > _history
std::list< std::string >::iterator _ith

Detailed Description

Python interpreter to be embedded to the SALOME study's GUI.

Python interpreter is created one per SALOME study.

Call initialize method defined in the base class PyInterp_Interp, to intialize interpreter after instance creation.

The method initialize() calls virtuals methods

/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. For example, 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.


Constructor & Destructor Documentation

PyConsole_Interp::PyConsole_Interp ( )

Creates new python interpreter.

PyConsole_Interp::~PyConsole_Interp ( )

Does nothing for the moment.


Member Function Documentation

bool PyConsole_Interp::initState ( ) [protected, virtual]

When calling initState the GIL is not held It must not be held on exit

Returns:
true on success

Implements PyInterp_Interp.

References PyInterp_Interp._argc, PyInterp_Interp._argv, PyInterp_Interp._interp, PyInterp_Interp._tstate, and PyInterp_Interp.builtinmodule.

bool PyConsole_Interp::initContext ( ) [protected, virtual]

The GIL is assumed to be held. It is the caller responsability to acquire the GIL. It must still be held on initContext() exit.

Returns:
true on success

Implements PyInterp_Interp.

Reimplemented in LightApp_PyInterp, and SalomeApp_PyInterp.

References PyInterp_Interp._g, and PyInterp_Interp.builtinmodule.

void PyInterp_Interp::initialize ( ) [inherited]

This method shoud be called after construction of the interpreter. The method initialize() calls virtuals methods

  • initPython() to initialize global Python interpreter
  • initState() to initialize embedded interpreter state
  • initContext() to initialize interpreter internal context
  • initRun() to prepare interpreter for running commands which should be implemented in the successor classes, according to the embedded Python interpreter policy (mono or multi interpreter, etc).

References PyInterp_Interp._history, PyInterp_Interp._ith, PyInterp_Interp._tstate, PyInterp_Interp._verr, PyInterp_Interp._vout, PyInterp_Interp.initContext(), PyInterp_Interp.initPython(), PyInterp_Interp.initRun(), PyInterp_Interp.initState(), and newPyStdOut().

int PyInterp_Interp::run ( const char *  command) [virtual, inherited]
Parameters:
commandPython command
Returns:
command status

Reimplemented in SALOME_PYQT_PyInterp.

References PyInterp_Interp.beforeRun(), and PyInterp_Interp.simpleRun().

PyLockWrapper PyInterp_Interp::GetLockWrapper ( ) [inherited]
Returns:
GIL lock wrapper (GIL is automatically acquired here)

References PyInterp_Interp._interp, PyInterp_Interp._tstate, and currentThreadMap.

std::string PyInterp_Interp::getbanner ( ) [inherited]
Returns:
banner string
void PyInterp_Interp::setverrcb ( PyOutChanged cb,
void *  data 
) [inherited]
Parameters:
cbcallback function
datacallback function parameters

References PyInterp_Interp._verr.

void PyInterp_Interp::setvoutcb ( PyOutChanged cb,
void *  data 
) [inherited]
Parameters:
cbcallback function
datacallback function parameters

References PyInterp_Interp._vout.

const char * PyInterp_Interp::getPrevious ( ) [inherited]
Returns:
previous command

References PyInterp_Interp._history, PyInterp_Interp._ith, and BEGIN_HISTORY_PY.

const char * PyInterp_Interp::getNext ( ) [inherited]
Returns:
next command

References PyInterp_Interp._history, PyInterp_Interp._ith, and TOP_HISTORY_PY.

virtual int PyInterp_Interp.beforeRun ( ) [protected, virtual, inherited]

Reimplemented in SalomeApp_PyInterp.

int PyInterp_Interp::simpleRun ( const char *  command,
const bool  addToHistory = true 
) [protected, inherited]
Parameters:
commandPython command
addToHistoryif true (default), the command is added to the commands history
Returns:
command status

References PyInterp_Interp._g, PyInterp_Interp._history, PyInterp_Interp._ith, PyInterp_Interp._verr, PyInterp_Interp._vout, and compile_command().

bool PyInterp_Interp::initRun ( ) [protected, virtual, inherited]

This method is used to prepare interpreter for running Python commands.

Returns:
true on success and false on error
void PyInterp_Interp::initPython ( ) [protected, virtual, inherited]

In case if Python is not initialized, it sets program name, initializes the interpreter, sets program arguments, initializes threads. Otherwise, it just obtains the global interpreter and thread states. This is important for light SALOME configuration, as in full SALOME this is done at SalomeApp level.

See also:
SalomeApp_PyInterp class

Reimplemented in LightApp_PyInterp, SALOME_PYQT_PyInterp, and SalomeApp_PyInterp.

References PyInterp_Interp._argc, PyInterp_Interp._argv, PyInterp_Interp._gtstate, and PyInterp_Interp._interp.


Field Documentation

int PyInterp_Interp::_argc = 1 [static, inherited]
char * PyInterp_Interp::_argv = {(char*)""} [static, inherited]
PyObject * PyInterp_Interp::builtinmodule = NULL [static, inherited]
PyThreadState * PyInterp_Interp::_gtstate = NULL [static, inherited]
PyInterpreterState * PyInterp_Interp::_interp = NULL [static, inherited]
PyThreadState* PyInterp_Interp._tstate [protected, inherited]
PyObject* PyInterp_Interp._vout [protected, inherited]
PyObject* PyInterp_Interp._verr [protected, inherited]
PyObject* PyInterp_Interp._g [protected, inherited]
PyObject* PyInterp_Interp._codeop [protected, inherited]
std::list<std::string> PyInterp_Interp._history [protected, inherited]
std::list<std::string>::iterator PyInterp_Interp._ith [protected, inherited]
Copyright © 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
Copyright © 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS