From 4dc5990fee18d323100614c032e0defe43df3bef Mon Sep 17 00:00:00 2001 From: Chris Lalancette Date: Mon, 8 Jul 2024 22:43:21 +0000 Subject: [PATCH] Switch to use rclpy.init context manager. This allows us to use less code, but still cleanup at a known time. Signed-off-by: Chris Lalancette --- launch_testing_ros/test/examples/listener.py | 13 +++++-------- .../test/examples/parameter_blackboard.py | 14 +++++--------- launch_testing_ros/test/examples/talker.py | 14 +++++--------- .../test/test_launch_ros/actions/test_ros_timer.py | 7 +++---- 4 files changed, 18 insertions(+), 30 deletions(-) diff --git a/launch_testing_ros/test/examples/listener.py b/launch_testing_ros/test/examples/listener.py index 8605c1c8..0bff1ed6 100644 --- a/launch_testing_ros/test/examples/listener.py +++ b/launch_testing_ros/test/examples/listener.py @@ -13,6 +13,7 @@ # limitations under the License. import rclpy +from rclpy.executors import ExternalShutdownException from rclpy.node import Node from std_msgs.msg import String @@ -31,16 +32,12 @@ def callback(self, msg: String): def main(args=None): - rclpy.init(args=args) - - node = Listener() try: - rclpy.spin(node) - except KeyboardInterrupt: + with rclpy.init(args=args): + node = Listener() + rclpy.spin(node) + except (KeyboardInterrupt, ExternalShutdownException): pass - finally: - node.destroy_node() - rclpy.shutdown() if __name__ == '__main__': diff --git a/launch_testing_ros/test/examples/parameter_blackboard.py b/launch_testing_ros/test/examples/parameter_blackboard.py index e3354c7a..08563341 100644 --- a/launch_testing_ros/test/examples/parameter_blackboard.py +++ b/launch_testing_ros/test/examples/parameter_blackboard.py @@ -13,6 +13,7 @@ # limitations under the License. import rclpy +from rclpy.executors import ExternalShutdownException from rclpy.node import Node @@ -24,17 +25,12 @@ def __init__(self): def main(args=None): - rclpy.init(args=args) - - node = TestNode() - try: - rclpy.spin(node) - except KeyboardInterrupt: + with rclpy.init(args=args): + node = TestNode() + rclpy.spin(node) + except (KeyboardInterrupt, ExternalShutdownException): pass - finally: - node.destroy_node() - rclpy.shutdown() if __name__ == '__main__': diff --git a/launch_testing_ros/test/examples/talker.py b/launch_testing_ros/test/examples/talker.py index fc9f27f6..a914672e 100644 --- a/launch_testing_ros/test/examples/talker.py +++ b/launch_testing_ros/test/examples/talker.py @@ -13,6 +13,7 @@ # limitations under the License. import rclpy +from rclpy.executors import ExternalShutdownException from rclpy.node import Node from std_msgs.msg import String @@ -35,17 +36,12 @@ def callback(self): def main(args=None): - rclpy.init(args=args) - - node = Talker() - try: - rclpy.spin(node) - except KeyboardInterrupt: + with rclpy.init(args=args): + node = Talker() + rclpy.spin(node) + except (KeyboardInterrupt, ExternalShutdownException): pass - finally: - node.destroy_node() - rclpy.shutdown() if __name__ == '__main__': diff --git a/test_launch_ros/test/test_launch_ros/actions/test_ros_timer.py b/test_launch_ros/test/test_launch_ros/actions/test_ros_timer.py index b1f6dc97..7f1c37e7 100644 --- a/test_launch_ros/test/test_launch_ros/actions/test_ros_timer.py +++ b/test_launch_ros/test/test_launch_ros/actions/test_ros_timer.py @@ -151,10 +151,9 @@ def test_shutdown_preempts_timers(): @pytest.fixture def rclpy_node(): - rclpy.init() - node = rclpy.create_node('test_ros_timer_action_node') - yield node - rclpy.shutdown() + with rclpy.init(): + node = rclpy.create_node('test_ros_timer_action_node') + yield node def test_timer_uses_sim_time(rclpy_node):