Skip to content

Commit

Permalink
Do not create an opm_embedded library as embedded anymore, this is au…
Browse files Browse the repository at this point in the history
…tomatically taken from the globally installed python

There are two reasons for this change:
- Avoid a duplicate module creation
- The code in OPM_EMBEDDED is a copy of PYBIND11_EMBEDDED_MODULE - without the following check:
	if (Py_IsInitialized() != 0) {
            //pybind11_fail("Can't add new modules after the interpreter has been initialized");
        }
  This is causing problems for Python 3.12 and is likely to cause further problems in the future
  • Loading branch information
lisajulia committed Apr 29, 2024
1 parent fc533a1 commit 987e677
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 89 deletions.
17 changes: 12 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ if (OPM_ENABLE_PYTHON)
# setup.py install manually - optionally with the generated script
# setup-install.sh - and completely bypass cmake in the installation phase.
# If OPM_ENABLE_EMBEDDED_PYTHON is enabled, we also install opmcommon_python,
# to make it available in a Python console.
# to make it available in a Python console and for e.g. opm-simulators
if (OPM_INSTALL_PYTHON OR OPM_ENABLE_EMBEDDED_PYTHON)
include(PyInstallPrefix)
install(TARGETS opmcommon_python DESTINATION ${DEST_PREFIX}${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_PREFIX}/opm)
Expand All @@ -442,11 +442,18 @@ if (OPM_ENABLE_PYTHON)
python/install.py
${PROJECT_BINARY_DIR}/python/opm
${DEST_PREFIX}${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_PREFIX} 1)")

## Need to install this Python script such that it can be used by opm-simulators when building against an installed
## opm-common
install( PROGRAMS "python/install.py" DESTINATION "${OPM_PYTHON_COMMON_DIR}" )
endif()
if (OPM_ENABLE_EMBEDDED_PYTHON)
install(
CODE "execute_process(
COMMAND ${PYTHON_EXECUTABLE}
python/install.py
${PROJECT_BINARY_DIR}/python/opm_embedded
${DEST_PREFIX}${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_PREFIX} 1)")
endif()
## Need to install this Python script such that it can be used by opm-simulators when building against an installed
## opm-common
install( PROGRAMS "python/install.py" DESTINATION "${OPM_PYTHON_COMMON_DIR}" )
endif()

# Observe that if the opmcommon library has been built as a shared library the
Expand Down
69 changes: 0 additions & 69 deletions opm/input/eclipse/Python/EmbedModule.hpp

This file was deleted.

51 changes: 36 additions & 15 deletions opm/input/eclipse/Python/PythonInterp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,22 @@

#include "python/cxx/export.hpp"
#include "PythonInterp.hpp"
#include "EmbedModule.hpp"

#include <iostream>
namespace py = pybind11;
namespace Opm {


/*
OPM_EMBEDDED_MODULE create a Python of all the Python/C++ classes which are
generated in the python::common::export_all_opm_embedded() function in the wrapping code.
The same module is created as a Pybind 11 module in export.cpp
*/

OPM_EMBEDDED_MODULE(opm_embedded, module) {
python::common::export_all(module);
}


/**
* @brief Executes Python code within a specified context.
*
* This function executes the provided Python code within the given context.
*
* @param python_code The Python code to execute, provided as a string.
* @param context The Python module providing the context for execution.
*
* @return bool The return value set at the "result" attribute in the context module.
*
*/
bool PythonInterp::exec(const std::string& python_code, py::module& context) {
py::bool_ def_result = false;
context.attr("result") = &def_result;
Expand All @@ -55,7 +54,19 @@ bool PythonInterp::exec(const std::string& python_code, py::module& context) {
}



/**
* @brief Executes Python code within a specified context.
*
* This function imports the module "opm_embedded" (initialized at opm-common/python/opm_embeded/__init__.py)
* as the context module, adds the parser and deck to the context and executes the given Python code.
*
* @param python_code The Python code to execute, provided as a string.
* @param parser The parser.
* @param deck The current deck.
*
* @return bool The return value set at the "result" attribute in the context module.
*
*/
bool PythonInterp::exec(const std::string& python_code, const Parser& parser, Deck& deck) {
if (!this->guard)
throw std::logic_error("Python interpreter not enabled");
Expand All @@ -67,7 +78,17 @@ bool PythonInterp::exec(const std::string& python_code, const Parser& parser, De
}



/**
* @brief Executes Python code within a specified context.
*
* This function imports the module "opm_embedded" (initialized at opm-common/python/opm_embeded/__init__.py)
* as the context module and executes the given Python code.
*
* @param python_code The Python code to execute, provided as a string.
*
* @return bool The return value set at the "result" attribute in the context module.
*
*/
bool PythonInterp::exec(const std::string& python_code) {
if (!this->guard)
throw std::logic_error("Python interpreter not enabled");
Expand Down

0 comments on commit 987e677

Please sign in to comment.