Salome HOME
[EDF27816] : fix all non regression tests
authorAnthony Geay <anthony.geay@edf.fr>
Wed, 7 Jun 2023 12:57:51 +0000 (14:57 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Thu, 8 Jun 2023 15:38:20 +0000 (17:38 +0200)
src/py2yacs/Test/Py2yacsTest.cxx
src/py2yacs/Test/testDeco.py
src/py2yacs/Test/testforeach.py
src/runtime/PythonNode.cxx
src/runtime/PythonNode.hxx
src/runtime/TypeConversions.cxx
src/yacsloader/samples/wlm_complex_foreach.xml
src/yacsloader_swig/Test/testPynodeWithCache.py
src/yacsloader_swig/Test/testSaveLoadRun.py

index 59d8ab2ff793815691d37ebd055aaf6c59455f03..e95fe58cdcf64416b1fb515582c84f416684d370 100644 (file)
@@ -25,6 +25,7 @@
 #include "py2yacs.hxx"
 #include "Proc.hxx"
 #include "Executor.hxx"
+#include "PythonCppUtils.hxx"
 
 #include "RuntimeSALOME.hxx"
 #include "PythonPorts.hxx"
@@ -295,10 +296,15 @@ void Py2yacsTest::schemaExec()
   // verify the output port value
   YACS::ENGINE::OutputPyPort* var = dynamic_cast<YACS::ENGINE::OutputPyPort*>(n->getOutputPort("x"));
   CPPUNIT_ASSERT(var);
+  double x = -2.0;
   PyObject* pyVal = var->get();
-  CPPUNIT_ASSERT(pyVal);
-  CPPUNIT_ASSERT(PyFloat_Check(pyVal));
-  CPPUNIT_ASSERT_DOUBLES_EQUAL(42., PyFloat_AsDouble(pyVal), 1.E-12);
+  {
+    AutoGIL agil;
+    CPPUNIT_ASSERT(pyVal);
+    x = PyFloat_AsDouble(pyVal);
+    CPPUNIT_ASSERT( PyErr_Occurred()==nullptr );// check pyVal is interpretable as float
+  }
+  CPPUNIT_ASSERT_DOUBLES_EQUAL(42., x, 1.E-12);
   p->shutdown(10); // kill all the containers
 }
 
index 4103f54d98132db7c0980b7416da9b5b206abe98..6f47f8cf3a24ae502641d6336d73f23b3a6ae3ca 100755 (executable)
@@ -65,6 +65,7 @@ class TestDeco(unittest.TestCase):
       f1 -> f3 -> f1
       """
       import testforeach
+      from SALOME_PyNode import UnProxyObjectSimple
       expected_1, expected_2 = testforeach.main()
       yacs_schema_file = os.path.join(dir_test, "schema_t1.xml")
       yacs_build_command = "yacsbuild.py"
@@ -76,8 +77,8 @@ class TestDeco(unittest.TestCase):
       ex = pilot.ExecutorSwig()
       proc = l.load(yacs_schema_file)
       ex.RunW(proc,0)
-      obtained_1 = proc.getChildByName("post_0").getOutputPort("s").getPyObj()
-      obtained_2 = proc.getChildByName("f1_1").getOutputPort("r").getPyObj()
+      obtained_1 = UnProxyObjectSimple( proc.getChildByName("post_0").getOutputPort("s").getPyObj() )
+      obtained_2 = UnProxyObjectSimple( proc.getChildByName("f1_1").getOutputPort("r").getPyObj() )
       self.assertEqual(expected_1, obtained_1)
       self.assertEqual(expected_2, obtained_2)
 
@@ -86,6 +87,7 @@ class TestDeco(unittest.TestCase):
       Foreach initialized by value.
       """
       import testforeach
+      from SALOME_PyNode import UnProxyObjectSimple
       expected_1, expected_2 = testforeach.mainblock()
       yacs_schema_file = os.path.join(dir_test, "schema_t2.xml")
       yacs_build_command = "yacsbuild.py"
@@ -97,8 +99,8 @@ class TestDeco(unittest.TestCase):
       ex = pilot.ExecutorSwig()
       proc = l.load(yacs_schema_file)
       ex.RunW(proc,0)
-      obtained_1 = proc.getChildByName("output_fr_0").getOutputPort("s_0").getPyObj()
-      obtained_2 = proc.getChildByName("output_fr_0").getOutputPort("p_1").getPyObj()
+      obtained_1 = UnProxyObjectSimple( proc.getChildByName("output_fr_0").getOutputPort("s_0").getPyObj() )
+      obtained_2 = UnProxyObjectSimple( proc.getChildByName("output_fr_0").getOutputPort("p_1").getPyObj() )
       self.assertEqual(expected_1, obtained_1)
       self.assertEqual(expected_2, obtained_2)
 
@@ -107,6 +109,7 @@ class TestDeco(unittest.TestCase):
       Foreach on 2 levels.
       """
       import testforeach
+      from SALOME_PyNode import UnProxyObjectSimple
       expected = testforeach.maindoublefr()
       yacs_schema_file = os.path.join(dir_test, "schema_t3.xml")
       yacs_build_command = "yacsbuild.py"
@@ -118,7 +121,7 @@ class TestDeco(unittest.TestCase):
       ex = pilot.ExecutorSwig()
       proc = l.load(yacs_schema_file)
       ex.RunW(proc,0)
-      obtained = proc.getChildByName("output_doublefr_0").getOutputPort("r_0_0").getPyObj()
+      obtained = UnProxyObjectSimple( proc.getChildByName("output_doublefr_0").getOutputPort("r_0_0").getPyObj() )
       self.assertEqual(expected, obtained)
 
     def test_t4(self):
index fd5b989aa8007b50d3f3c8bcf1ec01c6e7946b49..75f0c371f705fc31e61be923b30c012c1f96c8f4 100755 (executable)
@@ -41,7 +41,7 @@ def doublefr(v):
 def post(t):
   s = 0
   for e in t:
-    s += e
+    s += int( e )
   return s
 
 @yacsdecorator.block
index 0b7b65e60a64c075441d41dbd1c52f8e721085c8..fad8e96e3009466e4d3c04b005dd61d14953c55d 100644 (file)
@@ -673,13 +673,52 @@ void PythonNode::executeRemote()
   }
   DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
 }
+void PythonNode::executeLocalInternal(const std::string& codeStr)
+{
+  DEBTRACE(  code );
+  DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
+  std::ostringstream stream;
+  stream << "/tmp/PythonNode_";
+  stream << getpid();
+  AutoPyRef code=Py_CompileString(codeStr.c_str(), stream.str().c_str(), Py_file_input);
+  if(code == NULL)
+  {
+    _errorDetails=""; 
+    AutoPyRef new_stderr = newPyStdOut(_errorDetails);
+    PySys_SetObject((char*)"stderr", new_stderr);
+    PyErr_Print();
+    PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+    throw Exception("Error during execution");
+  }
+  {
+    AutoPyRef res = PyEval_EvalCode(  code, _context, _context);
+  }
+  DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
+  fflush(stdout);
+  fflush(stderr);
+  if(PyErr_Occurred ())
+  {
+    _errorDetails="";
+    AutoPyRef new_stderr = newPyStdOut(_errorDetails);
+    PySys_SetObject((char*)"stderr", new_stderr);
+    ofstream errorfile(stream.str().c_str());
+    if (errorfile.is_open())
+      {
+        errorfile << codeStr;
+        errorfile.close();
+      }
+    PyErr_Print();
+    PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+    throw Exception("Error during execution");
+  }
+}
 
 void PythonNode::executeLocal()
 {
   DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
   {
     AutoGIL agil;
-
+    std::ostringstream unpxy; unpxy << "from SALOME_PyNode import UnProxyObjectSimple" << std::endl;
     DEBTRACE( "---------------PyNode::inputs---------------" );
     list<InputPort *>::iterator iter2;
     for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
@@ -689,6 +728,7 @@ void PythonNode::executeLocal()
         DEBTRACE( "port kind: " << p->edGetType()->kind() );
         PyObject* ob=p->getPyObj();
         DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
+        unpxy << p->getName() << " = UnProxyObjectSimple( " << p->getName() << " )" << std::endl;
 #ifdef _DEVDEBUG_
         PyObject_Print(ob,stderr,Py_PRINT_RAW);
         cerr << endl;
@@ -701,47 +741,10 @@ void PythonNode::executeLocal()
 
     //calculation
     DEBTRACE( "----------------PyNode::calculation---------------" );
-    DEBTRACE(  _script );
-    DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
 
-    std::ostringstream stream;
-    stream << "/tmp/PythonNode_";
-    stream << getpid();
+    executeLocalInternal( unpxy.str() );
 
-    PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
-    if(code == 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 execution");
-      }
-    PyObject *res = PyEval_EvalCode(  code, _context, _context);
-
-    Py_DECREF(code);
-    Py_XDECREF(res);
-    DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
-    fflush(stdout);
-    fflush(stderr);
-    if(PyErr_Occurred ())
-      {
-        _errorDetails="";
-        PyObject* new_stderr = newPyStdOut(_errorDetails);
-        PySys_SetObject((char*)"stderr", new_stderr);
-        ofstream errorfile(stream.str().c_str());
-        if (errorfile.is_open())
-          {
-            errorfile << _script;
-            errorfile.close();
-          }
-        PyErr_Print();
-        PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
-        Py_DECREF(new_stderr);
-        throw Exception("Error during execution");
-      }
+    executeLocalInternal( _script );
 
     DEBTRACE( "-----------------PyNode::outputs-----------------" );
     list<OutputPort *>::iterator iter;
index 1c83f75820675c939acc526cbfccb75a54758305..e3c5e6a51cb8d8a961008307f50e35418b253a54 100644 (file)
@@ -107,6 +107,8 @@ namespace YACS
       void setSqueezeStatus(bool sqStatus) { _autoSqueeze=sqStatus; }
       bool getSqueezeStatus() const { return _autoSqueeze; }
       void squeezeMemorySafe();
+    private:
+      void executeLocalInternal(const std::string& codeStr);
     protected:
       void squeezeMemory();
       void squeezeMemoryRemote();
index 23701e50eb7c7a7e8352d3c1968d178071f8593a..e289a1f7eca339d5e9694928cffe0d195aecb2f1 100644 (file)
@@ -680,12 +680,10 @@ namespace YACS
       static inline double convert(const TypeCode *t,PyObject* o,void*)
         {
           double x;
-          if (PyFloat_Check(o))
-            x=PyFloat_AS_DOUBLE(o);
-          else if(PyLong_Check(o))
-            x=PyLong_AsLong(o);
-          else
+          x=PyFloat_AsDouble(o);
+          if( PyErr_Occurred() )
             {
+              PyErr_Restore(nullptr,nullptr,nullptr);
               stringstream msg;
               msg << "Not a python double. ";
 #ifdef _DEVDEBUG_
@@ -703,10 +701,10 @@ namespace YACS
       static inline long convert(const TypeCode *t,PyObject* o,void*)
         {
           long l;
-          if(PyLong_Check(o))
-            l=PyLong_AsLong(o);
-          else
+          l=PyLong_AsLong(o);
+          if( PyErr_Occurred() )
             {
+              PyErr_Restore(nullptr,nullptr,nullptr);
               stringstream msg;
               msg << "Not a python integer. ";
 #ifdef _DEVDEBUG_
index 7da065d851e3cb3751ec6cb6e29913dc3a382ac2..eda22e3da7858292b17e5077b6d2a19569f17066 100644 (file)
@@ -40,7 +40,7 @@
    </container>
    <bloc name="test/run">
       <remote name="testgenerate_conditions" elementaryWeight="-1">
-         <script><code><![CDATA[conditions_list=range(1000)
+         <script><code><![CDATA[conditions_list=list( range(1000) )
 ]]></code></script>
          <load container="default_container"/>
          <outport name="conditions_list" type="list[pyobj]"/>
index 5db0b5ea9b5241bd333d4b6b3a49b53a88f28108..5ecc8d2e6caeaa94a5e7e6171df810589567f6da 100755 (executable)
@@ -26,6 +26,7 @@ import loader
 import unittest
 import tempfile
 import os
+from SALOME_PyNode import UnProxyObjectSimple
 
 dir_test = tempfile.mkdtemp(suffix=".yacstest")
 
@@ -86,7 +87,7 @@ class TestEdit(unittest.TestCase):
       #executor.RunB(proc,0) # always use the "old" executor
       #executor.runWlm(proc,0) # always use the workload manager based executor
       reloaded_res_port = reloaded_proc.getChildByName("n2").getOutputPort("v")
-      self.assertEqual(reloaded_res_port.getPyObj(), 42)
+      self.assertEqual(UnProxyObjectSimple( reloaded_res_port.getPyObj() ), 42)
 
     def test2(self):
       """ Same as test1, but using the old executor instead of workload manager.
@@ -124,7 +125,7 @@ class TestEdit(unittest.TestCase):
       # you can also directly call the executor you wish, ignoring the property
       #executor.RunB(proc,0) # always use the "old" executor
       reloaded_res_port = reloaded_proc.getChildByName("n2").getOutputPort("v")
-      self.assertEqual(reloaded_res_port.getPyObj(), 42)
+      self.assertEqual(UnProxyObjectSimple( reloaded_res_port.getPyObj() ), 42)
 
 if __name__ == '__main__':
   file_test = os.path.join(dir_test,"UnitTestsResult")
index dac0bc758441873d148f8e992d4b656fe761f7cc..12778fa773736421f570cfa3d9eafe73e1eee0f1 100755 (executable)
@@ -1253,6 +1253,7 @@ for i in i8:
     pass
 
   def test15(self):
+    from SALOME_PyNode import UnProxyObjectSimple
     #fname=os.path.join(self.workdir, "BugInConcurrentLaunchDftCont.xml")
     p=self.r.createProc("pr")
     ti=p.createType("int","int")
@@ -1294,7 +1295,7 @@ for i in i8:
       ex = pilot.ExecutorSwig()
       self.assertEqual(p.getState(),pilot.READY)
       ex.RunW(p,0)
-      self.assertEqual(res.getPyObj(),6)
+      self.assertEqual(UnProxyObjectSimple( res.getPyObj() ),6)
       self.assertEqual(p.getState(),pilot.DONE)
     pass
 
@@ -1377,7 +1378,7 @@ for i in i8:
     fe0_end.setScript("""assert([elt[0] for elt in my_dpl_localization]==["FE0"])
 assert(my_dpl_localization[0][1]>=0 and my_dpl_localization[0][1]<4)""")
     n0=self.r.createScriptNode("Salome","n0") ; p.edAddChild(n0)
-    n0.setScript("o1=range(10)")
+    n0.setScript("o1=list(range(10))")
     a=n0.edAddOutputPort("o1",tdi)
     p.edAddLink(a,fe0.edGetSeqOfSamplesPort()) ; p.edAddCFLink(n0,fe0)
     # Level1
@@ -1385,7 +1386,7 @@ assert(my_dpl_localization[0][1]>=0 and my_dpl_localization[0][1]<4)""")
     n1=self.r.createScriptNode("Salome","n1") ; b0.edAddChild(n1)
     n1.setScript("""assert([elt[0] for elt in my_dpl_localization]==["FE0"])
 assert(my_dpl_localization[0][1]>=0 and my_dpl_localization[0][1]<4)
-o1=range(10)""")
+o1=list(range(10))""")
     b=n1.edAddOutputPort("o1",tdi)
     fe1=self.r.createForEachLoop("FE1",ti) ; b0.edAddChild(fe1)
     fe1.edGetNbOfBranchesPort().edInitInt(3)
@@ -1456,7 +1457,7 @@ assert(my_dpl_localization[0][1]>=0 and my_dpl_localization[0][1]<3)
     b0=self.r.createBloc("test26/main") ; p.edAddChild(b0)
     n0=self.r.createScriptNode("Salome","test26/n0") ; assignCont(n0,cont) # 1
     n0.setScript("""import os
-dd=range(10)""")
+dd=list( range(10) )""")
     dd=n0.edAddOutputPort("dd",sop) ; b0.edAddChild(n0)
     fe0=self.r.createForEachLoop("test26/FE0",po) ; b0.edAddChild(fe0)
     fe0.edGetNbOfBranchesPort().edInitInt(1) # very important for the test : 1 !