EmbedModule.hpp
Go to the documentation of this file.
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#define OPM_EMBEDDED_MODULE(name, variable) \
13 static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \
14 static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
15 auto m = pybind11::module(PYBIND11_TOSTRING(name)); \
16 try { \
17 PYBIND11_CONCAT(pybind11_init_, name)(m); \
18 return m.ptr(); \
19 } catch (pybind11::error_already_set &e) { \
20 PyErr_SetString(PyExc_ImportError, e.what()); \
21 return nullptr; \
22 } catch (const std::exception &e) { \
23 PyErr_SetString(PyExc_ImportError, e.what()); \
24 return nullptr; \
25 } \
26 } \
27 PYBIND11_EMBEDDED_MODULE_IMPL(name) \
28 Opm::embed::python_module name(PYBIND11_TOSTRING(name), \
29 PYBIND11_CONCAT(pybind11_init_impl_, name)); \
30 void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable)
31
32namespace Opm {
33namespace embed {
34
36struct python_module {
37#if PY_MAJOR_VERSION >= 3
38 using init_t = PyObject *(*)();
39#else
40 using init_t = void (*)();
41#endif
42 python_module(const char *name, init_t init) {
43
44 auto result = PyImport_AppendInittab(name, init);
45 if (result == -1)
46 pybind11::pybind11_fail("Insufficient memory to add a new module");
47 }
48};
49
50}
51}
52
53
54
55
56#endif
57
58#endif
const char *const name
Definition: cJSON.h:258
std::vector< double > init(const std::string &kewyord, const TableManager &tables, const Phases &phases, const std::vector< double > &cell_depth, const std::vector< int > &num, const std::vector< int > &endnum)
Definition: A.hpp:4