diff --git a/CMakeLists.txt b/CMakeLists.txt index b50ace459..707d17b77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,7 +113,6 @@ macro (sources_hook) list(APPEND opm-common_SOURCES ${PROJECT_BINARY_DIR}/ParserKeywords.cpp) endif() set_source_files_properties(src/opm/parser/eclipse/Python/Python.cpp - src/opm/parser/eclipse/Python/PythonInterp.cpp PROPERTIES COMPILE_FLAGS -Wno-shadow) endmacro (sources_hook) @@ -236,7 +235,8 @@ if (OPM_ENABLE_PYTHON) string(REPLACE ";" ":" _setup_lib_dirs "${_opmcommon_lib_dirs}") add_custom_command(OUTPUT python/python/opm/libopmcommon_python.so - DEPENDS python/cxx/connection.cpp + DEPENDS + python/cxx/connection.cpp python/cxx/converters.hpp python/cxx/deck.cpp python/cxx/deck_keyword.cpp diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index fb10c1716..967329358 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -1,5 +1,5 @@ # This file sets up five lists: -# MAIN_SOURCE_FILES List of compilation units which will be included in +# MAIN_SOURCE_FILES List of compilation units which will be included in # the library. If it isn't on this list, it won't be # part of the library. Please try to keep it sorted to # maintain sanity. @@ -47,7 +47,6 @@ if(ENABLE_ECL_INPUT) src/opm/parser/eclipse/Deck/Section.cpp src/opm/parser/eclipse/Deck/UDAValue.cpp src/opm/parser/eclipse/Python/Python.cpp - src/opm/parser/eclipse/Python/PythonInterp.cpp src/opm/parser/eclipse/EclipseState/AquiferCT.cpp src/opm/parser/eclipse/EclipseState/Aquifetp.cpp src/opm/parser/eclipse/EclipseState/Aquancon.cpp @@ -169,6 +168,32 @@ if(ENABLE_ECL_INPUT) src/opm/parser/eclipse/Utility/Stringview.cpp ) + if (OPM_ENABLE_EMBEDDED_PYTHON) + list( APPEND PYTHON_SOURCE_FILES + src/opm/parser/eclipse/Python/PythonInterp.cpp + python/cxx/connection.cpp + python/cxx/deck.cpp + python/cxx/deck_keyword.cpp + python/cxx/eclipse_3d_properties.cpp + python/cxx/eclipse_config.cpp + python/cxx/eclipse_grid.cpp + python/cxx/eclipse_state.cpp + python/cxx/group.cpp + python/cxx/parsecontext.cpp + python/cxx/parser.cpp + python/cxx/schedule.cpp + python/cxx/export.cpp + python/cxx/common_state.cpp + python/cxx/table_manager.cpp + python/cxx/well.cpp + python/cxx/log.cpp + ) + set_source_files_properties(${PYTHON_SOURCE_FILES} PROPERTIES COMPILE_FLAGS -Wno-shadow) + list( APPEND MAIN_SOURCE_FILES ${PYTHON_SOURCE_FILES}) + endif() + + + if(NOT cjson_FOUND) list(APPEND MAIN_SOURCE_FILES external/cjson/cJSON.c) endif() diff --git a/opm/parser/eclipse/Python/Python.hpp b/opm/parser/eclipse/Python/Python.hpp index 3f6545389..f33890e02 100644 --- a/opm/parser/eclipse/Python/Python.hpp +++ b/opm/parser/eclipse/Python/Python.hpp @@ -26,7 +26,8 @@ namespace Opm { class PythonInterp; - +class Parser; +class Deck; /* @@ -53,7 +54,8 @@ class PythonInterp; class Python { public: Python(); - bool exec(const std::string& python_code); + bool exec(const std::string& python_code) const; + bool exec(const std::string& python_code, const Parser& parser, Deck& deck) const; explicit operator bool() const; private: std::shared_ptr interp; diff --git a/python/cxx/parser.cpp b/python/cxx/parser.cpp index c89dac327..2bd084af6 100644 --- a/python/cxx/parser.cpp +++ b/python/cxx/parser.cpp @@ -47,7 +47,7 @@ namespace { parser->addParserKeyword(keyword); } - + } diff --git a/src/opm/parser/eclipse/Python/Python.cpp b/src/opm/parser/eclipse/Python/Python.cpp index 2e2a3d73c..5d7e75c67 100644 --- a/src/opm/parser/eclipse/Python/Python.cpp +++ b/src/opm/parser/eclipse/Python/Python.cpp @@ -29,12 +29,20 @@ Python::Python(): } -bool Python::exec(const std::string& python_code) { +bool Python::exec(const std::string& python_code) const { this->interp->exec(python_code); return true; } + +bool Python::exec(const std::string& python_code, const Parser& parser, Deck& deck) const { + this->interp->exec(python_code, parser, deck); + return true; +} + + + Python::operator bool() const { if (this->interp) return bool( *this->interp ); @@ -42,4 +50,5 @@ Python::operator bool() const { return false; } + } diff --git a/src/opm/parser/eclipse/Python/PythonInterp.cpp b/src/opm/parser/eclipse/Python/PythonInterp.cpp index 1f9bfdaa9..7366ea256 100644 --- a/src/opm/parser/eclipse/Python/PythonInterp.cpp +++ b/src/opm/parser/eclipse/Python/PythonInterp.cpp @@ -17,19 +17,36 @@ along with OPM. If not, see . */ -#include "PythonInterp.hpp" #ifdef EMBEDDED_PYTHON - #include -namespace py = pybind11; +#include +#include +#include + +#include "python/cxx/export.hpp" +#include "PythonInterp.hpp" + +namespace py = pybind11; namespace Opm { +PYBIND11_EMBEDDED_MODULE(context, module) { + python::common::export_all(module); +} + + +bool PythonInterp::exec(const std::string& python_code, const Parser& parser, Deck& deck) { + auto context = py::module::import("context"); + context.attr("deck") = &deck; + context.attr("parser") = &parser; + py::exec(python_code, py::globals(), py::dict(py::arg("context") = context)); + return true; +} + bool PythonInterp::exec(const std::string& python_code) { - py::object scope = py::module::import("__main__").attr("__dict__"); - py::exec(python_code, scope); + py::exec(python_code, py::globals()); return true; } diff --git a/src/opm/parser/eclipse/Python/PythonInterp.hpp b/src/opm/parser/eclipse/Python/PythonInterp.hpp index a14978ff6..de486bec0 100644 --- a/src/opm/parser/eclipse/Python/PythonInterp.hpp +++ b/src/opm/parser/eclipse/Python/PythonInterp.hpp @@ -36,10 +36,14 @@ namespace py = pybind11; namespace Opm { #ifdef EMBEDDED_PYTHON +class Parser; +class Deck; + class __attribute__ ((visibility("hidden"))) PythonInterp { public: bool exec(const std::string& python_code); + bool exec(const std::string& python_code, const Parser& parser, Deck& deck); explicit operator bool() const { return true; } private: py::scoped_interpreter guard = {}; @@ -54,6 +58,10 @@ public: return this->fail(); }; + bool exec(const std::string& python_code, const Parser& parser, Deck& deck) { + return this->fail(); + } + explicit operator bool() const { return false; } private: bool fail() { throw std::logic_error("The current opm code has been built without Python support;"); } diff --git a/tests/parser/EmbeddedPython.cpp b/tests/parser/EmbeddedPython.cpp index e7e31a58a..537a4fb9c 100644 --- a/tests/parser/EmbeddedPython.cpp +++ b/tests/parser/EmbeddedPython.cpp @@ -26,6 +26,8 @@ #include #include +#include +#include #ifndef EMBEDDED_PYTHON @@ -41,6 +43,14 @@ BOOST_AUTO_TEST_CASE(INSTANTIATE) { Opm::Python python; BOOST_CHECK(python); BOOST_CHECK_NO_THROW(python.exec("print('Hello world')")); + + Opm::Parser parser; + Opm::Deck deck; + std::string python_code = R"( +print('Parser: {}'.format(context.parser)) +print('Deck: {}'.format(context.deck)) +)"; + BOOST_CHECK_NO_THROW( python.exec(python_code, parser, deck)); } #endif