Merge pull request #515 from YosysHQ/python_improvements

Python code cleanup
This commit is contained in:
Miodrag Milanović 2020-11-14 17:17:25 +01:00 committed by GitHub
commit 15f35c19b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 33 deletions

View File

@ -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")) {

View File

@ -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));
py::initialize_interpreter();
py::module::import(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;
}
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
}

View File

@ -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();

View File

@ -96,10 +96,9 @@ void PythonTab::newContext(Context *ctx)
console->clear();
pyinterpreter_preinit();
init_python("nextpnr", true);
init_python("nextpnr");
pyinterpreter_initialize();
pyinterpreter_aquire();
init_python("nextpnr", false);
python_export_global("ctx", ctx);
pyinterpreter_release();