From d36b18a176e8abd245aab8aa4eb3484c4791a19c Mon Sep 17 00:00:00 2001 From: Emerson Knapp Date: Thu, 18 Mar 2021 17:53:34 -0700 Subject: [PATCH 1/3] Print 'Infinite' for infinite durations in topic endpoint info Signed-off-by: Emerson Knapp --- rclpy/rclpy/duration.py | 9 +++++++++ rclpy/rclpy/topic_endpoint_info.py | 8 ++++---- rclpy/src/rclpy/_rclpy_pybind11.cpp | 2 ++ rclpy/test/test_time.py | 4 ++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/rclpy/rclpy/duration.py b/rclpy/rclpy/duration.py index c59fc559e..a9a2e3991 100644 --- a/rclpy/rclpy/duration.py +++ b/rclpy/rclpy/duration.py @@ -34,6 +34,11 @@ def nanoseconds(self): def __repr__(self): return 'Duration(nanoseconds={0})'.format(self.nanoseconds) + def __str__(self): + if self == Duration.Infinite(): + return 'Infinite' + return f'{self.nanoseconds} nanoseconds' + def __eq__(self, other): if isinstance(other, Duration): return self.nanoseconds == other.nanoseconds @@ -77,5 +82,9 @@ def from_msg(cls, msg): raise TypeError('Must pass a builtin_interfaces.msg.Duration object') return cls(seconds=msg.sec, nanoseconds=msg.nanosec) + @classmethod + def Infinite(cls): + return cls(nanoseconds=_rclpy.rclpy_RMW_DURATION_INFINITE) + def get_c_duration(self): return self._duration_handle diff --git a/rclpy/rclpy/topic_endpoint_info.py b/rclpy/rclpy/topic_endpoint_info.py index be4ceb773..408f43965 100644 --- a/rclpy/rclpy/topic_endpoint_info.py +++ b/rclpy/rclpy/topic_endpoint_info.py @@ -166,9 +166,9 @@ def __str__(self): result += 'QoS profile:\n' result += ' Reliability: %s\n' % self.qos_profile.reliability.name result += ' Durability: %s\n' % self.qos_profile.durability.name - result += ' Lifespan: %d nanoseconds\n' % self.qos_profile.lifespan.nanoseconds - result += ' Deadline: %d nanoseconds\n' % self.qos_profile.deadline.nanoseconds + result += ' Lifespan: %s\n' % str(self.qos_profile.lifespan) + result += ' Deadline: %s\n' % str(self.qos_profile.deadline) result += ' Liveliness: %s\n' % self.qos_profile.liveliness.name - result += ' Liveliness lease duration: %d nanoseconds' % \ - self.qos_profile.liveliness_lease_duration.nanoseconds + result += ' Liveliness lease duration: %s' % \ + str(self.qos_profile.liveliness_lease_duration) return result diff --git a/rclpy/src/rclpy/_rclpy_pybind11.cpp b/rclpy/src/rclpy/_rclpy_pybind11.cpp index 2fbacb1ae..af6fa6aea 100644 --- a/rclpy/src/rclpy/_rclpy_pybind11.cpp +++ b/rclpy/src/rclpy/_rclpy_pybind11.cpp @@ -58,6 +58,7 @@ PYBIND11_MODULE(_rclpy_pybind11, m) { .value("STEADY_TIME", RCL_STEADY_TIME); m.attr("RCL_DEFAULT_DOMAIN_ID") = py::int_(RCL_DEFAULT_DOMAIN_ID); + m.attr("RMW_DURATION_INFINITE") = py::int_(rmw_time_total_nsec(RMW_DURATION_INFINITE)); py::enum_(m, "ClockChange") .value( @@ -85,6 +86,7 @@ PYBIND11_MODULE(_rclpy_pybind11, m) { .def_readonly("compatibility", &rclpy::QoSCheckCompatibleResult::compatibility) .def_readonly("reason", &rclpy::QoSCheckCompatibleResult::reason); + py::register_exception(m, "RCUtilsError", PyExc_RuntimeError); py::register_exception(m, "RMWError", PyExc_RuntimeError); auto rclerror = py::register_exception(m, "RCLError", PyExc_RuntimeError); diff --git a/rclpy/test/test_time.py b/rclpy/test/test_time.py index d00178107..bf69b4936 100644 --- a/rclpy/test/test_time.py +++ b/rclpy/test/test_time.py @@ -228,3 +228,7 @@ def test_seconds_nanoseconds(self): assert (1, int(5e8)) == Time(seconds=1, nanoseconds=5e8).seconds_nanoseconds() assert (1, int(5e8)) == Time(seconds=0, nanoseconds=15e8).seconds_nanoseconds() assert (0, 0) == Time().seconds_nanoseconds() + + def test_infinite_duration(self): + duration = Duration.Infinite() + assert str(duration) == 'Infinite' From c707d1181480fb8673840099e2eca76c7f079110 Mon Sep 17 00:00:00 2001 From: Emerson Knapp Date: Mon, 22 Mar 2021 14:38:32 -0700 Subject: [PATCH 2/3] Use format strings for topic endpoint info printing Signed-off-by: Emerson Knapp --- rclpy/rclpy/duration.py | 2 +- rclpy/rclpy/topic_endpoint_info.py | 29 +++++++++++++++-------------- rclpy/src/rclpy/_rclpy_pybind11.cpp | 1 - 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/rclpy/rclpy/duration.py b/rclpy/rclpy/duration.py index a9a2e3991..50ef7f9cd 100644 --- a/rclpy/rclpy/duration.py +++ b/rclpy/rclpy/duration.py @@ -84,7 +84,7 @@ def from_msg(cls, msg): @classmethod def Infinite(cls): - return cls(nanoseconds=_rclpy.rclpy_RMW_DURATION_INFINITE) + return cls(nanoseconds=_rclpy.RMW_DURATION_INFINITE) def get_c_duration(self): return self._duration_handle diff --git a/rclpy/rclpy/topic_endpoint_info.py b/rclpy/rclpy/topic_endpoint_info.py index 408f43965..7569a926d 100644 --- a/rclpy/rclpy/topic_endpoint_info.py +++ b/rclpy/rclpy/topic_endpoint_info.py @@ -158,17 +158,18 @@ def __eq__(self, other): for slot in self.__slots__) def __str__(self): - result = 'Node name: %s\n' % self.node_name - result += 'Node namespace: %s\n' % self.node_namespace - result += 'Topic type: %s\n' % self.topic_type - result += 'Endpoint type: %s\n' % self.endpoint_type.name - result += 'GID: %s\n' % '.'.join(format(x, '02x') for x in self.endpoint_gid) - result += 'QoS profile:\n' - result += ' Reliability: %s\n' % self.qos_profile.reliability.name - result += ' Durability: %s\n' % self.qos_profile.durability.name - result += ' Lifespan: %s\n' % str(self.qos_profile.lifespan) - result += ' Deadline: %s\n' % str(self.qos_profile.deadline) - result += ' Liveliness: %s\n' % self.qos_profile.liveliness.name - result += ' Liveliness lease duration: %s' % \ - str(self.qos_profile.liveliness_lease_duration) - return result + gid = '.'.join(format(x, '02x') for x in self.endpoint_gid) + return '\n'.join([ + f'Node name: {self.node_name}', + f'Node namespace: {self.node_namespace}', + f'Topic type: {self.topic_type}', + f'Endpoint type: {self.endpoint_type.name}', + f'GID: {gid}', + 'QoS profile:', + f' Reliability: {self.qos_profile.reliability.name}', + f' Durability: {self.qos_profile.durability.name}', + f' Lifespan: {self.qos_profile.lifespan}', + f' Deadline: {self.qos_profile.deadline}', + f' Liveliness: {self.qos_profile.liveliness.name}', + f' Liveliness lease duration: {self.qos_profile.liveliness_lease_duration}', + ]) diff --git a/rclpy/src/rclpy/_rclpy_pybind11.cpp b/rclpy/src/rclpy/_rclpy_pybind11.cpp index af6fa6aea..01df8143c 100644 --- a/rclpy/src/rclpy/_rclpy_pybind11.cpp +++ b/rclpy/src/rclpy/_rclpy_pybind11.cpp @@ -86,7 +86,6 @@ PYBIND11_MODULE(_rclpy_pybind11, m) { .def_readonly("compatibility", &rclpy::QoSCheckCompatibleResult::compatibility) .def_readonly("reason", &rclpy::QoSCheckCompatibleResult::reason); - py::register_exception(m, "RCUtilsError", PyExc_RuntimeError); py::register_exception(m, "RMWError", PyExc_RuntimeError); auto rclerror = py::register_exception(m, "RCLError", PyExc_RuntimeError); From 5b8cccc9bcaf6ff6a12245b409553cf59bb9d81f Mon Sep 17 00:00:00 2001 From: Emerson Knapp Date: Fri, 16 Apr 2021 11:12:46 -0700 Subject: [PATCH 3/3] use a constant instead of a static factory method for duration.infinite Signed-off-by: Emerson Knapp --- rclpy/rclpy/duration.py | 10 +++++----- rclpy/test/test_time.py | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/rclpy/rclpy/duration.py b/rclpy/rclpy/duration.py index 50ef7f9cd..4fde06f38 100644 --- a/rclpy/rclpy/duration.py +++ b/rclpy/rclpy/duration.py @@ -35,7 +35,7 @@ def __repr__(self): return 'Duration(nanoseconds={0})'.format(self.nanoseconds) def __str__(self): - if self == Duration.Infinite(): + if self == Infinite: return 'Infinite' return f'{self.nanoseconds} nanoseconds' @@ -82,9 +82,9 @@ def from_msg(cls, msg): raise TypeError('Must pass a builtin_interfaces.msg.Duration object') return cls(seconds=msg.sec, nanoseconds=msg.nanosec) - @classmethod - def Infinite(cls): - return cls(nanoseconds=_rclpy.RMW_DURATION_INFINITE) - def get_c_duration(self): return self._duration_handle + + +# Constant representing an infinite amount of time. +Infinite = Duration(nanoseconds=_rclpy.RMW_DURATION_INFINITE) diff --git a/rclpy/test/test_time.py b/rclpy/test/test_time.py index bf69b4936..c89aa0a4b 100644 --- a/rclpy/test/test_time.py +++ b/rclpy/test/test_time.py @@ -16,6 +16,7 @@ from rclpy.clock import ClockType from rclpy.duration import Duration +from rclpy.duration import Infinite from rclpy.time import Time from test_msgs.msg import Builtins @@ -230,5 +231,5 @@ def test_seconds_nanoseconds(self): assert (0, 0) == Time().seconds_nanoseconds() def test_infinite_duration(self): - duration = Duration.Infinite() + duration = Infinite assert str(duration) == 'Infinite'