Skip to content

Commit

Permalink
Handle empty strings when evaluating parameters (#300) (#302)
Browse files Browse the repository at this point in the history
If a substitution evaluates to the empty string, then yaml.safe_load() will return None, resulting
in an exception being raised. Instead, this commit allows for the empty string by first wrapping
it in an extra set of quotes before passing to yaml.safe_load().

Signed-off-by: Jacob Perron <[email protected]>
  • Loading branch information
jacobperron authored Mar 7, 2022
1 parent 21a9dde commit a58b2e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions launch_ros/launch_ros/utilities/evaluate_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ def check_sequence_type_is_allowed(sequence):
# Value is a list of substitutions, so perform them to make a string
evaluated_value = perform_substitutions(context, list(value))

# Handle special case where yaml.safe_load will return None given an empty string
if len(evaluated_value) == 0:
evaluated_value = "''"

try:
yaml_evaluated_value = yaml.safe_load(evaluated_value)
except yaml.YAMLError:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,11 @@ def test_unallowed_yaml_types_as_strings():
evaluate_parameters(LaunchContext(), norm)
expected = ({'foo': 1, 'fiz': 'Text That : Cannot Be Parsed As : Yaml'},)
assert evaluate_parameters(LaunchContext(), norm) == expected


def test_empty_string_evalutates_to_empty_string():
# Regression test for https://github.com/ros2/launch_ros/pull/289#discussion_r818070166
orig = [{'foo': TextSubstitution(text='')}]
norm = normalize_parameters(orig)
expected = ({'foo': ''},)
assert evaluate_parameters(LaunchContext(), norm) == expected

0 comments on commit a58b2e6

Please sign in to comment.