Skip to content

Commit

Permalink
Python code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mmicko committed Nov 14, 2020
1 parent 06555aa commit bb16fdb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 33 deletions.
2 changes: 1 addition & 1 deletion common/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
29 changes: 7 additions & 22 deletions common/pybindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "log.h"
#include "nextpnr.h"

#include <boost/filesystem.hpp>
#include <fstream>
#include <memory>
#include <signal.h>
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -272,40 +271,26 @@ 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);
if (program == NULL) {
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
}

void deinit_python()
{
#ifdef MAIN_EXECUTABLE
Py_Finalize();
py::finalize_interpreter();
PyMem_RawFree(program);
#endif
}
Expand Down
13 changes: 5 additions & 8 deletions common/pybindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <Python.h>
#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <stdexcept>
#include <utility>
#include "pycontainers.h"
Expand All @@ -35,27 +36,23 @@ 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;
std::terminate();
}
};

void init_python(const char *executable, bool first);
void init_python(const char *executable);

void deinit_python();

Expand Down
3 changes: 1 addition & 2 deletions gui/pythontab.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit bb16fdb

Please sign in to comment.