diff options
author | Miodrag Milanović <mmicko@gmail.com> | 2020-11-14 17:17:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-14 17:17:25 +0100 |
commit | 15f35c19b86e14e6fec97ee152232429ba0cba22 (patch) | |
tree | 26924d3a9c129004a1356d5a45cee0d6d6060cc3 /common/pybindings.h | |
parent | 06555aa00327061423af120f1b017f9c37d0c051 (diff) | |
parent | bb16fdb4bba86ff54df177aaf8b624da8d6677d0 (diff) | |
download | nextpnr-15f35c19b86e14e6fec97ee152232429ba0cba22.tar.gz nextpnr-15f35c19b86e14e6fec97ee152232429ba0cba22.tar.bz2 nextpnr-15f35c19b86e14e6fec97ee152232429ba0cba22.zip |
Merge pull request #515 from YosysHQ/python_improvements
Python code cleanup
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(); |