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 + common/pywrappers.h | 30 ++++++++++++++++++++++++++++++ generic/arch_pybindings.cc | 1 + 3 files changed, 32 insertions(+) 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(); diff --git a/common/pywrappers.h b/common/pywrappers.h index b564eb59..b9ed807d 100644 --- a/common/pywrappers.h +++ b/common/pywrappers.h @@ -250,6 +250,11 @@ template struct f } template static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } + + template static void def_wrap(WrapCls cls_, const char *name, Ta a = arg("arg1")) + { + cls_.def(name, wrapped_fn, a); + } }; // Two parameters, one return @@ -267,6 +272,11 @@ template static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } + + template static void def_wrap(WrapCls cls_, const char *name, const Ta &a) + { + cls_.def(name, wrapped_fn, a); + } }; // Three parameters, no return @@ -286,6 +296,11 @@ struct fn_wrapper_3a_v } template static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } + + template static void def_wrap(WrapCls cls_, const char *name, const Ta &a) + { + cls_.def(name, wrapped_fn, a); + } }; // Four parameters, no return @@ -309,6 +324,11 @@ struct fn_wrapper_4a_v } template static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } + + template static void def_wrap(WrapCls cls_, const char *name, const Ta &a) + { + cls_.def(name, wrapped_fn, a); + } }; // Five parameters, no return @@ -333,6 +353,11 @@ struct fn_wrapper_5a_v } template static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } + + template static void def_wrap(WrapCls cls_, const char *name, const Ta &a) + { + cls_.def(name, wrapped_fn, a); + } }; // Six parameters, no return @@ -358,6 +383,11 @@ struct fn_wrapper_6a_v } template static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } + + template static void def_wrap(WrapCls cls_, const char *name, const Ta &a) + { + cls_.def(name, wrapped_fn, a); + } }; // Wrapped getter diff --git a/generic/arch_pybindings.cc b/generic/arch_pybindings.cc index 5eabd42a..04620cd5 100644 --- a/generic/arch_pybindings.cc +++ b/generic/arch_pybindings.cc @@ -30,6 +30,7 @@ NEXTPNR_NAMESPACE_BEGIN void arch_wrap_python() { using namespace PythonConversion; + auto arch_cls = class_, boost::noncopyable>("Arch", init()); auto ctx_cls = class_, boost::noncopyable>("Context", no_init) .def("checksum", &Context::checksum) -- cgit v1.2.3