Skip to content

Commit

Permalink
Replace the OPM_EMBEDDED_MODULE macro by the one taken from pybind11/…
Browse files Browse the repository at this point in the history
…embed.h from newer pybind11 version
  • Loading branch information
lisajulia committed Apr 23, 2024
1 parent 69a1f2a commit b620f3d
Showing 1 changed file with 20 additions and 34 deletions.
54 changes: 20 additions & 34 deletions opm/input/eclipse/Python/EmbedModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,34 @@ It allows for slightly changing the python embedding without changing the pybind
#ifdef EMBEDDED_PYTHON
#include <pybind11/embed.h>

#if (PYBIND11_VERSION_MAJOR > 2 || (PYBIND11_VERSION_MAJOR == 2 && PYBIND11_VERSION_MINOR >= 6))
#define PYBIND11_INSTANCE_DEF(name) static pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name)
#define PYBIND11_INSTANCE_MODULE(name) pybind11::module_::create_extension_module(PYBIND11_TOSTRING(name), \
nullptr, \
&PYBIND11_CONCAT(pybind11_module_def_, name))
#else
#define PYBIND11_INSTANCE_DEF(name)
#define PYBIND11_INSTANCE_MODULE(name) pybind11::module(PYBIND11_TOSTRING(name))
#endif

#define OPM_EMBEDDED_MODULE(name, variable) \
PYBIND11_INSTANCE_DEF(name); \
static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \
static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
auto m = PYBIND11_INSTANCE_MODULE(name); \
try { \
PYBIND11_CONCAT(pybind11_init_, name)(m); \
return m.ptr(); \
} catch (pybind11::error_already_set &e) { \
PyErr_SetString(PyExc_ImportError, e.what()); \
return nullptr; \
} catch (const std::exception &e) { \
PyErr_SetString(PyExc_ImportError, e.what()); \
return nullptr; \
} \
} \
PYBIND11_EMBEDDED_MODULE_IMPL(name) \
Opm::embed::python_module name(PYBIND11_TOSTRING(name), \
PYBIND11_CONCAT(pybind11_init_impl_, name)); \
void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable)
#define OPM_EMBEDDED_MODULE(name, variable) \
static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name); \
static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \
static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
auto m = ::pybind11::module_::create_extension_module( \
PYBIND11_TOSTRING(name), nullptr, &PYBIND11_CONCAT(pybind11_module_def_, name)); \
try { \
PYBIND11_CONCAT(pybind11_init_, name)(m); \
return m.ptr(); \
} \
PYBIND11_CATCH_INIT_EXCEPTIONS \
} \
PYBIND11_EMBEDDED_MODULE_IMPL(name) \
Opm::embed::python_module PYBIND11_CONCAT(pybind11_module_, name)( \
PYBIND11_TOSTRING(name), PYBIND11_CONCAT(pybind11_init_impl_, name)); \
void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ \
& variable) // NOLINT(bugprone-macro-parentheses)

namespace Opm {
namespace embed {

/// Python 2.7/3.x compatible version of `PyImport_AppendInittab` and error checks.
struct python_module {
#if PY_MAJOR_VERSION >= 3
using init_t = PyObject *(*)();
#else
using init_t = void (*)();
#endif
python_module(const char *name, init_t init) {
if (Py_IsInitialized() != 0) {
//pybind11_fail("Can't add new modules after the interpreter has been initialized");
}

auto result = PyImport_AppendInittab(name, init);
if (result == -1)
Expand Down

0 comments on commit b620f3d

Please sign in to comment.