From: Anthony Geay Date: Wed, 7 Jun 2023 12:57:51 +0000 (+0200) Subject: [EDF27816] : fix all non regression tests X-Git-Tag: emc2p_1.4.0-rc1~16 X-Git-Url: http://git.salome-platform.org/gitweb/?p=modules%2Fyacs.git;a=commitdiff_plain;h=0a42afe2d73b6db2cac157b94a16f0df2a65acd7 [EDF27816] : fix all non regression tests --- diff --git a/src/py2yacs/Test/Py2yacsTest.cxx b/src/py2yacs/Test/Py2yacsTest.cxx index 59d8ab2ff..e95fe58cd 100644 --- a/src/py2yacs/Test/Py2yacsTest.cxx +++ b/src/py2yacs/Test/Py2yacsTest.cxx @@ -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(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 } diff --git a/src/py2yacs/Test/testDeco.py b/src/py2yacs/Test/testDeco.py index 4103f54d9..6f47f8cf3 100755 --- a/src/py2yacs/Test/testDeco.py +++ b/src/py2yacs/Test/testDeco.py @@ -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): diff --git a/src/py2yacs/Test/testforeach.py b/src/py2yacs/Test/testforeach.py index fd5b989aa..75f0c371f 100755 --- a/src/py2yacs/Test/testforeach.py +++ b/src/py2yacs/Test/testforeach.py @@ -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 diff --git a/src/runtime/PythonNode.cxx b/src/runtime/PythonNode.cxx index 0b7b65e60..fad8e96e3 100644 --- a/src/runtime/PythonNode.cxx +++ b/src/runtime/PythonNode.cxx @@ -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::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::iterator iter; diff --git a/src/runtime/PythonNode.hxx b/src/runtime/PythonNode.hxx index 1c83f7582..e3c5e6a51 100644 --- a/src/runtime/PythonNode.hxx +++ b/src/runtime/PythonNode.hxx @@ -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(); diff --git a/src/runtime/TypeConversions.cxx b/src/runtime/TypeConversions.cxx index 23701e50e..e289a1f7e 100644 --- a/src/runtime/TypeConversions.cxx +++ b/src/runtime/TypeConversions.cxx @@ -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_ diff --git a/src/yacsloader/samples/wlm_complex_foreach.xml b/src/yacsloader/samples/wlm_complex_foreach.xml index 7da065d85..eda22e3da 100644 --- a/src/yacsloader/samples/wlm_complex_foreach.xml +++ b/src/yacsloader/samples/wlm_complex_foreach.xml @@ -40,7 +40,7 @@ - diff --git a/src/yacsloader_swig/Test/testPynodeWithCache.py b/src/yacsloader_swig/Test/testPynodeWithCache.py index 5db0b5ea9..5ecc8d2e6 100755 --- a/src/yacsloader_swig/Test/testPynodeWithCache.py +++ b/src/yacsloader_swig/Test/testPynodeWithCache.py @@ -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") diff --git a/src/yacsloader_swig/Test/testSaveLoadRun.py b/src/yacsloader_swig/Test/testSaveLoadRun.py index dac0bc758..12778fa77 100755 --- a/src/yacsloader_swig/Test/testSaveLoadRun.py +++ b/src/yacsloader_swig/Test/testSaveLoadRun.py @@ -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 !