Skip to content

Commit

Permalink
Switch to use rclpy.init context manager. (#402)
Browse files Browse the repository at this point in the history
This allows us to use less code, but still cleanup
at a known time.

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette authored Jul 9, 2024
1 parent ef7c9b0 commit 5856482
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 30 deletions.
13 changes: 5 additions & 8 deletions launch_testing_ros/test/examples/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__':
Expand Down
14 changes: 5 additions & 9 deletions launch_testing_ros/test/examples/parameter_blackboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import rclpy
from rclpy.executors import ExternalShutdownException
from rclpy.node import Node


Expand All @@ -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__':
Expand Down
14 changes: 5 additions & 9 deletions launch_testing_ros/test/examples/talker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 5856482

Please sign in to comment.