const char PythonNode::IMPL_NAME[]="Python";
const char PythonNode::KIND[]="Python";
+const char PythonNode::SCRIPT_FOR_SERIALIZATION[]="import cPickle\n"
+ "def pickleForDistPyth2009(kws):\n"
+ " return cPickle.dumps(((),kws),-1)\n"
+ "\n"
+ "def unPickleForDistPyth2009(st):\n"
+ " args=cPickle.loads(st)\n"
+ " return args\n";
+
+const char PyFuncNode::SCRIPT_FOR_SERIALIZATION[]="import cPickle\n"
+ "def pickleForDistPyth2009(*args,**kws):\n"
+ " return cPickle.dumps((args,kws),-1)\n"
+ "\n"
+ "def unPickleForDistPyth2009(st):\n"
+ " args=cPickle.loads(st)\n"
+ " return args\n";
+
+PythonEntry::PythonEntry():_context(0),_pyfuncSer(0),_pyfuncUnser(0)
+{
+}
+
+PythonEntry::~PythonEntry()
+{
+ AutoGIL agil;
+ DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
+ Py_XDECREF(_pyfuncUnser);
+ Py_XDECREF(_pyfuncSer);
+ Py_XDECREF(_context);
+}
+
+void PythonEntry::commonRemoteLoad(InlineNode *reqNode)
+{
+ DEBTRACE( "---------------PythonEntry::CommonRemoteLoad function---------------" );
+ Container *container(reqNode->getContainer());
+ bool isContAlreadyStarted(false);
+ if(container)
+ {
+ isContAlreadyStarted=container->isAlreadyStarted(reqNode);
+ if(!isContAlreadyStarted)
+ {
+ try
+ {
+ container->start(reqNode);
+ }
+ catch(Exception& e)
+ {
+ reqNode->setErrorDetails(e.what());
+ throw e;
+ }
+ }
+ }
+ else
+ {
+ std::string what("PythonEntry::CommonRemoteLoad : a load operation requested on \"");
+ what+=reqNode->getName(); what+="\" with no container specified.";
+ reqNode->setErrorDetails(what);
+ throw Exception(what);
+ }
+ Engines::Container_var objContainer=Engines::Container::_nil();
+ if(!container)
+ throw Exception("No container specified !");
+ SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(container));
+ SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(container));
+ if(containerCast0)
+ objContainer=containerCast0->getContainerPtr(reqNode);
+ else if(containerCast1)
+ {
+ YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,reqNode));
+ objContainer=tmpCont->getContainerPtr(reqNode);
+ }
+ else
+ throw Exception("Unrecognized type of container ! Salome one is expected for PythonNode/PyFuncNode !");
+ if(CORBA::is_nil(objContainer))
+ throw Exception("Container corba pointer is NULL for PythonNode !");
+ bool isInitializeRequested(false);
+ try
+ {
+ if(containerCast0)
+ {
+ createRemoteAdaptedPyInterpretor(objContainer);
+ }
+ else
+ {
+ Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
+ if(CORBA::is_nil(dftPyScript))
+ {
+ isInitializeRequested=true;
+ createRemoteAdaptedPyInterpretor(objContainer);
+ }
+ else
+ assignRemotePyInterpretor(dftPyScript);
+ }
+ }
+ catch( const SALOME::SALOME_Exception& ex )
+ {
+ std::string msg="Exception on remote python node creation ";
+ msg += '\n';
+ msg += ex.details.text.in();
+ reqNode->setErrorDetails(msg);
+ throw Exception(msg);
+ }
+ Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
+ if(CORBA::is_nil(pynode))
+ throw Exception("In PythonNode the ref in NULL ! ");
+ ///
+ {
+ AutoGIL agil;
+ const char *picklizeScript(getSerializationScript());
+ PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
+ if(res == NULL)
+ {
+ std::string errorDetails;
+ PyObject* new_stderr = newPyStdOut(errorDetails);
+ reqNode->setErrorDetails(errorDetails);
+ PySys_SetObject((char*)"stderr", new_stderr);
+ PyErr_Print();
+ PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+ Py_DECREF(new_stderr);
+ throw Exception("Error during load");
+ }
+ Py_DECREF(res);
+ _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
+ _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
+ if(_pyfuncSer == NULL)
+ {
+ std::string errorDetails;
+ PyObject *new_stderr(newPyStdOut(errorDetails));
+ reqNode->setErrorDetails(errorDetails);
+ PySys_SetObject((char*)"stderr", new_stderr);
+ PyErr_Print();
+ PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+ Py_DECREF(new_stderr);
+ throw Exception("Error during load");
+ }
+ if(_pyfuncUnser == NULL)
+ {
+ std::string errorDetails;
+ PyObject *new_stderr(newPyStdOut(errorDetails));
+ reqNode->setErrorDetails(errorDetails);
+ PySys_SetObject((char*)"stderr", new_stderr);
+ PyErr_Print();
+ PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+ Py_DECREF(new_stderr);
+ throw Exception("Error during load");
+ }
+ if(isInitializeRequested)
+ {//This one is called only once at initialization in the container if an init-script is specified.
+ try
+ {
+ std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
+ if(!zeInitScriptKey.empty())
+ pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
+ }
+ catch( const SALOME::SALOME_Exception& ex )
+ {
+ std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
+ msg += '\n';
+ msg += ex.details.text.in();
+ reqNode->setErrorDetails(msg);
+ throw Exception(msg);
+ }
+ }
+ DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
+ }
+}
+
PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father)
{
_implementation=IMPL_NAME;
PythonNode::~PythonNode()
{
- AutoGIL agil;
- DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
- Py_DECREF(_context);
+ if(!CORBA::is_nil(_pynode))
+ {
+ _pynode->UnRegister();
+ }
}
void PythonNode::checkBasicConsistency() const throw(YACS::Exception)
void PythonNode::loadRemote()
{
- DEBTRACE( "---------------PyNode::loadRemote function---------------" );
- bool isContAlreadyStarted(false);
- if(_container)
- {
- isContAlreadyStarted=_container->isAlreadyStarted(this);
- if(!isContAlreadyStarted)
- {
- try
- {
- _container->start(this);
- }
- catch(Exception& e)
- {
- _errorDetails=e.what();
- throw e;
- }
- }
- }
- else
- {
- std::string what("PyNode::loadRemote : a load operation requested on \"");
- what+=_name; what+="\" with no container specified.";
- _errorDetails=what;
- throw Exception(what);
- }
-
-
- Engines::Container_var objContainer=Engines::Container::_nil();
- if(!_container)
- throw Exception("No container specified !");
- SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(_container));
- SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(_container));
- if(containerCast0)
- objContainer=containerCast0->getContainerPtr(this);
- else if(containerCast1)
- {
- YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,this));
- objContainer=tmpCont->getContainerPtr(this);
- }
- else
- throw Exception("Unrecognized type of container ! Salome one is expected for PythonNode !");
- if(CORBA::is_nil(objContainer))
- throw Exception("Container corba pointer is NULL for PythonNode !");
- bool isInitializeRequested(false);
- try
- {
- if(containerCast0)
- {
- _pynode = objContainer->createPyScriptNode(getName().c_str(),getScript().c_str());
- }
- else
- {
- Engines::PyScriptNode_var dftPyScript(objContainer->getDefaultPyScriptNode());
- if(CORBA::is_nil(dftPyScript))
- {
- isInitializeRequested=true;
- _pynode = objContainer->createPyScriptNode(getName().c_str(),getScript().c_str());
- }
- else
- _pynode = dftPyScript;
- }
- }
- catch( const SALOME::SALOME_Exception& ex )
- {
- std::string msg="Exception on remote python node creation ";
- msg += '\n';
- msg += ex.details.text.in();
- _errorDetails=msg;
- throw Exception(msg);
- }
-
- if(CORBA::is_nil(_pynode))
- throw Exception("In PythonNode the ref in NULL ! ");
-
- ///
- {
- AutoGIL agil;
- const char picklizeScript[]="import cPickle\n"
- "def pickleForDistPyth2009(kws):\n"
- " return cPickle.dumps(((),kws),-1)\n"
- "\n"
- "def unPickleForDistPyth2009(st):\n"
- " args=cPickle.loads(st)\n"
- " return args\n";
- PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
- if(res == NULL)
- {
- _errorDetails="";
- PyObject* new_stderr = newPyStdOut(_errorDetails);
- PySys_SetObject((char*)"stderr", new_stderr);
- PyErr_Print();
- PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
- Py_DECREF(new_stderr);
- throw Exception("Error during load");
- }
- Py_DECREF(res);
-
- _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
- _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
- if(_pyfuncSer == NULL)
- {
- _errorDetails="";
- PyObject* new_stderr = newPyStdOut(_errorDetails);
- PySys_SetObject((char*)"stderr", new_stderr);
- PyErr_Print();
- PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
- Py_DECREF(new_stderr);
- throw Exception("Error during load");
- }
- if(_pyfuncUnser == NULL)
- {
- _errorDetails="";
- PyObject* new_stderr = newPyStdOut(_errorDetails);
- PySys_SetObject((char*)"stderr", new_stderr);
- PyErr_Print();
- PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
- Py_DECREF(new_stderr);
- throw Exception("Error during load");
- }
- if(isInitializeRequested)
- {//This one is called only once at initialization in the container if an init-script is specified.
- try
- {
- std::string zeInitScriptKey(_container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
- if(!zeInitScriptKey.empty())
- _pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
- }
- catch( const SALOME::SALOME_Exception& ex )
- {
- std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
- msg += '\n';
- msg += ex.details.text.in();
- _errorDetails=msg;
- throw Exception(msg);
- }
- }
- DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
- }
+ commonRemoteLoad(this);
}
void PythonNode::execute()
return new PythonNode(*this,father);
}
+void PythonNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
+{
+ if(!CORBA::is_nil(_pynode))
+ _pynode->UnRegister();
+ _pynode=objContainer->createPyScriptNode(getName().c_str(),getScript().c_str());
+}
+
+Engines::PyNodeBase_var PythonNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
+{
+ Engines::PyScriptNode_var ret(objContainer->getDefaultPyScriptNode());
+ if(!CORBA::is_nil(ret))
+ {
+ ret->Register();
+ }
+ return Engines::PyNodeBase::_narrow(ret);
+}
+
+void PythonNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
+{
+ _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
+}
+
+Engines::PyNodeBase_var PythonNode::getRemoteInterpreterHandle()
+{
+ return Engines::PyNodeBase::_narrow(_pynode);
+}
+
//! Create a new node of same type with a given name
PythonNode* PythonNode::cloneNode(const std::string& name)
{
PyFuncNode::~PyFuncNode()
{
- DEBTRACE( getName() );
- {
- AutoGIL agil;
- DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
- if(_pyfunc)DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
- Py_DECREF(_context);
- }
if(!CORBA::is_nil(_pynode))
{
_pynode->UnRegister();
void PyFuncNode::loadRemote()
{
- DEBTRACE( "---------------PyfuncNode::loadRemote function---------------" );
- bool isContAlreadyStarted(false);
- if(_container)
- {
- isContAlreadyStarted=_container->isAlreadyStarted(this);
- if(!isContAlreadyStarted)
- {
- try
- {
- _container->start(this);
- }
- catch(Exception& e)
- {
- _errorDetails=e.what();
- throw e;
- }
- }
- }
- else
- {
- std::string what("PyFuncNode::loadRemote : a load operation requested on \"");
- what+=_name; what+="\" with no container specified.";
- _errorDetails=what;
- throw Exception(what);
- }
-
- Engines::Container_var objContainer=Engines::Container::_nil();
- if(!_container)
- throw Exception("No container specified !");
- SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(_container));
- SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(_container));
- if(containerCast0)
- objContainer=containerCast0->getContainerPtr(this);
- else if(containerCast1)
- {
- YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,this));
- objContainer=tmpCont->getContainerPtr(this);
- }
- else
- throw Exception("Unrecognized type of container ! Salome one is expected ! In PythonNode !");
- if(CORBA::is_nil(objContainer))
- throw Exception("Container corba pointer is NULL ! In PythonNode !");
- bool isInitializeRequested(false);
- try
- {
- if(containerCast0)
- {
- _pynode = objContainer->createPyNode(getName().c_str(),getScript().c_str());
- }
- else
- {
- Engines::PyNode_var dftPyScript(objContainer->getDefaultPyNode());
- if(CORBA::is_nil(dftPyScript))
- {
- isInitializeRequested=true;
- _pynode = objContainer->createPyNode(getName().c_str(),getScript().c_str());
- }
- else
- _pynode = dftPyScript;
- }
- }
- catch( const SALOME::SALOME_Exception& ex )
- {
- std::string msg="Exception on remote python node creation ";
- msg += '\n';
- msg += ex.details.text.in();
- _errorDetails=msg;
- throw Exception(msg);
- }
-
- if(CORBA::is_nil(_pynode))
- throw Exception("In PyFuncNode the ref in NULL ! ");
-
- {
- AutoGIL agil;
- const char picklizeScript[]="import cPickle\n"
- "def pickleForDistPyth2009(*args,**kws):\n"
- " return cPickle.dumps((args,kws),-1)\n"
- "\n"
- "def unPickleForDistPyth2009(st):\n"
- " args=cPickle.loads(st)\n"
- " return args\n";
- PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
- if(res == NULL)
- {
- _errorDetails="";
- PyObject* new_stderr = newPyStdOut(_errorDetails);
- PySys_SetObject((char*)"stderr", new_stderr);
- PyErr_Print();
- PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
- Py_DECREF(new_stderr);
- throw Exception("Error during load");
- }
- Py_DECREF(res);
-
- _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
- _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
- if(_pyfuncSer == NULL)
- {
- _errorDetails="";
- PyObject* new_stderr = newPyStdOut(_errorDetails);
- PySys_SetObject((char*)"stderr", new_stderr);
- PyErr_Print();
- PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
- Py_DECREF(new_stderr);
- throw Exception("Error during load");
- }
- if(_pyfuncUnser == NULL)
- {
- _errorDetails="";
- PyObject* new_stderr = newPyStdOut(_errorDetails);
- PySys_SetObject((char*)"stderr", new_stderr);
- PyErr_Print();
- PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
- Py_DECREF(new_stderr);
- throw Exception("Error during load");
- }
- if(isInitializeRequested)
- {//This one is called only once at initialization in the container if an init-script is specified.
- try
- {
- std::string zeInitScriptKey(_container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
- if(!zeInitScriptKey.empty())
- _pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
- }
- catch( const SALOME::SALOME_Exception& ex )
- {
- std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
- msg += '\n';
- msg += ex.details.text.in();
- _errorDetails=msg;
- throw Exception(msg);
- }
- }
- DEBTRACE( "---------------End PyfuncNode::loadRemote function---------------" );
- }
+ commonRemoteLoad(this);
}
void PyFuncNode::loadLocal()
return new PyFuncNode(*this,father);
}
+void PyFuncNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
+{
+ if(!CORBA::is_nil(_pynode))
+ _pynode->UnRegister();
+ _pynode=objContainer->createPyNode(getName().c_str(),getScript().c_str());
+}
+
+Engines::PyNodeBase_var PyFuncNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
+{
+ Engines::PyNode_var ret(objContainer->getDefaultPyNode());
+ if(!CORBA::is_nil(ret))
+ {
+ ret->Register();
+ }
+ return Engines::PyNodeBase::_narrow(ret);
+}
+
+void PyFuncNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
+{
+ _pynode=Engines::PyNode::_narrow(remoteInterp);
+}
+
+Engines::PyNodeBase_var PyFuncNode::getRemoteInterpreterHandle()
+{
+ return Engines::PyNodeBase::_narrow(_pynode);
+}
+
//! Create a new node of same type with a given name
PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
{
#include "InlineNode.hxx"
#include <SALOMEconfig.h>
#include CORBA_CLIENT_HEADER(SALOME_PyNode)
+#include CORBA_CLIENT_HEADER(SALOME_Component)
#include <Python.h>
{
namespace ENGINE
{
- class YACSRUNTIMESALOME_EXPORT PythonNode : public InlineNode
+ class YACSRUNTIMESALOME_EXPORT PythonEntry
+ {
+ protected:
+ PythonEntry();
+ ~PythonEntry();
+ virtual void assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp) = 0;
+ virtual Engines::PyNodeBase_var retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const = 0;
+ virtual void createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer) = 0;
+ virtual Engines::PyNodeBase_var getRemoteInterpreterHandle() = 0;
+ virtual const char *getSerializationScript() const = 0;
+ //
+ void commonRemoteLoad(InlineNode *reqNode);
+ protected:
+ PyObject *_context;
+ PyObject *_pyfuncSer;
+ PyObject *_pyfuncUnser;
+ };
+
+ class YACSRUNTIMESALOME_EXPORT PythonNode : public InlineNode, public PythonEntry
{
protected:
Node *simpleClone(ComposedNode *father, bool editionOnly) const;
+ // overload part of PythonEntry
+ void createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer);
+ Engines::PyNodeBase_var retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const;
+ void assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp);
+ Engines::PyNodeBase_var getRemoteInterpreterHandle();
+ const char *getSerializationScript() const { return SCRIPT_FOR_SERIALIZATION; }
public:
PythonNode(const PythonNode& other, ComposedNode *father);
PythonNode(const std::string& name);
virtual void shutdown(int level);
std::string getContainerLog();
PythonNode* cloneNode(const std::string& name);
+ virtual std::string typeName() { return "YACS__ENGINE__PythonNode"; }
+ public:
static const char KIND[];
static const char IMPL_NAME[];
- virtual std::string typeName() {return "YACS__ENGINE__PythonNode";}
+ static const char SCRIPT_FOR_SERIALIZATION[];
protected:
- PyObject* _context;
- PyObject* _pyfuncSer;
- PyObject* _pyfuncUnser;
Engines::PyScriptNode_var _pynode;
};
- class PyFuncNode : public InlineFuncNode
+ class PyFuncNode : public InlineFuncNode, public PythonEntry
{
protected:
Node *simpleClone(ComposedNode *father, bool editionOnly) const;
+ // overload part of PythonEntry
+ void createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer);Engines::
+ PyNodeBase_var retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const;
+ void assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp);
+ Engines::PyNodeBase_var getRemoteInterpreterHandle();
+ const char *getSerializationScript() const { return SCRIPT_FOR_SERIALIZATION; }
public:
PyFuncNode(const PyFuncNode& other, ComposedNode *father);
PyFuncNode(const std::string& name);
virtual void shutdown(int level);
std::string getContainerLog();
PyFuncNode* cloneNode(const std::string& name);
- virtual std::string typeName() {return "YACS__ENGINE__PyFuncNode";}
+ virtual std::string typeName() { return "YACS__ENGINE__PyFuncNode"; }
+ public:
+ static const char SCRIPT_FOR_SERIALIZATION[];
protected:
- PyObject* _context;
PyObject* _pyfunc;
- PyObject* _pyfuncSer;
- PyObject* _pyfuncUnser;
Engines::PyNode_var _pynode;
};
}