Salome HOME
updated copyright message
[modules/yacs.git] / src / evalyfx / YACSEvalYFXPattern.cxx
index db435ffa3fbb4f8eb40b433f164109523fd6a403..f9e60001a1f924cbf6f0a2114494f05343dc3795 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2016  CEA/DEN, EDF R&D
+// Copyright (C) 2012-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -43,7 +43,7 @@
 #include "InlineNode.hxx"
 #include "ServiceNode.hxx"
 #include "PyStdout.hxx"
-#include "AutoGIL.hxx"
+#include "PythonCppUtils.hxx"
 
 #include "ResourcesManager.hxx"
 
@@ -79,15 +79,10 @@ public:
   {
     if(_isToSave)
       {
-        PyThreadState *save(PyThreadState_Swap(NULL));// safe call of PyEval_SaveThread()
-        if(save)
-          {
-            _save=save;
-            PyEval_ReleaseLock();
-          }
+        _save = PyEval_SaveThread();
       }
   }
-  ~MyAutoThreadSaver() { if(_isToSave) if(_save) { PyEval_AcquireLock(); PyThreadState_Swap(_save); /*safe call of PyEval_RestoreThread*/ } }
+  ~MyAutoThreadSaver() { if(_isToSave) PyEval_RestoreThread(_save); }
 private:
   bool _isToSave;
   PyThreadState *_save;
@@ -263,7 +258,7 @@ YACSEvalSeqAny *YACSEvalYFXPattern::BuildValueInPort(YACS::ENGINE::InputPyPort *
       for(std::size_t i=0;i<sz;i++)
         {
           PyObject *elt(PyList_GetItem(obj,i));
-          eltCpp[i]=PyInt_AsLong(elt);
+          eltCpp[i]=PyLong_AsLong(elt);
         }
       YACS::AutoCppPtr<YACSEvalSeqAnyInt> elt(new YACSEvalSeqAnyInt(eltCpp));
       return elt.dettach();
@@ -891,19 +886,26 @@ std::vector<YACSEvalSeqAny *> YACSEvalYFXGraphGenInteractive::getResults() const
 
 void YACSEvalYFXGraphGenCluster::generateGraph()
 {
-  YACS::ENGINE::AutoGIL agil;
+  AutoGIL agil;
   //
   const char EFXGenFileName[]="EFXGenFileName";
   const char EFXGenContent[]="import getpass,datetime,os\nn=datetime.datetime.now()\nreturn os.path.join(os.path.sep,\"tmp\",\"EvalYFX_%s_%s_%s.xml\"%(getpass.getuser(),n.strftime(\"%d%m%y\"),n.strftime(\"%H%M%S\")))";
   const char EFXGenContent2[]="import getpass,datetime\nn=datetime.datetime.now()\nreturn \"EvalYFX_%s_%s_%s\"%(getpass.getuser(),n.strftime(\"%d%m%y\"),n.strftime(\"%H%M%S\"))";
   //
-  YACS::ENGINE::AutoPyRef func(YACS::ENGINE::evalPy(EFXGenFileName,EFXGenContent));
-  YACS::ENGINE::AutoPyRef val(YACS::ENGINE::evalFuncPyWithNoParams(func));
-  _locSchemaFile=PyString_AsString(val);
+  AutoPyRef func(YACS::ENGINE::evalPy(EFXGenFileName,EFXGenContent));
+  AutoPyRef val(YACS::ENGINE::evalFuncPyWithNoParams(func));
+  if (PyUnicode_Check(val))
+    _locSchemaFile = PyUnicode_AsUTF8(val);
+  else
+    throw YACS::Exception("YACSEvalYFXGraphGenCluster::generateGraph: python call error. ");
+
   func=YACS::ENGINE::evalPy(EFXGenFileName,EFXGenContent2);
   val=YACS::ENGINE::evalFuncPyWithNoParams(func);
-  _jobName=PyString_AsString(val);
-  //
+  if (PyUnicode_Check(val))
+    _jobName = PyUnicode_AsUTF8(val);
+  else
+    throw YACS::Exception("YACSEvalYFXGraphGenCluster::generateGraph: python call error. ");
+
   class ClusterPatcher : public YACSEvalYFXGraphGen::CustomPatcher
   {
   public:
@@ -911,7 +913,7 @@ void YACSEvalYFXGraphGenCluster::generateGraph()
     void addOutputVar(const std::string& name) { n2Script<< name << ", "; }
     void assignOutput(YACS::ENGINE::InlineNode *node) {
       n2Script << "]\nwith open(\"" << _jobName << "\",\"w\") as f:" << std::endl;
-      n2Script << "  f.write(str(zeRes))" << std::endl;
+      n2Script << "  f.write(repr(zeRes))" << std::endl;
       node->setScript(n2Script.str());
     }
   private:
@@ -925,7 +927,7 @@ void YACSEvalYFXGraphGenCluster::generateGraph()
 
 bool YACSEvalYFXGraphGenCluster::go(const YACSEvalExecParams& params, YACSEvalSession *session) const
 {
-  YACS::ENGINE::AutoGIL agil;
+  AutoGIL agil;
   _errors = "";
   getUndergroundGeneratedGraph()->saveSchema(_locSchemaFile);
   YACSEvalListOfResources *rss(getBoss()->getResourcesInternal());
@@ -942,9 +944,9 @@ bool YACSEvalYFXGraphGenCluster::go(const YACSEvalExecParams& params, YACSEvalSe
   rr.OS=CORBA::string_dup("Linux");
   rr.componentList.length(0);
   rr.nb_proc=rss->getNumberOfProcsDeclared();// <- important
-  rr.mem_mb=1024;
-  rr.cpu_clock=1000;
-  rr.nb_node=1;// useless only nb_proc used.
+  rr.mem_mb=0; // use default value
+  rr.cpu_clock=0; // use default value
+  rr.nb_node=0;// the number of nodes is not explicitly set
   rr.nb_proc_per_node=1;// useless only nb_proc used.
   rr.policy=CORBA::string_dup("cycl");
   rr.resList.length(0);
@@ -1012,11 +1014,11 @@ bool YACSEvalYFXGraphGenCluster::go(const YACSEvalExecParams& params, YACSEvalSe
     {
       std::ostringstream oss; oss << "import os" << std::endl << "p=os.path.join(\"" << cli.getLocalWorkingDir() << "\",\"" << _jobName  << "\")" << std::endl;
       oss << "if not os.path.exists(p):\n  return None\n";
-      oss << "f=file(p,\"r\")" << std::endl;
+      oss << "f=open(p,\"r\")" << std::endl;
       oss << "return eval(f.read())";
       std::string zeInput(oss.str());
-      YACS::ENGINE::AutoPyRef func(YACS::ENGINE::evalPy("fetch",zeInput));
-      YACS::ENGINE::AutoPyRef val(YACS::ENGINE::evalFuncPyWithNoParams(func));
+      AutoPyRef func(YACS::ENGINE::evalPy("fetch",zeInput));
+      AutoPyRef val(YACS::ENGINE::evalFuncPyWithNoParams(func));
       if(!PyList_Check(val))
         throw YACS::Exception("Fetched file does not contain a list !");
       Py_ssize_t sz(PyList_Size(val));