From 8845fc41c69ed42d18fdf3c5aa2a32571e0ccaa9 Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Tue, 10 Jan 2023 16:57:21 +0100 Subject: [PATCH] Deal with exceptions with no message. --- src/cpp/Test/StudyGeneralTest.cxx | 59 ++++++++++++++++++++++++++ src/cpp/Test/StudyGeneralTest.hxx | 2 + src/pydefx/plugins/pointeval.py | 2 + src/pydefx/schemas/idefix_pyschema.xml | 4 +- 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/cpp/Test/StudyGeneralTest.cxx b/src/cpp/Test/StudyGeneralTest.cxx index 67f5cee..d73d5cc 100644 --- a/src/cpp/Test/StudyGeneralTest.cxx +++ b/src/cpp/Test/StudyGeneralTest.cxx @@ -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 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 sample; + // set default value for not computed and failed points + sample.outputs().setDefault(std::nan("")); + std::vector x_vals = {0., 1., 2., 3.}; + sample.inputs().set("x", x_vals); + sample.outputs().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& result = sample.outputs().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" diff --git a/src/cpp/Test/StudyGeneralTest.hxx b/src/cpp/Test/StudyGeneralTest.hxx index 0542e99..bf684c7 100644 --- a/src/cpp/Test/StudyGeneralTest.hxx +++ b/src/cpp/Test/StudyGeneralTest.hxx @@ -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 diff --git a/src/pydefx/plugins/pointeval.py b/src/pydefx/plugins/pointeval.py index c652b26..daa1907 100644 --- a/src/pydefx/plugins/pointeval.py +++ b/src/pydefx/plugins/pointeval.py @@ -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) diff --git a/src/pydefx/schemas/idefix_pyschema.xml b/src/pydefx/schemas/idefix_pyschema.xml index 19dd54c..407d13d 100644 --- a/src/pydefx/schemas/idefix_pyschema.xml +++ b/src/pydefx/schemas/idefix_pyschema.xml @@ -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() ]]> -- 2.39.2