Skip to content

Commit

Permalink
Display a proper error message in case import("opm_embedded") fails i…
Browse files Browse the repository at this point in the history
…n the class PythonInterp
  • Loading branch information
lisajulia committed Apr 30, 2024
1 parent b4024fd commit 7937503
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions opm/input/eclipse/Python/PythonInterp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>
#include <fmt/format.h>

#include <opm/common/ErrorMacros.hpp>
#include <opm/common/OpmLog/OpmLog.hpp>

#include <opm/input/eclipse/Deck/Deck.hpp>
#include <opm/input/eclipse/Parser/Parser.hpp>
Expand Down Expand Up @@ -70,7 +74,15 @@ bool PythonInterp::exec(const std::string& python_code, const Parser& parser, De
if (!this->guard)
throw std::logic_error("Python interpreter not enabled");

auto context = py::module::import("opm_embedded");
py::module context;
try {
context = py::module::import("opm_embedded");
} catch (const std::exception& e) {
OpmLog::error(fmt::format("Exception thrown when loading Python module opm_embedded: {}", e.what()));
throw e;
} catch (...) {
OPM_THROW(std::runtime_error, "General exception thrown when loading Python module opm_embedded!");
}
context.attr("deck") = &deck;
context.attr("parser") = &parser;
return this->exec(python_code, context);
Expand All @@ -92,7 +104,15 @@ bool PythonInterp::exec(const std::string& python_code) {
if (!this->guard)
throw std::logic_error("Python interpreter not enabled");

auto context = py::module::import("opm_embedded");
py::module context;
try {
context = py::module::import("opm_embedded");
} catch (const std::exception& e) {
OpmLog::error(fmt::format("Exception thrown when loading Python module opm_embedded: {}", e.what()));
throw e;
} catch (...) {
OPM_THROW(std::runtime_error, "General exception thrown when loading Python module opm_embedded!");
}
return this->exec(python_code, context);
}

Expand Down

0 comments on commit 7937503

Please sign in to comment.