From bdd93135823ae1ef293d4be3eb50d0ca6bdeee9e Mon Sep 17 00:00:00 2001 From: David Shah Date: Thu, 7 Jun 2018 08:56:54 +0200 Subject: Allow loading and running Python files before GUI starts Signed-off-by: David Shah --- common/pybindings.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'common/pybindings.h') diff --git a/common/pybindings.h b/common/pybindings.h index f594784c..84280908 100644 --- a/common/pybindings.h +++ b/common/pybindings.h @@ -26,7 +26,6 @@ #include #include #include - using namespace boost::python; /* A wrapper for a Pythonised nextpnr Iterator. The actual class wrapped is a @@ -79,7 +78,10 @@ struct range_wrapper { #define WRAP_RANGE(t) range_wrapper().wrap(#t "Range", #t "Iterator") -void execute_python_file(const char *executable, const char* python_file); +void init_python(const char *executable); +void deinit_python(); + +void execute_python_file(const char* python_file); std::string parse_python_exception(); void arch_appendinittab(); -- cgit v1.2.3 From 3769b2058019ce7c6be824085e1047e1beec07dc Mon Sep 17 00:00:00 2001 From: David Shah Date: Thu, 7 Jun 2018 09:47:00 +0200 Subject: Adding Python to/from string wrappers for internal IDs --- common/pybindings.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'common/pybindings.h') diff --git a/common/pybindings.h b/common/pybindings.h index 84280908..5b2adc6a 100644 --- a/common/pybindings.h +++ b/common/pybindings.h @@ -76,6 +76,54 @@ struct range_wrapper { } }; +/* +A wrapper to enable custom type/ID to/from string conversions + */ +template struct string_wrapper { + template + struct from_pystring_converter { + from_pystring_converter() { + converter::registry::push_back( + &convertible, + &construct, + boost::python::type_id()); + }; + + static void* convertible(PyObject* object) { + return PyUnicode_Check(object) ? object : 0; + } + + static void construct( + PyObject* object, + converter::rvalue_from_python_stage1_data* data) { + const wchar_t* value = PyUnicode_AsUnicode(object); + const std::wstring value_ws(value); + if (value == 0) throw_error_already_set(); + void* storage = ( + (boost::python::converter::rvalue_from_python_storage*) + data)->storage.bytes; + new (storage) T(fn(std::string(value_ws.begin(), value_ws.end()))); + data->convertible = storage; + } + + static F fn; + }; + + template struct to_str_wrapper { + static F fn; + std::string str(T& x) { + return fn(x); + } + }; + + template static void wrap(const char *type_name, F1 to_str_fn, F2 from_str_fn) { + from_pystring_converter::fn = from_str_fn; + from_pystring_converter(); + to_str_wrapper::fn = to_str_fn; + class_(type_name, no_init).def("__str__", to_str_wrapper::str); + }; +}; + #define WRAP_RANGE(t) range_wrapper().wrap(#t "Range", #t "Iterator") void init_python(const char *executable); -- cgit v1.2.3 From e576f71838290ff12b98145fb02af84563fc03c1 Mon Sep 17 00:00:00 2001 From: David Shah Date: Thu, 7 Jun 2018 11:41:54 +0200 Subject: Developing Python bindings for Design and related types --- common/pybindings.h | 52 +++------------------------------------------------- 1 file changed, 3 insertions(+), 49 deletions(-) (limited to 'common/pybindings.h') diff --git a/common/pybindings.h b/common/pybindings.h index 5b2adc6a..3dfadeee 100644 --- a/common/pybindings.h +++ b/common/pybindings.h @@ -20,6 +20,9 @@ #ifndef COMMON_PYBINDINGS_H #define COMMON_PYBINDINGS_H + +#include "pycontainers.h" + #include #include #include @@ -27,54 +30,6 @@ #include #include using namespace boost::python; -/* -A wrapper for a Pythonised nextpnr Iterator. The actual class wrapped is a -pair containing (current, end) -*/ - -template -struct iterator_wrapper { - typedef decltype(*(std::declval())) value_t; - - static value_t next(std::pair &iter) { - if (iter.first != iter.second) { - value_t val = *iter.first; - ++iter.first; - return val; - } else { - PyErr_SetString(PyExc_StopIteration, "End of range reached"); - boost::python::throw_error_already_set(); - // Should be unreachable, but prevent control may reach end of non-void - throw std::runtime_error("unreachable"); - } - } - - static void wrap(const char *python_name) { - class_>(python_name, no_init) - .def("__next__", next); - } -}; - -/* -A wrapper for a nextpnr Range. Ranges should have two functions, begin() -and end() which return iterator-like objects supporting ++, * and != -Full STL iterator semantics are not required, unlike the standard Boost wrappers -*/ - -template -struct range_wrapper { - typedef decltype(std::declval().begin()) iterator_t; - - static std::pair iter(T &range) { - return std::make_pair(range.begin(), range.end()); - } - - static void wrap(const char *range_name, const char *iter_name) { - class_(range_name, no_init) - .def("__iter__", iter); - iterator_wrapper().wrap(iter_name); - } -}; /* A wrapper to enable custom type/ID to/from string conversions @@ -124,7 +79,6 @@ template struct string_wrapper { }; }; -#define WRAP_RANGE(t) range_wrapper().wrap(#t "Range", #t "Iterator") void init_python(const char *executable); void deinit_python(); -- cgit v1.2.3 From a5249da02d2a55d4b838dd8c29e159513dd71418 Mon Sep 17 00:00:00 2001 From: David Shah Date: Thu, 7 Jun 2018 12:40:31 +0200 Subject: Working on global Python design object Signed-off-by: David Shah --- common/pybindings.h | 53 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 13 deletions(-) (limited to 'common/pybindings.h') diff --git a/common/pybindings.h b/common/pybindings.h index 3dfadeee..44898198 100644 --- a/common/pybindings.h +++ b/common/pybindings.h @@ -29,12 +29,14 @@ #include #include #include +#include using namespace boost::python; /* A wrapper to enable custom type/ID to/from string conversions */ -template struct string_wrapper { +template +struct string_wrapper { template struct from_pystring_converter { from_pystring_converter() { @@ -44,34 +46,37 @@ template struct string_wrapper { boost::python::type_id()); }; - static void* convertible(PyObject* object) { + static void *convertible(PyObject *object) { return PyUnicode_Check(object) ? object : 0; } static void construct( - PyObject* object, - converter::rvalue_from_python_stage1_data* data) { - const wchar_t* value = PyUnicode_AsUnicode(object); + PyObject *object, + converter::rvalue_from_python_stage1_data *data) { + const wchar_t *value = PyUnicode_AsUnicode(object); const std::wstring value_ws(value); if (value == 0) throw_error_already_set(); - void* storage = ( - (boost::python::converter::rvalue_from_python_storage*) + void *storage = ( + (boost::python::converter::rvalue_from_python_storage *) data)->storage.bytes; - new (storage) T(fn(std::string(value_ws.begin(), value_ws.end()))); + new(storage) T(fn(std::string(value_ws.begin(), value_ws.end()))); data->convertible = storage; } - static F fn; + static F fn; }; - template struct to_str_wrapper { + template + struct to_str_wrapper { static F fn; - std::string str(T& x) { + + std::string str(T &x) { return fn(x); } }; - template static void wrap(const char *type_name, F1 to_str_fn, F2 from_str_fn) { + template + static void wrap(const char *type_name, F1 to_str_fn, F2 from_str_fn) { from_pystring_converter::fn = from_str_fn; from_pystring_converter(); to_str_wrapper::fn = to_str_fn; @@ -79,12 +84,34 @@ template struct string_wrapper { }; }; +std::string parse_python_exception(); + +template +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 { + PyObject * p = object(boost::ref(x)).ptr(); + PyDict_SetItemString(d, name, p); + } 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; + std::terminate(); + } +}; void init_python(const char *executable); + void deinit_python(); -void execute_python_file(const char* python_file); +void execute_python_file(const char *python_file); + std::string parse_python_exception(); + void arch_appendinittab(); #endif /* end of include guard: COMMON_PYBINDINGS_HH */ -- cgit v1.2.3 From b0e66d441cf584e7e48049d4f07afcc8e743309a Mon Sep 17 00:00:00 2001 From: David Shah Date: Thu, 7 Jun 2018 12:57:52 +0200 Subject: Global design object working Signed-off-by: David Shah --- common/pybindings.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'common/pybindings.h') diff --git a/common/pybindings.h b/common/pybindings.h index 44898198..a99ad51b 100644 --- a/common/pybindings.h +++ b/common/pybindings.h @@ -94,7 +94,7 @@ void python_export_global(const char *name, Tn &x) { return; d = PyModule_GetDict(m); try { - PyObject * p = object(boost::ref(x)).ptr(); + PyObject * p = incref(object(boost::ref(x)).ptr()); PyDict_SetItemString(d, name, p); } catch (boost::python::error_already_set const &) { // Parse and output the exception -- cgit v1.2.3 From c3e02527030c11f0177e3bf8d8c5d9a5c9925dc4 Mon Sep 17 00:00:00 2001 From: David Shah Date: Thu, 7 Jun 2018 13:10:53 +0200 Subject: Reformat Python bindings and ice40 main Signed-off-by: David Shah --- common/pybindings.h | 136 ++++++++++++++++++++++++++-------------------------- 1 file changed, 68 insertions(+), 68 deletions(-) (limited to 'common/pybindings.h') diff --git a/common/pybindings.h b/common/pybindings.h index a99ad51b..bb060718 100644 --- a/common/pybindings.h +++ b/common/pybindings.h @@ -23,85 +23,85 @@ #include "pycontainers.h" -#include -#include +#include #include -#include -#include #include -#include +#include +#include +#include using namespace boost::python; /* A wrapper to enable custom type/ID to/from string conversions */ -template -struct string_wrapper { - template - struct from_pystring_converter { - from_pystring_converter() { - converter::registry::push_back( - &convertible, - &construct, - boost::python::type_id()); - }; - - static void *convertible(PyObject *object) { - return PyUnicode_Check(object) ? object : 0; - } - - static void construct( - PyObject *object, - converter::rvalue_from_python_stage1_data *data) { - const wchar_t *value = PyUnicode_AsUnicode(object); - const std::wstring value_ws(value); - if (value == 0) throw_error_already_set(); - void *storage = ( - (boost::python::converter::rvalue_from_python_storage *) - data)->storage.bytes; - new(storage) T(fn(std::string(value_ws.begin(), value_ws.end()))); - data->convertible = storage; - } - - static F fn; - }; - - template - struct to_str_wrapper { - static F fn; - - std::string str(T &x) { - return fn(x); - } - }; - - template - static void wrap(const char *type_name, F1 to_str_fn, F2 from_str_fn) { - from_pystring_converter::fn = from_str_fn; - from_pystring_converter(); - to_str_wrapper::fn = to_str_fn; - class_(type_name, no_init).def("__str__", to_str_wrapper::str); - }; +template struct string_wrapper +{ + template struct from_pystring_converter + { + from_pystring_converter() + { + converter::registry::push_back(&convertible, &construct, + boost::python::type_id()); + }; + + static void *convertible(PyObject *object) + { + return PyUnicode_Check(object) ? object : 0; + } + + static void construct(PyObject *object, + converter::rvalue_from_python_stage1_data *data) + { + const wchar_t *value = PyUnicode_AsUnicode(object); + const std::wstring value_ws(value); + if (value == 0) + throw_error_already_set(); + void *storage = + ((boost::python::converter::rvalue_from_python_storage *) + data) + ->storage.bytes; + new (storage) T(fn(std::string(value_ws.begin(), value_ws.end()))); + data->convertible = storage; + } + + static F fn; + }; + + template struct to_str_wrapper + { + static F fn; + + std::string str(T &x) { return fn(x); } + }; + + template + static void wrap(const char *type_name, F1 to_str_fn, F2 from_str_fn) + { + from_pystring_converter::fn = from_str_fn; + from_pystring_converter(); + to_str_wrapper::fn = to_str_fn; + class_(type_name, no_init).def("__str__", to_str_wrapper::str); + }; }; std::string parse_python_exception(); -template -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 { - PyObject * p = incref(object(boost::ref(x)).ptr()); - PyDict_SetItemString(d, name, p); - } 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; - std::terminate(); - } +template 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 { + PyObject *p = incref(object(boost::ref(x)).ptr()); + PyDict_SetItemString(d, name, p); + } 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; + std::terminate(); + } }; void init_python(const char *executable); -- cgit v1.2.3