toml icon indicating copy to clipboard operation
toml copied to clipboard

Multiline string literal parsing doesn't handle '' in some cases

Open mac-again opened this issue 5 years ago • 5 comments

I have a toml file:

multiline-string-literal = '''
Text string
  ends in ''
  ends again in ''
  more text
'''

toml.load("file.toml") gives the error :

raise TomlDecodeError(str(err), original, pos)
toml.decoder.TomlDecodeError: Stuff after closed string. WTF? (line 6 column 1 char 87)

However removing one of the lines that ends in '' :

multiline-string-literal = '''
Text string
  ends in ''
  more text
'''

parses, but incorrectly to:

{'multiline-string-literal': "Text string\n  ends in '''  more text\n"}

Note the ''' rather than ''. As this is a string literal '' should be allowed anywhere as long as they do not form a '''

mac-again avatar Feb 11 '20 10:02 mac-again

Seen a similar problem with

description = """
"Hello, World!" demo"""

This key is loaded but a subsequent multiline string gives spurious errors.

mosteo avatar Jul 22 '20 09:07 mosteo

I have a similar problem. Neither of these will load, Unterminated string found.

text = """ He said "How 'bout you?" """
text = ''' He said "How 'bout you?" '''

Removing the single quote means they load.

norm avatar Jul 28 '20 06:07 norm

similar problems here:

[page.page2]
next = '''
(ctx) => ctx.skipped ? ['page1'] : ['page4']
'''

panda2134 avatar Aug 16 '20 12:08 panda2134

My workaround:

          try:
            input_dict = toml.loads(data)
            # Many bugs above.
          except TomlDecodeError as e:
            import qtoml
            input_dict = qtoml.loads(data)

vvasuki avatar May 16 '21 01:05 vvasuki

I'm afraid that this toml library is no longer being maintained. But I think I can suggest a replacement.

Have you tried the tomli library instead? It works a little differently (i.e. you read in the TOML as a binary file), but it will become the basis for the future standard module tomllib in Python 3.11, and it passes all the v1.0.0 tests. For earlier versions of Python, tomli can be dropped in, in place of tomllib.

You run it like this (and this comes from sample code at the project and on PyPI):

import tomli

with open("path_to_file/conf.toml", "rb") as f:
    toml_dict = tomli.load(f)

EDIT: I had the project wrong.

eksortso avatar Oct 23 '22 05:10 eksortso