Skip to content
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

as_string(), dumps() incorrectly escape table name "\\" #245

Closed
andreareina opened this issue Oct 24, 2022 · 0 comments
Closed

as_string(), dumps() incorrectly escape table name "\\" #245

andreareina opened this issue Oct 24, 2022 · 0 comments

Comments

@andreareina
Copy link

andreareina commented Oct 24, 2022

The .as_string() should return '["\\\\"]' not '["\\"]', i.e. the on-file document should look like

["\\"]

not

["\"]

reproduction script:

import tomlkit

tomlkit.__version__             # '0.11.5'
doc = tomlkit.TOMLDocument()
doc.add("\\", tomlkit.table())  # {'\\': {}}
doc.as_string()                 # '["\\"]\n'
print(doc.as_string())
tomlkit.loads(doc.as_string())  # error!
tomlkit.loads('["\\\\"]\n')     # {'\\': {}}

interpreter trace:

>>> tomlkit.__version__
'0.11.5'
>>> doc = tomlkit.TOMLDocument()
>>> doc.add("\\", tomlkit.table())
{'\\': {}}
>>> doc.as_string()
'["\\"]\n'
>>> print(doc.as_string())
["\"]

>>> tomlkit.loads(doc.as_string())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "[...]/tomlkit-repro.py", line 7, in <module>
  File "[...]/.venv/lib/python3.8/site-packages/tomlkit/api.py", line 42, in loads
    return parse(string)
  File "[...]/.venv/lib/python3.8/site-packages/tomlkit/api.py", line 82, in parse
    return Parser(string).parse()
  File "[...]/.venv/lib/python3.8/site-packages/tomlkit/parser.py", line 158, in parse
    key, value = self._parse_table()
  File "[...]/.venv/lib/python3.8/site-packages/tomlkit/parser.py", line 906, in _parse_table
    key = self._parse_key()
  File "[...]/.venv/lib/python3.8/site-packages/tomlkit/parser.py", line 362, in _parse_key
    return self._parse_quoted_key()
  File "[...]/.venv/lib/python3.8/site-packages/tomlkit/parser.py", line 378, in _parse_quoted_key
    key_str = self._parse_string(
  File "[...]/.venv/lib/python3.8/site-packages/tomlkit/parser.py", line 825, in _parse_string
    raise self.parse_error(InvalidControlChar, code, "strings")
tomlkit.exceptions.InvalidControlChar: Control characters (codes less than 0x1f and 0x7f) are not allowed in strings, use \u000a instead at line 1 col 5
>>> tomlkit.loads('["\\\\"]\n')
{'\\': {}}
capuanob pushed a commit to capuanob/tomlkit that referenced this issue Mar 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant