From f5bfd557b625d1b5349c4a8305eccec30bafbe69 Mon Sep 17 00:00:00 2001 From: David Shah Date: Sun, 31 Mar 2019 12:34:26 +0100 Subject: python: Infrastructure for generic arch Python API Signed-off-by: David Shah --- common/pybindings.cc | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'common/pybindings.cc') diff --git a/common/pybindings.cc b/common/pybindings.cc index eee78b5e..dad8e5c7 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -81,6 +81,34 @@ template <> struct string_converter } // namespace PythonConversion +struct loc_from_tuple +{ + loc_from_tuple() { converter::registry::push_back(&convertible, &construct, boost::python::type_id()); } + + static void *convertible(PyObject *obj_ptr) + { + if (!PyTuple_Check(obj_ptr)) + return 0; + return obj_ptr; + } + + static void construct(PyObject *obj_ptr, converter::rvalue_from_python_stage1_data *data) + { + int val[3]; + for (int i = 0; i < 3; i++) { + PyObject *pyo = PyTuple_GetItem(obj_ptr, i); + if (!pyo) + throw_error_already_set(); + NPNR_ASSERT(PyLong_Check(pyo)); + val[i] = int(PyLong_AsLong(pyo)); + } + + void *storage = ((converter::rvalue_from_python_storage *)data)->storage.bytes; + new (storage) Loc(val[0], val[1], val[2]); + data->convertible = storage; + } +}; + BOOST_PYTHON_MODULE(MODULE_NAME) { register_exception_translator(&translate_assertfail); @@ -108,6 +136,9 @@ BOOST_PYTHON_MODULE(MODULE_NAME) class_("BaseCtx", no_init); + auto loc_cls = + class_("Loc").def_readwrite("x", &Loc::x).def_readwrite("y", &Loc::y).def_readwrite("z", &Loc::z); + auto ci_cls = class_>("CellInfo", no_init); readwrite_wrapper, conv_from_str>::def_wrap(ci_cls, "name"); -- cgit v1.2.3 From 30f0c582e4b809dd860d34c8c4c598a91374b029 Mon Sep 17 00:00:00 2001 From: David Shah Date: Sun, 31 Mar 2019 17:28:45 +0100 Subject: python: Named argument support Signed-off-by: David Shah --- common/pybindings.cc | 1 + 1 file changed, 1 insertion(+) (limited to 'common/pybindings.cc') diff --git a/common/pybindings.cc b/common/pybindings.cc index dad8e5c7..547d9b62 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -241,6 +241,7 @@ void init_python(const char *executable, bool first) Py_Initialize(); if (first) PyImport_ImportModule(TOSTRING(MODULE_NAME)); + PyRun_SimpleString("from " TOSTRING(MODULE_NAME) " import *"); } catch (boost::python::error_already_set const &) { // Parse and output the exception std::string perror_str = parse_python_exception(); -- cgit v1.2.3 From 50fd8aa01fde3426ff74fcf9b0126a24f279efca Mon Sep 17 00:00:00 2001 From: David Shah Date: Sun, 31 Mar 2019 17:54:52 +0100 Subject: generic: Place a single SLICE Signed-off-by: David Shah --- common/pybindings.cc | 35 +++++------------------------------ 1 file changed, 5 insertions(+), 30 deletions(-) (limited to 'common/pybindings.cc') diff --git a/common/pybindings.cc b/common/pybindings.cc index 547d9b62..bf5382eb 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -81,34 +81,6 @@ template <> struct string_converter } // namespace PythonConversion -struct loc_from_tuple -{ - loc_from_tuple() { converter::registry::push_back(&convertible, &construct, boost::python::type_id()); } - - static void *convertible(PyObject *obj_ptr) - { - if (!PyTuple_Check(obj_ptr)) - return 0; - return obj_ptr; - } - - static void construct(PyObject *obj_ptr, converter::rvalue_from_python_stage1_data *data) - { - int val[3]; - for (int i = 0; i < 3; i++) { - PyObject *pyo = PyTuple_GetItem(obj_ptr, i); - if (!pyo) - throw_error_already_set(); - NPNR_ASSERT(PyLong_Check(pyo)); - val[i] = int(PyLong_AsLong(pyo)); - } - - void *storage = ((converter::rvalue_from_python_storage *)data)->storage.bytes; - new (storage) Loc(val[0], val[1], val[2]); - data->convertible = storage; - } -}; - BOOST_PYTHON_MODULE(MODULE_NAME) { register_exception_translator(&translate_assertfail); @@ -136,8 +108,11 @@ BOOST_PYTHON_MODULE(MODULE_NAME) class_("BaseCtx", no_init); - auto loc_cls = - class_("Loc").def_readwrite("x", &Loc::x).def_readwrite("y", &Loc::y).def_readwrite("z", &Loc::z); + auto loc_cls = class_("Loc") + .def(init()) + .def_readwrite("x", &Loc::x) + .def_readwrite("y", &Loc::y) + .def_readwrite("z", &Loc::z); auto ci_cls = class_>("CellInfo", no_init); readwrite_wrapper, -- cgit v1.2.3 From 6fffe24177f9b99d6c332c18e343648cf33d4397 Mon Sep 17 00:00:00 2001 From: David Shah Date: Wed, 3 Apr 2019 16:08:33 +0100 Subject: generic: GUI Python bindings Signed-off-by: David Shah --- common/pybindings.cc | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'common/pybindings.cc') diff --git a/common/pybindings.cc b/common/pybindings.cc index bf5382eb..e1fc8534 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -87,7 +87,26 @@ BOOST_PYTHON_MODULE(MODULE_NAME) using namespace PythonConversion; + enum_("GraphicElementType") + .value("TYPE_NONE", GraphicElement::TYPE_NONE) + .value("TYPE_LINE", GraphicElement::TYPE_LINE) + .value("TYPE_ARROW", GraphicElement::TYPE_ARROW) + .value("TYPE_BOX", GraphicElement::TYPE_BOX) + .value("TYPE_CIRCLE", GraphicElement::TYPE_CIRCLE) + .value("TYPE_LABEL", GraphicElement::TYPE_LABEL) + .export_values(); + + enum_("GraphicElementStyle") + .value("STYLE_GRID", GraphicElement::STYLE_GRID) + .value("STYLE_FRAME", GraphicElement::STYLE_FRAME) + .value("STYLE_HIDDEN", GraphicElement::STYLE_HIDDEN) + .value("STYLE_INACTIVE", GraphicElement::STYLE_INACTIVE) + .value("STYLE_ACTIVE", GraphicElement::STYLE_ACTIVE) + .export_values(); + class_("GraphicElement") + .def(init( + (args("type"), "style", "x1", "y1", "x2", "y2", "z"))) .def_readwrite("type", &GraphicElement::type) .def_readwrite("x1", &GraphicElement::x1) .def_readwrite("y1", &GraphicElement::y1) @@ -214,8 +233,7 @@ void init_python(const char *executable, bool first) PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME); Py_SetProgramName(program); Py_Initialize(); - if (first) - PyImport_ImportModule(TOSTRING(MODULE_NAME)); + PyImport_ImportModule(TOSTRING(MODULE_NAME)); PyRun_SimpleString("from " TOSTRING(MODULE_NAME) " import *"); } catch (boost::python::error_already_set const &) { // Parse and output the exception -- cgit v1.2.3 From 90ceb829f347372975310d40d2bb6bade0352890 Mon Sep 17 00:00:00 2001 From: David Shah Date: Wed, 17 Apr 2019 10:57:19 +0100 Subject: pybindings: Fix use of import in user scripts Signed-off-by: David Shah --- common/pybindings.cc | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'common/pybindings.cc') diff --git a/common/pybindings.cc b/common/pybindings.cc index e1fc8534..f8f366cf 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -25,6 +25,7 @@ #include "jsonparse.h" #include "nextpnr.h" +#include #include #include #include @@ -233,6 +234,12 @@ void init_python(const char *executable, bool 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 (boost::python::error_already_set const &) { -- cgit v1.2.3 From 9fa13b5adcb4bfb193645fee0091c5c51c88c17b Mon Sep 17 00:00:00 2001 From: David Shah Date: Wed, 17 Apr 2019 11:12:58 +0100 Subject: pybindings: make errors in Python scripts stop nextpnr execution Signed-off-by: David Shah --- common/pybindings.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'common/pybindings.cc') diff --git a/common/pybindings.cc b/common/pybindings.cc index f8f366cf..60f87e27 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -23,6 +23,7 @@ #include "pybindings.h" #include "arch_pybindings.h" #include "jsonparse.h" +#include "log.h" #include "nextpnr.h" #include @@ -267,12 +268,15 @@ void execute_python_file(const char *python_file) fprintf(stderr, "Fatal error: file not found %s\n", python_file); exit(1); } - PyRun_SimpleFile(fp, python_file); + int result = PyRun_SimpleFile(fp, python_file); fclose(fp); + if (result == -1) { + log_error("Error occurred while executing Python script %s\n", python_file); + } } catch (boost::python::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; + log_error("Error in Python: %s\n", perror_str.c_str()); } } -- cgit v1.2.3