38 #define getpid _getpid 
   41 #if PY_VERSION_HEX < 0x02050000  
   48 using namespace YACS::ENGINE;
 
   52     "def pickleForVarSimplePyth2009(val):\n" 
   53     "  return cPickle.dumps(val,-1)\n" 
   60     "def pickleForDistPyth2009(kws):\n" 
   61     "  return cPickle.dumps(((),kws),-1)\n" 
   63     "def unPickleForDistPyth2009(st):\n" 
   64     "  args=cPickle.loads(st)\n" 
   72     "def pickleForDistPyth2009(*args,**kws):\n" 
   73     "  return cPickle.dumps((args,kws),-1)\n" 
   75     "def unPickleForDistPyth2009(st):\n" 
   76     "  args=cPickle.loads(st)\n" 
   95   DEBTRACE( 
"---------------PythonEntry::CommonRemoteLoad function---------------" );
 
   97   bool isContAlreadyStarted(
false);
 
  100       isContAlreadyStarted=container->isAlreadyStarted(reqNode);
 
  101       if(!isContAlreadyStarted)
 
  105               container->start(reqNode);
 
  116       std::string what(
"PythonEntry::CommonRemoteLoad : a load operation requested on \"");
 
  117       what+=reqNode->
getName(); what+=
"\" with no container specified.";
 
  126   Engines::Container_var objContainer=Engines::Container::_nil();
 
  128     throw Exception(
"No container specified !");
 
  129   SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(container));
 
  133   else if(containerCast1)
 
  139     throw Exception(
"Unrecognized type of container ! Salome one is expected for PythonNode/PyFuncNode !");
 
  140   if(CORBA::is_nil(objContainer))
 
  141     throw Exception(
"Container corba pointer is NULL for PythonNode !");
 
  142   isInitializeRequested=
false;
 
  152           if(CORBA::is_nil(dftPyScript))
 
  154               isInitializeRequested=
true;
 
  161   catch( 
const SALOME::SALOME_Exception& ex )
 
  163       std::string msg=
"Exception on remote python node creation ";
 
  165       msg += ex.details.text.in();
 
  170   if(CORBA::is_nil(pynode))
 
  171     throw Exception(
"In PythonNode the ref in NULL ! ");
 
  185     if(res == NULL || res2==NULL)
 
  187         std::string errorDetails;
 
  190         PySys_SetObject((
char*)
"stderr", new_stderr);
 
  192         PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
 
  193         Py_DECREF(new_stderr);
 
  196     Py_DECREF(res); Py_DECREF(res2);
 
  202         std::string errorDetails;
 
  205         PySys_SetObject((
char*)
"stderr", new_stderr);
 
  207         PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
 
  208         Py_DECREF(new_stderr);
 
  213         std::string errorDetails;
 
  216         PySys_SetObject((
char*)
"stderr", new_stderr);
 
  218         PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
 
  219         Py_DECREF(new_stderr);
 
  224         std::string errorDetails;
 
  227         PySys_SetObject((
char*)
"stderr", new_stderr);
 
  229         PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
 
  230         Py_DECREF(new_stderr);
 
  234   if(isInitializeRequested)
 
  239           if(!zeInitScriptKey.empty())
 
  240             pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
 
  242       catch( 
const SALOME::SALOME_Exception& ex )
 
  244           std::string msg=
"Exception on PythonNode::loadRemote python invocation of initializisation py script !";
 
  246           msg += ex.details.text.in();
 
  250       DEBTRACE( 
"---------------End PyNode::loadRemote function---------------" );
 
  262       SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
 
  266           Engines::Container_var objContainer(containerCast->
getContainerPtr(askingTask));
 
  267           CORBA::String_var logname = objContainer->logfilename();
 
  270           std::string::size_type pos = msg.find(
":");
 
  271           msg=msg.substr(pos+1);
 
  273       else if(objContainer2)
 
  275           msg=
"Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
 
  279           msg=
"Not implemented yet for container log for that type of container !";
 
  284       msg = 
"Container no longer reachable";
 
  292   bool isInitializeRequested;
 
  306         msg << 
"Impossible to set builtins" << __FILE__ << 
":" << __LINE__;
 
  322         msg << 
"Impossible to set builtins" << __FILE__ << 
":" << __LINE__;
 
  344     res=Py_CompileString(
_script.c_str(),
getName().c_str(),Py_file_input);
 
  347         std::string error=
"";
 
  349         PySys_SetObject((
char*)
"stderr", new_stderr);
 
  351         PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
 
  352         Py_DECREF(new_stderr);
 
  353         throw Exception(error);
 
  362   DEBTRACE( 
"---------------PyNode::load function---------------" );
 
  371   DEBTRACE( 
"---------------PyNode::loadLocal function---------------" );
 
  390   DEBTRACE( 
"++++++++++++++ PyNode::executeRemote: " << 
getName() << 
" ++++++++++++++++++++" );
 
  392     throw Exception(
"DistributedPythonNode badly loaded");
 
  394   if(dynamic_cast<HomogeneousPoolContainer *>(
getContainer()))
 
  401   Engines::pickledArgs_var serializationInputCorba(
new Engines::pickledArgs);
 
  404       PyObject *args(0),*ob(0);
 
  409       std::list<InputPort *>::iterator iter2;
 
  415           PyDict_SetItemString(args,p->
getName().c_str(),ob);
 
  419       PyObject_Print(args,stderr,Py_PRINT_RAW);
 
  422       PyObject *serializationInput(PyObject_CallFunctionObjArgs(
_pyfuncSer,args,NULL));
 
  425       char *serializationInputC(0);
 
  427       if (PyString_AsStringAndSize(serializationInput, &serializationInputC, &len))
 
  428         throw Exception(
"DistributedPythonNode problem in python pickle");
 
  429       serializationInputCorba->length(len);
 
  430       for(
int i=0; 
i < len ; 
i++)
 
  431         serializationInputCorba[
i]=serializationInputC[
i];
 
  432       Py_DECREF(serializationInput);
 
  436   std::list<OutputPort *>::iterator iter;
 
  437   Engines::listofstring myseq;
 
  443       myseq[pos]=p->
getName().c_str();
 
  452   DEBTRACE( 
"-----------------starting remote python invocation-----------------" );
 
  453   Engines::pickledArgs_var resultCorba;
 
  457       resultCorba=
_pynode->execute(myseq,serializationInputCorba);
 
  459   catch( 
const SALOME::SALOME_Exception& ex )
 
  461       std::string msg=
"Exception on remote python invocation";
 
  463       msg += ex.details.text.in();
 
  467   DEBTRACE( 
"-----------------end of remote python invocation-----------------" );
 
  471   char *resultCorbaC=
new char[resultCorba->length()+1];
 
  472   resultCorbaC[resultCorba->length()]=
'\0';
 
  473   for(
int i=0;
i<resultCorba->length();
i++)
 
  474     resultCorbaC[
i]=resultCorba[
i];
 
  478       PyObject *args(0),*ob(0);
 
  479       PyObject* resultPython=PyString_FromStringAndSize(resultCorbaC,resultCorba->length());
 
  480       delete [] resultCorbaC;
 
  481       args = PyTuple_New(1);
 
  482       PyTuple_SetItem(args,0,resultPython);
 
  483       PyObject *finalResult=PyObject_CallObject(
_pyfuncUnser,args);
 
  486       if (finalResult == NULL)
 
  488           std::stringstream msg;
 
  489           msg << 
"Conversion with pickle of output ports failed !";
 
  490           msg << 
" : " << __FILE__ << 
":" << __LINE__;
 
  495       DEBTRACE( 
"-----------------PythonNode::outputs-----------------" );
 
  497       if(finalResult == Py_None)
 
  499       else if(PyTuple_Check(finalResult))
 
  500         nres=PyTuple_Size(finalResult);
 
  504           std::string msg=
"Number of output arguments : Mismatch between definition and execution";
 
  505           Py_DECREF(finalResult);
 
  519               if(PyTuple_Check(finalResult))
 
  520                 ob=PyTuple_GetItem(finalResult,pos) ;
 
  523               DEBTRACE( 
"ob refcnt: " << ob->ob_refcnt );
 
  527           Py_DECREF(finalResult);
 
  531           Py_DECREF(finalResult);
 
  536   DEBTRACE( 
"++++++++++++++ ENDOF PyNode::executeRemote: " << 
getName() << 
" ++++++++++++++++++++" );
 
  541   DEBTRACE( 
"++++++++++++++ PyNode::executeLocal: " << 
getName() << 
" ++++++++++++++++++++" );
 
  545     DEBTRACE( 
"---------------PyNode::inputs---------------" );
 
  546     list<InputPort *>::iterator iter2;
 
  553         DEBTRACE( 
"ob refcnt: " << ob->ob_refcnt );
 
  555         PyObject_Print(ob,stderr,Py_PRINT_RAW);
 
  559         DEBTRACE( 
"after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
 
  562     DEBTRACE( 
"---------------End PyNode::inputs---------------" );
 
  565     DEBTRACE( 
"----------------PyNode::calculation---------------" );
 
  569     std::ostringstream stream;
 
  570     stream << 
"/tmp/PythonNode_";
 
  573     PyObject* code=Py_CompileString(
_script.c_str(), stream.str().c_str(), Py_file_input);
 
  578         PySys_SetObject((
char*)
"stderr", new_stderr);
 
  580         PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
 
  581         Py_DECREF(new_stderr);
 
  582         throw Exception(
"Error during execution");
 
  591     if(PyErr_Occurred ())
 
  595         PySys_SetObject((
char*)
"stderr", new_stderr);
 
  596         ofstream errorfile(stream.str().c_str());
 
  597         if (errorfile.is_open())
 
  603         PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
 
  604         Py_DECREF(new_stderr);
 
  605         throw Exception(
"Error during execution");
 
  608     DEBTRACE( 
"-----------------PyNode::outputs-----------------" );
 
  609     list<OutputPort *>::iterator iter;
 
  620                 std::string msg=
"Error during execution: there is no variable ";
 
  621                 msg=msg+p->
getName()+
" in node context";
 
  625             DEBTRACE( 
"PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
 
  627             PyObject_Print(ob,stderr,Py_PRINT_RAW);
 
  639     DEBTRACE( 
"-----------------End PyNode::outputs-----------------" );
 
  641   DEBTRACE( 
"++++++++++++++ End PyNode::execute: " << 
getName() << 
" ++++++++++++++++++++" );
 
  651   DEBTRACE(
"PythonNode::shutdown " << level);
 
  652   if(
_mode==
"local")
return;
 
  656       _pynode=Engines::PyScriptNode::_nil();
 
  675   Engines::PyScriptNode_var 
ret(objContainer->getDefaultPyScriptNode(
getName().c_str()));
 
  676   if(!CORBA::is_nil(
ret))
 
  680   return Engines::PyNodeBase::_narrow(
ret);
 
  687       Engines::PyScriptNode_var tmpp(Engines::PyScriptNode::_narrow(remoteInterp));
 
  688       if(
_pynode->_is_equivalent(tmpp))
 
  693   _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
 
  698   return Engines::PyNodeBase::_narrow(
_pynode);
 
  706   list<InputPort *>::iterator iter;
 
  714   list<OutputPort *>::iterator iter2;
 
  734     std::size_t sz(ret.size());
 
  736     for(std::size_t 
i=0;
i<sz;
i++)
 
  738         const std::pair<std::string,int>& 
p(ret[
i]);
 
  739         PyObject *elt(PyTuple_New(2));
 
  740         PyTuple_SetItem(elt,0,PyString_FromString(p.first.c_str()));
 
  741         PyTuple_SetItem(elt,1,PyLong_FromLong(p.second));
 
  742         PyList_SetItem(ob,i,elt);
 
  747       Engines::pickledArgs_var serializationInputCorba(
new Engines::pickledArgs);
 
  750         PyObject *serializationInput(PyObject_CallFunctionObjArgs(
_pyfuncSimpleSer,ob,NULL));
 
  752         char *serializationInputC(0);
 
  754         if (PyString_AsStringAndSize(serializationInput, &serializationInputC, &len))
 
  755           throw Exception(
"DistributedPythonNode problem in python pickle");
 
  756         serializationInputCorba->length(len);
 
  757         for(
int i=0; 
i < len ; 
i++)
 
  758           serializationInputCorba[
i]=serializationInputC[
i];
 
  759         Py_XDECREF(serializationInput);
 
  781         msg << 
"Not possible to set builtins" << __FILE__ << 
":" << __LINE__;
 
  792   DEBTRACE( 
"PyFuncNode::PyFuncNode " << name );
 
  800         msg << 
"Not possible to set builtins" << __FILE__ << 
":" << __LINE__;
 
  836     res=Py_CompileString(
_script.c_str(),
getName().c_str(),Py_file_input);
 
  839         std::string error=
"";
 
  841         PySys_SetObject((
char*)
"stderr", new_stderr);
 
  843         PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
 
  844         Py_DECREF(new_stderr);
 
  845         throw Exception(error);
 
  854   DEBTRACE( 
"---------------PyfuncNode::load function---------------" );
 
  868   DEBTRACE( 
"---------------PyFuncNode::load function " << 
getName() << 
" ---------------" );
 
  872   list<OutputPort *>::iterator iter;
 
  885     std::ostringstream stream;
 
  886     stream << 
"/tmp/PythonNode_";
 
  889     PyObject* code=Py_CompileString(
_script.c_str(), stream.str().c_str(), Py_file_input);
 
  894         PySys_SetObject((
char*)
"stderr", new_stderr);
 
  896         PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
 
  897         Py_DECREF(new_stderr);
 
  898         throw Exception(
"Error during execution");
 
  905     if(PyErr_Occurred ())
 
  909         PySys_SetObject((
char*)
"stderr", new_stderr);
 
  910         ofstream errorfile(stream.str().c_str());
 
  911         if (errorfile.is_open())
 
  917         PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
 
  918         Py_DECREF(new_stderr);
 
  919         throw Exception(
"Error during execution");
 
  928         PySys_SetObject((
char*)
"stderr", new_stderr);
 
  930         PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
 
  931         Py_DECREF(new_stderr);
 
  932         throw Exception(
"Error during execution");
 
  934     DEBTRACE( 
"---------------End PyFuncNode::load function---------------" );
 
  948   DEBTRACE( 
"++++++++++++++ PyFuncNode::executeRemote: " << 
getName() << 
" ++++++++++++++++++++" );
 
  950     throw Exception(
"DistributedPythonNode badly loaded");
 
  952   if(dynamic_cast<HomogeneousPoolContainer *>(
getContainer()))
 
  959   Engines::pickledArgs_var serializationInputCorba(
new Engines::pickledArgs);;
 
  973           PyTuple_SetItem(args,pos,ob);
 
  976       PyObject_Print(args,stderr,Py_PRINT_RAW);
 
  979       PyObject *serializationInput=PyObject_CallObject(
_pyfuncSer,args);
 
  982       char *serializationInputC(0);
 
  984       if (PyString_AsStringAndSize(serializationInput, &serializationInputC, &len))
 
  985         throw Exception(
"DistributedPythonNode problem in python pickle");
 
  987       serializationInputCorba->length(len);
 
  988       for(
int i=0; 
i < len ; 
i++)
 
  989         serializationInputCorba[
i]=serializationInputC[
i];
 
  990       Py_DECREF(serializationInput);
 
  996   DEBTRACE( 
"-----------------starting remote python invocation-----------------" );
 
  997   Engines::pickledArgs_var resultCorba;
 
 1000       resultCorba=
_pynode->execute(
getFname().c_str(),serializationInputCorba);
 
 1002   catch( 
const SALOME::SALOME_Exception& ex )
 
 1004       std::string msg=
"Exception on remote python invocation";
 
 1006       msg += ex.details.text.in();
 
 1010   DEBTRACE( 
"-----------------end of remote python invocation-----------------" );
 
 1014   char *resultCorbaC=
new char[resultCorba->length()+1];
 
 1015   resultCorbaC[resultCorba->length()]=
'\0';
 
 1016   for(
int i=0;
i<resultCorba->length();
i++)
 
 1017     resultCorbaC[
i]=resultCorba[
i];
 
 1022       PyObject *resultPython(PyString_FromStringAndSize(resultCorbaC,resultCorba->length()));
 
 1023       delete [] resultCorbaC;
 
 1024       PyObject *args(PyTuple_New(1)),*ob(0);
 
 1025       PyTuple_SetItem(args,0,resultPython);
 
 1026       PyObject *finalResult=PyObject_CallObject(
_pyfuncUnser,args);
 
 1029       DEBTRACE( 
"-----------------PythonNode::outputs-----------------" );
 
 1031       if(finalResult == Py_None)
 
 1033       else if(PyTuple_Check(finalResult))
 
 1034         nres=PyTuple_Size(finalResult);
 
 1038           std::string msg=
"Number of output arguments : Mismatch between definition and execution";
 
 1039           Py_DECREF(finalResult);
 
 1053               if(PyTuple_Check(finalResult))
 
 1054                 ob=PyTuple_GetItem(finalResult,pos) ;
 
 1057               DEBTRACE( 
"ob refcnt: " << ob->ob_refcnt );
 
 1060           Py_DECREF(finalResult);
 
 1064           Py_DECREF(finalResult);
 
 1070   DEBTRACE( 
"++++++++++++++ ENDOF PyFuncNode::executeRemote: " << 
getName() << 
" ++++++++++++++++++++" );
 
 1075   DEBTRACE( 
"++++++++++++++ PyFuncNode::execute: " << 
getName() << 
" ++++++++++++++++++++" );
 
 1082       DEBTRACE( 
"---------------PyFuncNode::inputs---------------" );
 
 1084       list<InputPort *>::iterator iter2;
 
 1092           PyObject_Print(ob,stderr,Py_PRINT_RAW);
 
 1095           DEBTRACE( 
"ob refcnt: " << ob->ob_refcnt );
 
 1097           PyTuple_SetItem(args,pos,ob);
 
 1098           DEBTRACE( 
"ob refcnt: " << ob->ob_refcnt );
 
 1101       DEBTRACE( 
"---------------End PyFuncNode::inputs---------------" );
 
 1103       DEBTRACE( 
"----------------PyFuncNode::calculation---------------" );
 
 1105       PyObject_Print(
_pyfunc,stderr,Py_PRINT_RAW);
 
 1107       PyObject_Print(args,stderr,Py_PRINT_RAW);
 
 1111       PyObject* result = PyObject_CallObject( 
_pyfunc , args ) ;
 
 1120           PySys_SetObject((
char*)
"stderr", new_stderr);
 
 1121           std::ostringstream stream;
 
 1122           stream << 
"/tmp/PythonNode_";
 
 1124           ofstream errorfile(stream.str().c_str());
 
 1125           if (errorfile.is_open())
 
 1131           PySys_SetObject((
char*)
"stderr", PySys_GetObject((
char*)
"__stderr__"));
 
 1132           Py_DECREF(new_stderr);
 
 1133           throw Exception(
"Error during execution");
 
 1135       DEBTRACE( 
"----------------End PyFuncNode::calculation---------------" );
 
 1137       DEBTRACE( 
"-----------------PyFuncNode::outputs-----------------" );
 
 1139       if(result == Py_None)
 
 1141       else if(PyTuple_Check(result))
 
 1142         nres=PyTuple_Size(result);
 
 1146           std::string msg=
"Number of output arguments : Mismatch between definition and execution";
 
 1154       PyObject_Print(result,stderr,Py_PRINT_RAW);
 
 1157       list<OutputPort *>::iterator iter;
 
 1166               if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
 
 1168               DEBTRACE( 
"ob refcnt: " << ob->ob_refcnt );
 
 1170               PyObject_Print(ob,stderr,Py_PRINT_RAW);
 
 1183       DEBTRACE( 
"-----------------End PyFuncNode::outputs-----------------" );
 
 1186   DEBTRACE( 
"++++++++++++++ End PyFuncNode::execute: " << 
getName() << 
" ++++++++++++++++++++" );
 
 1203   Engines::PyNode_var 
ret(objContainer->getDefaultPyNode(
getName().c_str()));
 
 1204   if(!CORBA::is_nil(
ret))
 
 1208   return Engines::PyNodeBase::_narrow(
ret);
 
 1215       Engines::PyNode_var tmpp(Engines::PyNode::_narrow(remoteInterp));
 
 1216       if(
_pynode->_is_equivalent(tmpp))
 
 1221   _pynode=Engines::PyNode::_narrow(remoteInterp);
 
 1226   return Engines::PyNodeBase::_narrow(
_pynode);
 
 1235   list<InputPort *>::iterator iter;
 
 1241   list<OutputPort *>::iterator iter2;
 
 1257   DEBTRACE(
"PyFuncNode::shutdown " << level);
 
 1258   if(
_mode==
"local")
return;
 
 1262       _pynode=Engines::PyNode::_nil();