Skip to content
This repository was archived by the owner on May 31, 2025. It is now read-only.

Add escaping for strings which is valid in YAML #79

Merged
dirk-thomas merged 5 commits intoros:kinetic-develfrom
DorianScholz:kinetic-devel
Jul 24, 2017
Merged

Add escaping for strings which is valid in YAML #79
dirk-thomas merged 5 commits intoros:kinetic-develfrom
DorianScholz:kinetic-devel

Conversation

@DorianScholz
Copy link
Copy Markdown
Contributor

Feeding output of "rostopic echo" back into "rostopic pub" does not work properly with strings containing characters which have special meaning in YAML (e.g. '#', "'" or '"').
These chars should be escaped properly to make the output of "rostopic echo" and the string representation of messages in general valid YAML code.

Strings are not properly escaped in strify_message, so publishing a string like e.g. 'foo # bar':

$ rostopic pub -1 /string std_msgs/String "data: 'foo # bar'"

results in YAML code where the string only contains "foo " and "# bar" is treated as a comment, changing the data without even raising an error:

$ rostopic echo /string
data: foo # bar
---

After applying this PR the correct result is printed

$ rostopic echo /string
data: 'foo # bar'
---

This also holds for enclosed "'" and '"' characters.
To test this I also updated the test_strify_yaml to check for proper escaping.

package.xml Outdated
<run_depend>genmsg</run_depend>
<run_depend>python-yaml</run_depend>

<test_depend>python-yaml</test_depend>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the added run dependency this test dependency need to be removed, see current CI failures:

The test dependency on "python-yaml" is redundant with: run_depend

@DorianScholz
Copy link
Copy Markdown
Contributor Author

Seems to build now ;-)

return "''"
return val
# escape strings for use in yaml file using yaml dump
return yaml.dump(val).split('\n')[0]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dorian, is there a particular reason to only print the first line? It would be important for me to also handle multi-line strings.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks indeed like a problem. If the output of dump is being wrapped in the middle the first line on its own is even not valid.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about the late reply, must have over looked the notification while traveling...
You're right, I did not fix it for multi-line strings, yet.
My last commit now solves this as well.

return "''"
return val
# escape strings for use in yaml file using yaml dump with default style to avoid trailing "...\n"
return yaml.dump(val, default_style='"').rstrip('\n')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reading the comment I have one question: with default_style='"' does it still need the rstrip('\n')?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the new style just removes the "\n..." there is still an additional "\n" at the end...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying!


def test_strify_yaml(self):
def roundtrip(m):
#print('\n\n\nroundtrip')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please remove the added but commented lines.

@dirk-thomas
Copy link
Copy Markdown
Member

We are aiming for a new round of releases early next week (Mon or Tue). It would be great f this would be ready before in order to get released.

# TODO: escape strings properly
elif isstring(val0):
# escape list of strings for use in yaml file using yaml dump
return yaml.dump(val).rstrip('\n')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this line also use default_style='"'?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, its not needed here since val is a list of strings which will never be serialized using the "\n...\n" format. (this format is used only for "simple" strings, with no characters that need escaping...)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying!

@dirk-thomas dirk-thomas merged commit 4e813e7 into ros:kinetic-devel Jul 24, 2017
@dirk-thomas
Copy link
Copy Markdown
Member

Thank you for the patch!

@DorianScholz
Copy link
Copy Markdown
Contributor Author

Thanks for the thorough reviews!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants