diff options
Diffstat (limited to 'common/pybindings.h')
-rw-r--r-- | common/pybindings.h | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/common/pybindings.h b/common/pybindings.h index d7429ed0..2645f8cd 100644 --- a/common/pybindings.h +++ b/common/pybindings.h @@ -24,6 +24,7 @@ #include <Python.h> #include <iostream> #include <pybind11/pybind11.h> +#include <pybind11/embed.h> #include <stdexcept> #include <utility> #include "pycontainers.h" @@ -35,19 +36,15 @@ NEXTPNR_NAMESPACE_BEGIN namespace py = pybind11; + std::string parse_python_exception(); template <typename Tn> void python_export_global(const char *name, Tn &x) { - PyObject *m, *d; - m = PyImport_AddModule("__main__"); - if (m == NULL) - return; - d = PyModule_GetDict(m); try { py::object obj = py::cast(x, py::return_value_policy::reference); - PyDict_SetItemString(d, name, obj.ptr()); - } catch (py::error_already_set const &) { + py::module::import("__main__").attr(name) = obj.ptr(); + } catch (pybind11::error_already_set &) { // Parse and output the exception std::string perror_str = parse_python_exception(); std::cout << "Error in Python: " << perror_str << std::endl; @@ -55,7 +52,7 @@ template <typename Tn> void python_export_global(const char *name, Tn &x) } }; -void init_python(const char *executable, bool first); +void init_python(const char *executable); void deinit_python(); |