]> SALOME platform Git repositories - tools/ydefx.git/commitdiff
Salome HOME
Deal with exceptions with no message.
authorOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Tue, 10 Jan 2023 15:57:21 +0000 (16:57 +0100)
committerOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Tue, 10 Jan 2023 15:57:21 +0000 (16:57 +0100)
src/cpp/Test/StudyGeneralTest.cxx
src/cpp/Test/StudyGeneralTest.hxx
src/pydefx/plugins/pointeval.py
src/pydefx/schemas/idefix_pyschema.xml

index 67f5ceedbdc6c598b927ea5dc93654407847d6eb..d73d5cc2cb593f2dd72d788bea6af189b0bc952f 100644 (file)
@@ -203,5 +203,64 @@ void SampleTest::genericStudy()
     CPPUNIT_ASSERT(l.lastError().find("SyntaxError") != std::string::npos);
 }
 
+void SampleTest::emptyError()
+/// Test the case of an error with an empty description.
+{
+    std::list<std::string> resources = ydefx::JobParametersProxy::AvailableResources();
+    CPPUNIT_ASSERT(resources.size() > 0);
+
+    ydefx::JobParametersProxy jobParams;
+    jobParams.configureResource("localhost");
+    std::time_t t = std::time(nullptr);
+    std::tm tm = *std::localtime(&t);
+    std::stringstream ss;
+    ss << jobParams.work_directory() << "/GenericTest"
+       << std::put_time(&tm, "%m%d%H%M%S");
+    jobParams.work_directory(ss.str());
+    jobParams.createResultDirectory("/tmp");
+    std::string pyScript =
+"def _exec(x):\n"
+"  if x==0:\n"
+"    raise RuntimeError("")\n" // error with an empty description
+"  y = x * x\n"
+"  return y\n";
+
+    ydefx::PyStudyFunction studyFunction;
+    studyFunction.loadString(pyScript);
+    CPPUNIT_ASSERT(studyFunction.isValid());
+
+    ydefx::Sample<double, py2cpp::PyPtr > sample;
+    // set default value for not computed and failed points
+    sample.outputs<double>().setDefault(std::nan(""));
+    std::vector<double> x_vals = {0., 1., 2., 3.};
+    sample.inputs<double>().set("x", x_vals);
+    sample.outputs<double>().addName("y");
+
+    py2cpp::PyFunction objConstructor;
+    objConstructor.loadExp("pydefx", "PyStudy");
+    py2cpp::PyPtr pyStudy = objConstructor();
+
+    ydefx::Launcher l;
+    ydefx::Job* myJob = l.submitPyStudyJob(pyStudy, studyFunction, sample, jobParams);
+    CPPUNIT_ASSERT(myJob);
+    bool ok = myJob->wait();
+    CPPUNIT_ASSERT(ok);
+    CPPUNIT_ASSERT(myJob->lastError().empty());
+    std::string jobState = myJob->state();
+    CPPUNIT_ASSERT(jobState == "FINISHED");
+    ok = myJob->fetch();
+    CPPUNIT_ASSERT(ok);
+    CPPUNIT_ASSERT(myJob->lastError().empty());
+    CPPUNIT_ASSERT(sample.pointState(0) == ydefx::ExecutionState::ERROR);
+    const std::vector<double>& result = sample.outputs<double>().get("y");
+    CPPUNIT_ASSERT(std::isnan(result[0]));
+    CPPUNIT_ASSERT(1. == result[1]);
+    CPPUNIT_ASSERT(4. == result[2]);
+    CPPUNIT_ASSERT(9. == result[3]);
+    CPPUNIT_ASSERT(!sample.getError(0).empty());
+    delete myJob;
+}
+
+
 CPPUNIT_TEST_SUITE_REGISTRATION( SampleTest );
 #include "PyTestMain.cxx"
index 0542e99aeac01fb19d9f3fb525d4f96265551789..bf684c7037db819a5c2c27552ae048085d41a1b0 100644 (file)
@@ -27,6 +27,7 @@ class SampleTest: public CppUnit::TestFixture
   CPPUNIT_TEST_SUITE(SampleTest);
   CPPUNIT_TEST(fullStudy);
   CPPUNIT_TEST(genericStudy);
+  CPPUNIT_TEST(emptyError);
   CPPUNIT_TEST_SUITE_END();
 public:
   void setUp();
@@ -34,6 +35,7 @@ public:
   void cleanUp();
   void fullStudy();
   void genericStudy();
+  void emptyError();
 };
 
 #endif // YDEFX_SAMPLETEST_HXX
index c652b269dd5de8e1ce368eca1d467251424aec2f..daa190769b0eeeaf87819afe7f6715fc9c206529 100644 (file)
@@ -24,6 +24,8 @@ try:
   result = _exec(**inputvals)
 except Exception as e:
   error=str(e)
+  if not error :
+    error = "Exception " + repr(e)
   os.chdir(old_dir) # back to the current case job directory
   with open(traceback_result, "w") as f:
     traceback.print_exc(file=f)
index 19dd54c393045cf14ec257b620eb2f5e75bfb8ff..407d13d60b7b3032c94ae2c8e0dbdfa79aae6cdd 100644 (file)
@@ -45,7 +45,9 @@ result=None
 try:
   result=idefixstudy._exec(**inputvals)
 except Exception as e:
-  error=str(e) 
+  error=str(e)
+  if not error :
+    error = "Exception " + repr(e)
   traceback.print_exc()
 o0=pickle.dumps((error, result), protocol=0).decode()
 ]]></code></script>