Skip to content

Commit

Permalink
Handle unknown global ROS arguments. (#485)
Browse files Browse the repository at this point in the history
Signed-off-by: Michel Hidalgo <[email protected]>
  • Loading branch information
hidmic authored Dec 19, 2019
1 parent 4725106 commit d2c30d9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
33 changes: 26 additions & 7 deletions rclpy/src/rclpy/_rclpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,16 +558,35 @@ rclpy_init(PyObject * Py_UNUSED(self), PyObject * args)
if (have_args) {
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
rcl_ret_t ret = rcl_init_options_init(&init_options, allocator);
if (RCL_RET_OK != ret) {
PyErr_Format(
RCLError, "Failed to initialize init_options: %s", rcl_get_error_string().str);
rcl_reset_error();
} else {
if (RCL_RET_OK == ret) {
ret = rcl_init(num_args, arg_values, &init_options, context);
if (ret != RCL_RET_OK) {
PyErr_Format(RCLError, "Failed to init: %s", rcl_get_error_string().str);
if (RCL_RET_OK == ret) {
int unparsed_ros_args_count =
rcl_arguments_get_count_unparsed_ros(&context->global_arguments);
if (unparsed_ros_args_count > 0) {
int * unparsed_ros_args_indices = NULL;
ret = rcl_arguments_get_unparsed_ros(
&context->global_arguments, allocator, &unparsed_ros_args_indices);
if (RCL_RET_OK == ret) {
_rclpy_raise_unknown_ros_args(
pyargs, unparsed_ros_args_indices, unparsed_ros_args_count);
allocator.deallocate(unparsed_ros_args_indices, allocator.state);
} else {
PyErr_Format(
PyExc_RuntimeError,
"Failed to get unparsed ROS arguments: %s",
rcl_get_error_string().str);
rcl_reset_error();
}
}
} else {
PyErr_Format(PyExc_RuntimeError, "Failed to init: %s", rcl_get_error_string().str);
rcl_reset_error();
}
} else {
PyErr_Format(
PyExc_RuntimeError, "Failed to initialize init_options: %s", rcl_get_error_string().str);
rcl_reset_error();
}
}
if (NULL != arg_values) {
Expand Down
9 changes: 9 additions & 0 deletions rclpy/test/test_init_shutdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def test_init():
rclpy.shutdown(context=context)


def test_init_with_unknown_ros_args():
from rclpy.impl.implementation_singleton import rclpy_implementation as _rclpy

context = rclpy.context.Context()
unknown_ros_args_error_pattern = r'Found unknown ROS arguments:.*\[\'unknown\'\]'
with pytest.raises(_rclpy.UnknownROSArgsError, match=unknown_ros_args_error_pattern):
rclpy.init(context=context, args=['--ros-args', 'unknown'])


def test_init_with_non_utf8_arguments():
context = rclpy.context.Context()
# Embed non decodable characters e.g. due to
Expand Down

0 comments on commit d2c30d9

Please sign in to comment.