Skip to content

Commit

Permalink
raise TypeError when passing invalid message types
Browse files Browse the repository at this point in the history
Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
  • Loading branch information
dirk-thomas committed Apr 22, 2019
1 parent bc7b7b0 commit d49d33b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
12 changes: 12 additions & 0 deletions rclpy/rclpy/action/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,13 @@ def send_goal(self, goal, **kwargs):
:type goal: action_type.Goal
:return: The result response.
:rtype: action_type.Result
:raises: TypeError if the type of the passed goal isn't an instance of
the Goal type of the provided action when the service was
constructed.
"""
if not isinstance(goal, self._action_type.Goal):
raise TypeError()

event = threading.Event()

def unblock(future):
Expand Down Expand Up @@ -386,7 +392,13 @@ def send_goal_async(self, goal, feedback_callback=None, goal_uuid=None):
:return: a Future instance to a goal handle that completes when the goal request
has been accepted or rejected.
:rtype: :class:`rclpy.task.Future` instance
:raises: TypeError if the type of the passed goal isn't an instance of
the Goal type of the provided action when the service was
constructed.
"""
if not isinstance(goal, self._action_type.Goal):
raise TypeError()

request = self._action_type.Impl.SendGoalService.Request()
request.goal_id = self._generate_random_uuid() if goal_uuid is None else goal_uuid
request.goal = goal
Expand Down
3 changes: 3 additions & 0 deletions rclpy/rclpy/action/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def execute(self, execute_callback=None):
self._action_server.notify_execute(self, execute_callback)

def publish_feedback(self, feedback):
if not isinstance(feedback, self._action_server.action_type.Feedback):
raise TypeError()

with self._lock:
# Ignore for already destructed goal handles
if self._handle is None:
Expand Down
12 changes: 12 additions & 0 deletions rclpy/rclpy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ def call(self, request: SrvTypeRequest) -> SrvTypeResponse:
:param request: The service request.
:return: The service response.
:raises: TypeError if the type of the passed request isn't an instance
of the Request type of the provided service when the client was
constructed.
"""
if not isinstance(request, self.srv_type.Request):
raise TypeError()

event = threading.Event()

def unblock(future):
Expand Down Expand Up @@ -116,7 +122,13 @@ def call_async(self, request: SrvTypeRequest) -> Future:
:param request: The service request.
:return: A future that completes when the request does.
:raises: TypeError if the type of the passed request isn't an instance
of the Request type of the provided service when the client was
constructed.
"""
if not isinstance(request, self.srv_type.Request):
raise TypeError()

sequence_number = _rclpy.rclpy_send_request(self.client_handle, request)
if sequence_number in self._pending_requests:
raise RuntimeError('Sequence (%r) conflicts with pending request' % sequence_number)
Expand Down
7 changes: 5 additions & 2 deletions rclpy/rclpy/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ def publish(self, msg: MsgType) -> None:
"""
Send a message to the topic for the publisher.
:param msg: The ROS message to publish. The message must be the same type as the type
provided when the publisher was constructed.
:param msg: The ROS message to publish.
:raises: TypeError if the type of the passed message isn't an instance
of the provided type when the publisher was constructed.
"""
if not isinstance(msg, self.msg_type):
raise TypeError()
_rclpy.rclpy_publish(self.publisher_handle, msg)
5 changes: 5 additions & 0 deletions rclpy/rclpy/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,10 @@ def send_response(self, response: SrvTypeResponse, header) -> None:
:param response: The service response.
:param header: Capsule pointing to the service header from the original request.
:raises: TypeError if the type of the passed response isn't an instance
of the Response type of the provided service when the service was
constructed.
"""
if not isinstance(response, self.srv_type.Response):
raise TypeError()
_rclpy.rclpy_send_response(self.service_handle, response, header)

0 comments on commit d49d33b

Please sign in to comment.