toml
toml copied to clipboard
Multiline string literal parsing doesn't handle '' in some cases
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 '''
Seen a similar problem with
description = """
"Hello, World!" demo"""
This key is loaded but a subsequent multiline string gives spurious errors.
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.
similar problems here:
[page.page2]
next = '''
(ctx) => ctx.skipped ? ['page1'] : ['page4']
'''
My workaround:
try:
input_dict = toml.loads(data)
# Many bugs above.
except TomlDecodeError as e:
import qtoml
input_dict = qtoml.loads(data)
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.