diff options
author | Miodrag Milanovic <mmicko@gmail.com> | 2020-11-14 16:34:12 +0100 |
---|---|---|
committer | Miodrag Milanovic <mmicko@gmail.com> | 2020-11-14 16:34:12 +0100 |
commit | bb16fdb4bba86ff54df177aaf8b624da8d6677d0 (patch) | |
tree | 26924d3a9c129004a1356d5a45cee0d6d6060cc3 /common | |
parent | 06555aa00327061423af120f1b017f9c37d0c051 (diff) | |
download | nextpnr-bb16fdb4bba86ff54df177aaf8b624da8d6677d0.tar.gz nextpnr-bb16fdb4bba86ff54df177aaf8b624da8d6677d0.tar.bz2 nextpnr-bb16fdb4bba86ff54df177aaf8b624da8d6677d0.zip |
Python code cleanup
Diffstat (limited to 'common')
-rw-r--r-- | common/command.cc | 2 | ||||
-rw-r--r-- | common/pybindings.cc | 29 | ||||
-rw-r--r-- | common/pybindings.h | 13 |
3 files changed, 13 insertions, 31 deletions
diff --git a/common/command.cc b/common/command.cc index 96ea8deb..a18612e4 100644 --- a/common/command.cc +++ b/common/command.cc @@ -321,7 +321,7 @@ int CommandHandler::executeMain(std::unique_ptr<Context> ctx) } #ifndef NO_PYTHON - init_python(argv[0], true); + init_python(argv[0]); python_export_global("ctx", *ctx); if (vm.count("run")) { diff --git a/common/pybindings.cc b/common/pybindings.cc index 0f5780b5..aacc8d9c 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -26,7 +26,6 @@ #include "log.h" #include "nextpnr.h" -#include <boost/filesystem.hpp> #include <fstream> #include <memory> #include <signal.h> @@ -84,7 +83,7 @@ template <> struct string_converter<Property> } // namespace PythonConversion -PYBIND11_MODULE(MODULE_NAME, m) +PYBIND11_EMBEDDED_MODULE(MODULE_NAME, m) { py::register_exception_translator([](std::exception_ptr p) { try { @@ -272,7 +271,7 @@ PYBIND11_MODULE(MODULE_NAME, m) static wchar_t *program; #endif -void init_python(const char *executable, bool first) +void init_python(const char *executable) { #ifdef MAIN_EXECUTABLE program = Py_DecodeLocale(executable, NULL); @@ -280,24 +279,10 @@ void init_python(const char *executable, bool first) fprintf(stderr, "Fatal error: cannot decode executable filename\n"); exit(1); } - try { - if (first) - PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME); - Py_SetProgramName(program); - Py_Initialize(); - - // Add cwd to Python's search path so `import` can be used in user scripts - boost::filesystem::path cwd = boost::filesystem::absolute("./").normalize(); - PyObject *sys_path = PySys_GetObject("path"); - PyList_Insert(sys_path, 0, PyUnicode_FromString(cwd.string().c_str())); - - PyImport_ImportModule(TOSTRING(MODULE_NAME)); - PyRun_SimpleString("from " TOSTRING(MODULE_NAME) " import *"); - } catch (py::error_already_set const &) { - // Parse and output the exception - std::string perror_str = parse_python_exception(); - std::cout << "Error in Python: " << perror_str << std::endl; - } + Py_SetProgramName(program); + py::initialize_interpreter(); + py::module::import(TOSTRING(MODULE_NAME)); + PyRun_SimpleString("from " TOSTRING(MODULE_NAME) " import *"); signal(SIGINT, SIG_DFL); #endif } @@ -305,7 +290,7 @@ void init_python(const char *executable, bool first) void deinit_python() { #ifdef MAIN_EXECUTABLE - Py_Finalize(); + py::finalize_interpreter(); PyMem_RawFree(program); #endif } 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(); |