26 #include "PythonCppUtils.hxx"
62 bool isContAlreadyStarted(
false);
71 msg <<
"Impossible to set builtins" << __FILE__ <<
":" << __LINE__;
75 const char picklizeScript[]=
"import pickle\ndef pickleForDistPyth2009(*args,**kws):\n return pickle.dumps((args,kws),-1)\n\ndef unPickleForDistPyth2009(st):\n args=pickle.loads(st)\n return args\n";
81 PySys_SetObject((
char*)
"stderr", new_stderr);
83 PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
84 Py_DECREF(new_stderr);
85 throw Exception(
"Error during execution");
95 PySys_SetObject((
char*)
"stderr", new_stderr);
97 PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
98 Py_DECREF(new_stderr);
99 throw Exception(
"Error during execution");
105 PySys_SetObject((
char*)
"stderr", new_stderr);
107 PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
108 Py_DECREF(new_stderr);
109 throw Exception(
"Error during execution");
112 Engines::Container_var objContainer=Engines::Container::_nil();
114 throw Exception(
"No container specified !");
119 else if(containerCast1)
122 objContainer=tmpCont->getContainerPtr(
this);
125 throw Exception(
"Unrecognized type of container ! Salome one is expected !");
126 if(CORBA::is_nil(objContainer))
127 throw Exception(
"Container corba pointer is NULL !");
131 if(containerCast0 || !isContAlreadyStarted)
137 Engines::PyNode_var dftPyScript(objContainer->getDefaultPyNode(
getName().c_str()));
138 if(CORBA::is_nil(dftPyScript))
144 catch(
const SALOME::SALOME_Exception&
ex )
146 std::string msg=
"Exception on remote python node creation ";
148 msg +=
ex.details.text.in();
154 throw Exception(
"In DistributedPythonNode the ref in NULL ! ");
157 DEBUG_YACSTRACE(
"---------------End PyfuncSerNode::load function---------------" );
167 throw Exception(
"DistributedPythonNode badly loaded");
168 Engines::pickledArgs *serializationInputCorba(0);
173 GURU_YACSTRACE(
"---------------DistributedPythonNode::inputs---------------" );
181 PyTuple_SetItem(
args,pos,ob);
183 PyObject *serializationInput=PyObject_CallObject(
_pyfuncSer,
args);
184 Py_ssize_t len = PyBytes_Size(serializationInput);
185 char* serializationInputC = PyBytes_AsString(serializationInput);
187 serializationInputCorba=
new Engines::pickledArgs;
188 serializationInputCorba->length(len+1);
189 for(
int i=0;
i<len+1;
i++)
190 (*serializationInputCorba)[
i]=serializationInputC[
i];
191 Py_DECREF(serializationInput);
194 DEBUG_YACSTRACE(
"-----------------DistributedPythonNode starting remote python invocation-----------------" );
195 Engines::pickledArgs *resultCorba;
198 resultCorba=
_pynode->execute(
getFname().c_str(),*serializationInputCorba);
202 std::string msg=
"Exception on remote python invocation";
206 DEBUG_YACSTRACE(
"-----------------DistributedPythonNode end of remote python invocation-----------------" );
208 delete serializationInputCorba;
209 char *resultCorbaC=
new char[resultCorba->length()+1];
210 resultCorbaC[resultCorba->length()]=
'\0';
211 for(
int i=0;
i<resultCorba->length();
i++)
212 resultCorbaC[
i]=(*resultCorba)[
i];
213 int lenResCorba=resultCorba->length();
217 args = PyTuple_New(1);
219 PyObject* resultPython=PyBytes_FromStringAndSize(resultCorbaC,lenResCorba);
220 delete [] resultCorbaC;
221 PyTuple_SetItem(
args,0,resultPython);
223 GURU_YACSTRACE(
"-----------------DistributedPythonNode::outputs-----------------" );
224 if(finalResult == NULL)
226 std::stringstream msg;
227 msg <<
"Conversion with pickle of output ports failed !";
228 msg <<
" : " << __FILE__ <<
":" << __LINE__;
233 if(finalResult == Py_None)
235 else if(PyTuple_Check(finalResult))
236 nres=PyTuple_Size(finalResult);
240 std::string msg=
"Number of output arguments : Mismatch between definition and execution";
241 Py_DECREF(finalResult);
254 if(PyTuple_Check(finalResult))ob=PyTuple_GetItem(finalResult,pos) ;
262 Py_DECREF(finalResult);
267 GURU_YACSTRACE(
"++++++++++++++ End DistributedPythonNode::execute: " <<
getName() <<
" ++++++++++++++++++++" );
300 GURU_YACSTRACE(
"The raised exception is of Type:" << exc->_name() );
302 CORBA::SystemException* sysexc;
303 sysexc=CORBA::SystemException::_downcast(exc);
309 std::string text=
"Execution problem: ";
310 std::string excname=sysexc->_name();
311 if(excname ==
"BAD_OPERATION")
313 text=text+
"bad operation detected";
315 else if(excname ==
"MARSHAL" && sysexc->minor() == omni::MARSHAL_PassEndOfMessage)
317 text=text+
"probably an error in arguments of service '" + method +
"' from component '" +
ref+
"'";
319 else if(excname ==
"COMM_FAILURE" && sysexc->minor() == omni::COMM_FAILURE_UnMarshalResults)
321 text=text+
"probably an error in output arguments of service '" + method +
"' from component '" +
ref+
"'";
323 else if(excname ==
"COMM_FAILURE" && sysexc->minor() == omni::COMM_FAILURE_UnMarshalArguments)
325 text=text+
"probably an error in input arguments of service '" + method +
"' from component '" +
ref+
"'";
327 else if(excname ==
"COMM_FAILURE" && sysexc->minor() == omni::COMM_FAILURE_WaitingForReply)
329 text=text+
"probably an error in input arguments of service '" + method +
"' from component '" +
ref+
"'";
334 text=text+
"System Exception "+ excname;
341 CORBA::UnknownUserException* userexc;
342 userexc=CORBA::UnknownUserException::_downcast(exc);
345 CORBA::Any anyExcept = userexc->exception();
347 const SALOME::SALOME_Exception* salexc;
348 if(anyExcept >>= salexc)
350 GURU_YACSTRACE(
"SALOME_Exception: "<< salexc->details.sourceFile);
355 std::string msg=
"Execution problem: User Exception occurred";
359 std::string msg=
"Execution problem";
#define DEBUG_YACSTRACE(msg)
#define GURU_YACSTRACE(msg)
Base class for all composed nodes.
std::string getName() const
virtual bool isAlreadyStarted(const Task *askingNode) const =0
Node * simpleClone(ComposedNode *father, bool editionOnly) const
static const char SALOME_CONTAINER_METHOD_IDL[]
DistributedPythonNode(const std::string &name)
ServerNode * createNode(const std::string &name) const
std::string getKind() const
this method returns the type of input/output expected
void dealException(CORBA::Exception *exc, const char *method, const char *ref)
std::string getEffectiveKindOfServer() const
this method returns the type of container expected
static const char IMPL_NAME[]
Engines::PyNode_var _pynode
int getNumberOfOutputPorts() const
int getNumberOfInputPorts() const
std::list< InputPort * > _setOfInputPort
virtual std::string getErrorDetails()
Give a description of error when node status is ERROR.
std::list< OutputPort * > _setOfOutputPort
virtual std::string getFname()
virtual void setContainer(Container *container)
virtual std::string getScript()
Base class for all nodes.
std::string _implementation
const std::string & getName() const
std::string _errorDetails
virtual std::string typeName()
static SalomeContainerTmpForHP * BuildFrom(const SalomeHPContainer *cont, const Task *askingNode)
Engines::Container_ptr getContainerPtr(const Task *askingNode) const
YACSRUNTIMESALOME_EXPORT RuntimeSALOME * getSALOMERuntime()
PyObject * newPyStdOut(std::string &out)
def ref(target, callback=None)