Skip to content

Commit 476ff34

Browse files
authored
Handle empty strings when evaluating parameters (#300)
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]>
1 parent db360e0 commit 476ff34

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

launch_ros/launch_ros/utilities/evaluate_parameters.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def check_sequence_type_is_allowed(sequence):
7171
# Value is a list of substitutions, so perform them to make a string
7272
evaluated_value = perform_substitutions(context, list(value))
7373

74+
# Handle special case where yaml.safe_load will return None given an empty string
75+
if len(evaluated_value) == 0:
76+
evaluated_value = "''"
77+
7478
try:
7579
yaml_evaluated_value = yaml.safe_load(evaluated_value)
7680
except yaml.YAMLError:

test_launch_ros/test/test_launch_ros/utilities/test_normalize_parameters.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,11 @@ def test_unallowed_yaml_types_as_strings():
396396
evaluate_parameters(LaunchContext(), norm)
397397
expected = ({'foo': 1, 'fiz': 'Text That : Cannot Be Parsed As : Yaml'},)
398398
assert evaluate_parameters(LaunchContext(), norm) == expected
399+
400+
401+
def test_empty_string_evalutates_to_empty_string():
402+
# Regression test for https://github.com/ros2/launch_ros/pull/289#discussion_r818070166
403+
orig = [{'foo': TextSubstitution(text='')}]
404+
norm = normalize_parameters(orig)
405+
expected = ({'foo': ''},)
406+
assert evaluate_parameters(LaunchContext(), norm) == expected

0 commit comments

Comments
 (0)