diff options
author | Martin Braun <martin.braun@ettus.com> | 2019-02-04 16:00:16 +0100 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2019-02-22 16:56:52 -0800 |
commit | 692ddc71b17196487dcad982836e074cab9a0f25 (patch) | |
tree | cf76abf577dde6128e03561c52d9c31dca302026 /host/lib/types | |
parent | 51bbf548c9b442d0b53b6c8de5f89403de274424 (diff) | |
download | uhd-692ddc71b17196487dcad982836e074cab9a0f25.tar.gz uhd-692ddc71b17196487dcad982836e074cab9a0f25.tar.bz2 uhd-692ddc71b17196487dcad982836e074cab9a0f25.zip |
python: Replace Boost.Python with PyBind11
This does not change the Python API itself, but it is still
a significant change. Most importantly, it removes the dependency on
Boost.Python.
Diffstat (limited to 'host/lib/types')
-rw-r--r-- | host/lib/types/filters_python.hpp | 27 | ||||
-rw-r--r-- | host/lib/types/metadata_python.hpp | 49 | ||||
-rw-r--r-- | host/lib/types/sensors_python.hpp | 24 | ||||
-rw-r--r-- | host/lib/types/serial_python.hpp | 16 | ||||
-rw-r--r-- | host/lib/types/time_spec_python.hpp | 24 | ||||
-rw-r--r-- | host/lib/types/tune_python.hpp | 12 | ||||
-rw-r--r-- | host/lib/types/types_python.hpp | 35 |
7 files changed, 89 insertions, 98 deletions
diff --git a/host/lib/types/filters_python.hpp b/host/lib/types/filters_python.hpp index ff5785345..ad13eed1e 100644 --- a/host/lib/types/filters_python.hpp +++ b/host/lib/types/filters_python.hpp @@ -1,5 +1,6 @@ // // Copyright 2017-2018 Ettus Research, a National Instruments Company +// Copyright 2019 Ettus Research, a National Instruments Brand // // SPDX-License-Identifier: GPL-3.0-or-later // @@ -9,24 +10,22 @@ #include <uhd/types/filters.hpp> -void export_filters() +void export_filters(py::module& m) { using filter_info_base = uhd::filter_info_base; using filter_info_type = filter_info_base::filter_type; using analog_filter_base = uhd::analog_filter_base; using analog_filter_lp = uhd::analog_filter_lp; - bp::enum_<filter_info_type>("filter_type") + py::enum_<filter_info_type>(m, "filter_type") .value("analog_low_pass" , filter_info_base::ANALOG_LOW_PASS ) .value("analog_band_pass", filter_info_base::ANALOG_BAND_PASS) .value("digital_i16" , filter_info_base::DIGITAL_I16 ) .value("digital_fir_i16" , filter_info_base::DIGITAL_FIR_I16 ) ; - bp::class_< - filter_info_base, - boost::shared_ptr<filter_info_base> > - ("filter_info_base", bp::init<filter_info_type, bool, size_t>()) + py::class_<filter_info_base, filter_info_base::sptr>(m, "filter_info_base") + .def(py::init<filter_info_type, bool, size_t>()) // Methods .def("is_bypassed", &filter_info_base::is_bypassed ) @@ -34,21 +33,15 @@ void export_filters() .def("__str__" , &filter_info_base::to_pp_string) ; - bp::class_< - analog_filter_base, - boost::shared_ptr<analog_filter_base>, - bp::bases<filter_info_base> > - ("analog_filter_base", bp::init<filter_info_type, bool, size_t, std::string>()) + py::class_<analog_filter_base, analog_filter_base::sptr>(m, "analog_filter_base") + .def(py::init<filter_info_type, bool, size_t, std::string>()) // Methods - .def("get_analog_type", &analog_filter_base::get_analog_type, bp::return_value_policy<bp::copy_const_reference>()) + .def("get_analog_type", &analog_filter_base::get_analog_type) ; - bp::class_< - analog_filter_lp, - boost::shared_ptr<analog_filter_lp>, - bp::bases<analog_filter_base> > - ("analog_filter_lp", bp::init<filter_info_type, bool, size_t, const std::string, double, double>()) + py::class_<analog_filter_lp, boost::shared_ptr<analog_filter_lp>>(m, "analog_filter_lp") + .def(py::init<filter_info_type, bool, size_t, const std::string, double, double>()) // Methods .def("get_cutoff" , &analog_filter_lp::get_cutoff ) diff --git a/host/lib/types/metadata_python.hpp b/host/lib/types/metadata_python.hpp index 876756d73..f57d82dc9 100644 --- a/host/lib/types/metadata_python.hpp +++ b/host/lib/types/metadata_python.hpp @@ -1,5 +1,6 @@ // // Copyright 2017-2018 Ettus Research, a National Instruments Company +// Copyright 2019 Ettus Research, a National Instruments Brand // // SPDX-License-Identifier: GPL-3.0-or-later // @@ -9,7 +10,7 @@ #include <uhd/types/ranges.hpp> -void export_metadata() +void export_metadata(py::module& m) { using range_t = uhd::range_t; using meta_range_t = uhd::meta_range_t; @@ -19,7 +20,7 @@ void export_metadata() using async_metadata_t = uhd::async_metadata_t; using event_code_t = async_metadata_t::event_code_t; - bp::enum_<error_code_t>("rx_metadata_error_code") + py::enum_<error_code_t>(m, "rx_metadata_error_code") .value("none" , error_code_t::ERROR_CODE_NONE ) .value("timeout" , error_code_t::ERROR_CODE_TIMEOUT ) .value("late" , error_code_t::ERROR_CODE_LATE_COMMAND) @@ -29,11 +30,11 @@ void export_metadata() .value("bad_packet" , error_code_t::ERROR_CODE_BAD_PACKET ) ; - bp::class_<range_t> - ("range", bp::init<double>()) - + py::class_<range_t>(m, "range") // Constructors - .def(bp::init<double, double, double>()) + .def(py::init<double>()) + .def(py::init<double, double>()) + .def(py::init<double, double, double>()) // Methods .def("start" , &range_t::start ) @@ -42,30 +43,28 @@ void export_metadata() .def("__str__", &range_t::to_pp_string) ; - bp::class_<std::vector<range_t> >("range_vector") - .def(bp::vector_indexing_suite<std::vector<range_t> >()); - - bp::class_<meta_range_t, bp::bases<std::vector<range_t> > > - ("meta_range", bp::init<>()) - + py::class_<meta_range_t>(m, "meta_range_t") // Constructors - .def(bp::init<double, double, double>()) + .def(py::init<>()) + .def(py::init<double, double>()) + .def(py::init<double, double, double>()) // Methods .def("start" , &meta_range_t::start ) .def("stop" , &meta_range_t::stop ) .def("step" , &meta_range_t::step ) - .def("clip" , &meta_range_t::clip ) + .def("clip" , &meta_range_t::clip, py::arg("value"), py::arg("clip_step") = false) .def("__str__", &meta_range_t::to_pp_string) ; - bp::class_<rx_metadata_t>("rx_metadata", bp::init<>()) + py::class_<rx_metadata_t>(m, "rx_metadata") + .def(py::init<>()) // Methods .def("reset" , &rx_metadata_t::reset ) .def("to_pp_string", &rx_metadata_t::to_pp_string) .def("strerror" , &rx_metadata_t::strerror ) - .def("__str__" , &rx_metadata_t::to_pp_string, bp::args("compact") = false) + .def("__str__" , &rx_metadata_t::to_pp_string, py::arg("compact") = false) // Properties .def_readonly("has_time_spec" , &rx_metadata_t::has_time_spec ) @@ -77,7 +76,8 @@ void export_metadata() .def_readonly("out_of_sequence", &rx_metadata_t::out_of_sequence) ; - bp::class_<tx_metadata_t>("tx_metadata", bp::init<>()) + py::class_<tx_metadata_t>(m, "tx_metadata") + .def(py::init<>()) // Properties .def_readwrite("has_time_spec" , &tx_metadata_t::has_time_spec ) @@ -86,7 +86,7 @@ void export_metadata() .def_readwrite("end_of_burst" , &tx_metadata_t::end_of_burst ) ; - bp::enum_<event_code_t>("tx_metadata_event_code") + py::enum_<event_code_t>(m, "tx_metadata_event_code") .value("burst_ack" , event_code_t::EVENT_CODE_BURST_ACK ) .value("underflow" , event_code_t::EVENT_CODE_UNDERFLOW ) .value("seq_error" , event_code_t::EVENT_CODE_SEQ_ERROR ) @@ -96,15 +96,16 @@ void export_metadata() .value("user_payload" , event_code_t::EVENT_CODE_USER_PAYLOAD ) ; - bp::class_<async_metadata_t>("async_metadata", bp::init<>()) + py::class_<async_metadata_t>(m, "async_metadata") + .def(py::init<>()) // Properties - .def_readwrite("channel" , &async_metadata_t::channel ) - .def_readwrite("has_time_spec", &async_metadata_t::has_time_spec) - .def_readwrite("time_spec" , &async_metadata_t::time_spec ) - .def_readwrite("event_code" , &async_metadata_t::event_code ) + .def_readonly("channel" , &async_metadata_t::channel ) + .def_readonly("has_time_spec", &async_metadata_t::has_time_spec) + .def_readonly("time_spec" , &async_metadata_t::time_spec ) + .def_readonly("event_code" , &async_metadata_t::event_code ) // TODO: Expose user payloads - //.def_readwrite("user_payload" , &async_metadata_t::user_payload ) + //.def_readonly("user_payload" , &async_metadata_t::user_payload ) ; } diff --git a/host/lib/types/sensors_python.hpp b/host/lib/types/sensors_python.hpp index 8fd9da4e3..e3937d117 100644 --- a/host/lib/types/sensors_python.hpp +++ b/host/lib/types/sensors_python.hpp @@ -1,5 +1,6 @@ // // Copyright 2017-2018 Ettus Research, a National Instruments Company +// Copyright 2019 Ettus Research, a National Instruments Brand // // SPDX-License-Identifier: GPL-3.0-or-later // @@ -9,25 +10,24 @@ #include <uhd/types/sensors.hpp> -void export_sensors() +void export_sensors(py::module& m) { using sensor_value_t = uhd::sensor_value_t; using data_type_t = sensor_value_t::data_type_t; - bp::enum_<data_type_t>("data_type") + py::enum_<data_type_t>(m, "data_type") .value("b", data_type_t::BOOLEAN) .value("i", data_type_t::INTEGER) .value("r", data_type_t::REALNUM) .value("s", data_type_t::STRING ) ; - bp::class_<sensor_value_t> - ("sensor_value", bp::init<const std::string&, bool, const std::string&, const std::string&>()) - + py::class_<sensor_value_t>(m, "sensor_value") // Constructors - .def(bp::init<const std::string&, signed, const std::string&, const std::string&>()) - .def(bp::init<const std::string&, double, const std::string&, const std::string&>()) - .def(bp::init<const std::string&, const std::string& , const std::string&>()) + .def(py::init<const std::string&, bool, const std::string&, const std::string&>()) + .def(py::init<const std::string&, signed, const std::string&, const std::string&>()) + .def(py::init<const std::string&, double, const std::string&, const std::string&>()) + .def(py::init<const std::string&, const std::string& , const std::string&>()) // Methods .def("to_bool", &sensor_value_t::to_bool ) @@ -36,10 +36,10 @@ void export_sensors() .def("__str__", &sensor_value_t::to_pp_string) // Properties - .add_property("name", &sensor_value_t::name ) - .add_property("value", &sensor_value_t::value) - .add_property("unit", &sensor_value_t::unit ) - .add_property("type", &sensor_value_t::type ) + .def_readwrite("name", &sensor_value_t::name ) + .def_readwrite("value", &sensor_value_t::value) + .def_readwrite("unit", &sensor_value_t::unit ) + .def_readwrite("type", &sensor_value_t::type ) ; } diff --git a/host/lib/types/serial_python.hpp b/host/lib/types/serial_python.hpp index 441bcc4d7..eb52472a5 100644 --- a/host/lib/types/serial_python.hpp +++ b/host/lib/types/serial_python.hpp @@ -1,5 +1,6 @@ // // Copyright 2017-2018 Ettus Research, a National Instruments Company +// Copyright 2019 Ettus Research, a National Instruments Brand // // SPDX-License-Identifier: GPL-3.0-or-later // @@ -9,23 +10,24 @@ #include <uhd/types/serial.hpp> -void export_spi_config() +void export_spi_config(py::module& m) { using spi_config_t = uhd::spi_config_t; using spi_edge_t = spi_config_t::edge_t; - bp::enum_<spi_edge_t>("spi_edge") + py::enum_<spi_edge_t>(m, "spi_edge") .value("EDGE_RISE" , spi_edge_t::EDGE_RISE) .value("EDGE_FALL", spi_edge_t::EDGE_FALL) ; - bp::class_<spi_config_t>("spi_config", bp::init<spi_edge_t>()) + py::class_<spi_config_t>(m, "spi_config") + .def(py::init<spi_edge_t>()) // Properties - .add_property("mosi_edge" , &spi_config_t::mosi_edge ) - .add_property("miso_edge" , &spi_config_t::miso_edge ) - .add_property("use_custom_divider", &spi_config_t::use_custom_divider) - .add_property("divider" , &spi_config_t::divider ) + .def_readwrite("mosi_edge" , &spi_config_t::mosi_edge ) + .def_readwrite("miso_edge" , &spi_config_t::miso_edge ) + .def_readwrite("use_custom_divider", &spi_config_t::use_custom_divider) + .def_readwrite("divider" , &spi_config_t::divider ) ; } diff --git a/host/lib/types/time_spec_python.hpp b/host/lib/types/time_spec_python.hpp index 219dd076b..12489fd44 100644 --- a/host/lib/types/time_spec_python.hpp +++ b/host/lib/types/time_spec_python.hpp @@ -1,5 +1,6 @@ // // Copyright 2017-2018 Ettus Research, a National Instruments Company +// Copyright 2019 Ettus Research, a National Instruments Brand // // SPDX-License-Identifier: GPL-3.0-or-later // @@ -8,30 +9,31 @@ #define INCLUDED_UHD_TIME_SPEC_PYTHON_HPP #include <uhd/types/time_spec.hpp> +#include <pybind11/operators.h> -void export_time_spec() +void export_time_spec(py::module& m) { using time_spec_t = uhd::time_spec_t; - bp::class_<time_spec_t>("time_spec", bp::init<double>()) + py::class_<time_spec_t>(m, "time_spec") //Additional constructors - .def(bp::init<int64_t, double>()) - .def(bp::init<int64_t, long, double>()) + .def(py::init<double>()) + .def(py::init<int64_t, double>()) + .def(py::init<int64_t, long, double>()) // Methods - .def("from_ticks" , &time_spec_t::from_ticks ) - .staticmethod("from_ticks" ) + .def_static("from_ticks" , &time_spec_t::from_ticks) .def("get_tick_count" , &time_spec_t::get_tick_count ) .def("to_ticks" , &time_spec_t::to_ticks ) .def("get_real_secs" , &time_spec_t::get_real_secs ) .def("get_frac_secs" , &time_spec_t::get_frac_secs ) - .def(bp::self += time_spec_t()) - .def(bp::self += double()) - .def(bp::self + double()) - .def(bp::self + time_spec_t()) - .def(bp::self -= time_spec_t()) + .def(py::self += time_spec_t()) + .def(py::self += double()) + .def(py::self + double()) + .def(py::self + time_spec_t()) + .def(py::self -= time_spec_t()) ; } diff --git a/host/lib/types/tune_python.hpp b/host/lib/types/tune_python.hpp index 587c8cbea..6c3607908 100644 --- a/host/lib/types/tune_python.hpp +++ b/host/lib/types/tune_python.hpp @@ -1,5 +1,6 @@ // // Copyright 2017-2018 Ettus Research, a National Instruments Company +// Copyright 2019 Ettus Research, a National Instruments Brand // // SPDX-License-Identifier: GPL-3.0-or-later // @@ -10,20 +11,20 @@ #include <uhd/types/tune_result.hpp> #include <uhd/types/tune_request.hpp> -void export_tune() +void export_tune(py::module& m) { using tune_request_t = uhd::tune_request_t; using tune_result_t = uhd::tune_result_t; using policy_t = tune_request_t::policy_t; - bp::enum_<policy_t>("tune_request_policy") + py::enum_<policy_t>(m, "tune_request_policy") .value("none", tune_request_t::POLICY_NONE ) .value("auto", tune_request_t::POLICY_AUTO ) .value("manual", tune_request_t::POLICY_MANUAL) ; - bp::class_<tune_request_t>("tune_request", bp::init<double>()) - .def(bp::init<double, double>()) + py::class_<tune_request_t>(m, "tune_request") + .def(py::init<double, double>()) .def_readwrite("target_freq" , &tune_request_t::target_freq ) .def_readwrite("rf_freq_policy" , &tune_request_t::rf_freq_policy ) .def_readwrite("dsp_freq_policy", &tune_request_t::dsp_freq_policy) @@ -32,7 +33,8 @@ void export_tune() .def_readwrite("args" , &tune_request_t::args ) ; - bp::class_<tune_result_t>("tune_result", bp::init<>()) + py::class_<tune_result_t>(m, "tune_result") + .def(py::init<>()) .def_readwrite("clipped_rf_freq", &tune_result_t::clipped_rf_freq) .def_readwrite("target_rf_freq" , &tune_result_t::target_rf_freq ) .def_readwrite("actual_rf_freq" , &tune_result_t::actual_rf_freq ) diff --git a/host/lib/types/types_python.hpp b/host/lib/types/types_python.hpp index 434ae9171..b7003d7b0 100644 --- a/host/lib/types/types_python.hpp +++ b/host/lib/types/types_python.hpp @@ -1,5 +1,6 @@ // // Copyright 2017-2018 Ettus Research, a National Instruments Company +// Copyright 2019 Ettus Research, a National Instruments Brand // // SPDX-License-Identifier: GPL-3.0-or-later // @@ -7,59 +8,49 @@ #ifndef INCLUDED_UHD_TYPES_PYTHON_HPP #define INCLUDED_UHD_TYPES_PYTHON_HPP +#include <pybind11/stl.h> #include <uhd/types/device_addr.hpp> #include <uhd/types/stream_cmd.hpp> -#include <boost/python.hpp> #include <string> #include <map> -//! Make a device_addr_t from a Python dict of strings -static uhd::device_addr_t make_device_addr(bp::dict& info) { - // Manually extract each key and each value, copy them to a map of strings, and return that. - std::map<std::string,std::string> info_map; - auto keys = info.keys(); - for (int ii = 0; ii < bp::len(keys); ++ii) { - std::string key = bp::extract<std::string>(keys[ii]); - info_map[key] = bp::extract<std::string>(info[key]); - } - return uhd::device_addr_t(info_map); -} - -void export_types() +void export_types(py::module& m) { using stream_cmd_t = uhd::stream_cmd_t; using stream_mode_t = stream_cmd_t::stream_mode_t; using str_map = std::map<std::string, std::string>; - bp::enum_<stream_mode_t>("stream_mode") + py::enum_<stream_mode_t>(m, "stream_mode") .value("start_cont", stream_cmd_t::STREAM_MODE_START_CONTINUOUS ) .value("stop_cont" , stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS ) .value("num_done" , stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE) .value("num_more" , stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_MORE) ; - bp::class_<stream_cmd_t>("stream_cmd", bp::init<stream_cmd_t::stream_mode_t>()) - + py::class_<stream_cmd_t>(m, "stream_cmd") + .def(py::init<stream_cmd_t::stream_mode_t>()) // Properties .def_readwrite("num_samps" , &stream_cmd_t::num_samps ) .def_readwrite("time_spec" , &stream_cmd_t::time_spec ) .def_readwrite("stream_now", &stream_cmd_t::stream_now) ; - bp::class_<uhd::device_addr_t>("device_addr", bp::init<bp::optional<std::string> >()) + py::class_<uhd::device_addr_t>(m, "device_addr") // Constructors - /* TODO: This calls the correct C++ constructor, but Python - dictionaries != str_maps, so we get a signature error */ - .def(bp::init<str_map>()) + .def(py::init<>()) + .def(py::init<std::string>()) + .def(py::init<str_map>()) // Methods .def("__str__", &uhd::device_addr_t::to_pp_string) .def("to_string", &uhd::device_addr_t::to_string) .def("to_pp_string", &uhd::device_addr_t::to_pp_string) ; + // This will allow functions in Python that take a device_addr to also take + // a string: + py::implicitly_convertible<std::string, uhd::device_addr_t>(); - bp::def("make_device_addr", make_device_addr); } #endif /* INCLUDED_UHD_TYPES_PYTHON_HPP */ |