My Project
Loading...
Searching...
No Matches
EmbedModule.hpp
1/*
2This Code is a copy paste of part of the contents of pybind11/embed.h
3It allows for slightly changing the python embedding without changing the pybind11 sourcecode.
4*/
5
6#ifndef OPM_EMBED_MODULE
7#define OPM_EMBED_MODULE
8
9#ifdef EMBEDDED_PYTHON
10#include <pybind11/embed.h>
11
12#if (PYBIND11_VERSION_MAJOR > 2 || (PYBIND11_VERSION_MAJOR == 2 && PYBIND11_VERSION_MINOR >= 6))
13#define PYBIND11_INSTANCE_DEF(name) static pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name)
14#define PYBIND11_INSTANCE_MODULE(name) pybind11::module_::create_extension_module(PYBIND11_TOSTRING(name), \
15 nullptr, \
16 &PYBIND11_CONCAT(pybind11_module_def_, name))
17#else
18#define PYBIND11_INSTANCE_DEF(name)
19#define PYBIND11_INSTANCE_MODULE(name) pybind11::module(PYBIND11_TOSTRING(name))
20#endif
21
22#define OPM_EMBEDDED_MODULE(name, variable) \
23 PYBIND11_INSTANCE_DEF(name); \
24 static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \
25 static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
26 auto m = PYBIND11_INSTANCE_MODULE(name); \
27 try { \
28 PYBIND11_CONCAT(pybind11_init_, name)(m); \
29 return m.ptr(); \
30 } catch (pybind11::error_already_set &e) { \
31 PyErr_SetString(PyExc_ImportError, e.what()); \
32 return nullptr; \
33 } catch (const std::exception &e) { \
34 PyErr_SetString(PyExc_ImportError, e.what()); \
35 return nullptr; \
36 } \
37 } \
38 PYBIND11_EMBEDDED_MODULE_IMPL(name) \
39 Opm::embed::python_module name(PYBIND11_TOSTRING(name), \
40 PYBIND11_CONCAT(pybind11_init_impl_, name)); \
41 void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable)
42
43namespace Opm {
44namespace embed {
45
47struct python_module {
48#if PY_MAJOR_VERSION >= 3
49 using init_t = PyObject *(*)();
50#else
51 using init_t = void (*)();
52#endif
53 python_module(const char *name, init_t init) {
54
55 auto result = PyImport_AppendInittab(name, init);
56 if (result == -1)
57 pybind11::pybind11_fail("Insufficient memory to add a new module");
58 }
59};
60
61}
62}
63
64
65
66
67#endif
68
69#endif
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30