-
-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Escape characters lost after concatenating string values #231
Comments
import tomlkit
content = """\
[default.deploy.parameters]
parameter_overrides = "<parameter_name>=\\"<parameter_value>\\""
"""
print(tomlkit.dumps(tomlkit.loads(content))) Results in:
Can you check it again? |
After trying out your suggestion, I found some strange behavior:
Conclusion - finalIt is in my opinion quite confusing that strings behave in so many different ways depending on how you load them and whether or not the strings are altered on between loading and dumping the Is it possible to unify the behavior of how backslashes are parsed in strings? |
In a python string literal, which appears between a pair of quotes in a python source code, backslashes are escape characters, >>> '\"'
'"' so the second attempt is an invalid toml document. In a toml document, if quotes appear in between a pair of quotes, they must be escaped by a If you are still confused after my explanation that is okay, I might not be good at it. Just read more Python manuals and try and you will understand it after you are more experienced. I am just going to close this issue now. |
Ok, so it seems great that the characters are read as-is when reading # config.toml
[default.deploy.parameters]
parameter_overrides = "<parameter_name>=\"<parameter_value>\"" With this test code: import tomlkit
from pprint import pprint as pp
with open('config.toml', 'rt', encoding='utf-8') as f:
config = tomlkit.load(f)
print('Config that was loaded')
pp(config)
print()
print('Dumps version of config before adjustment')
print(tomlkit.dumps(config))
print()
config['default']['deploy']['parameters']['parameter_overrides'] \
+= f' <parameter_name_2>=\\"<parameter_value_2>\\"'
print('Dumps version of config after adjustment')
print(tomlkit.dumps(config))
print()
with open('test.toml', 'wt', encoding='utf-8') as f:
tomlkit.dump(config, f)
with open('test.toml', 'r') as f:
print('\nSaved config')
print(f.read()) you will see this output:
and the resulting # test.toml
[default.deploy.parameters]
parameter_overrides = "<parameter_name>="<parameter_value>" <parameter_name_2>=\"<parameter_value_2>\"" The double quotes around parameter_value_2 are correctly escaped, but the double quotes around the original parameter_value are lost, creating a broken string, as is seen in |
This is wrong, the double backslashes are only needed in a TOML document, BEFORE parsing. After parsed, the string will become import tomlkit
content = """\
[default.deploy.parameters]
parameter_overrides = "<parameter_name>=\\"<parameter_value>\\""
"""
doc = tomlkit.parse(content)
doc['default']['deploy']['parameters']['parameter_overrides']
# Output: '<parameter_name>="<parameter_value>"' |
Oh, I got, when the string is updated, the back slashes are gone in dumps result |
Yes indeed, and then in the resulting |
The way I initially solved it was by doing this statement: config['default']['deploy']['parameters']['parameter_overrides'] \
= config['default']['deploy']['parameters']['parameter_overrides'].replace('"', '\"') \
+ f' <parameter_name_2>=\"<parameter_value_2>\"' and then I indeed get a correct # test.toml
[default.deploy.parameters]
parameter_overrides = "<parameter_name>=\"<parameter_value>\" <parameter_name_2>=\"<parameter_value_2>\"" Which seems again weird to me, because now I use single backslashes and it also works. However, the replace statement is very clunky and also, because it now works with single backslashes as well, was confusing to me. |
When working with AWS samconfig.toml, you have to specify parameter overrides as literal strings:
When loading this
.toml
withtomlkit
, the backslashes are lost in translation. So a roundtrip:tomlkit.dump(tomlkit.load(config))
results in:And this breaks the
parameter_overrides
string, since the intermediate"
now break apart the string.How is it possible to load strings as literal strings using
tomlkit
?The text was updated successfully, but these errors were encountered: