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

Fix #204:decoder.loads:detection for triple quote ending not correct on non stripped line #293

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,15 @@ def test_comment_preserve_decoder_encoder():
encoder=toml.TomlPreserveCommentEncoder())

assert len(s) == len(test_str) and sorted(test_str) == sorted(s)


@pytest.mark.xfail
def test_checks_on_triple_quote_fail():
assert toml.loads('foo="""1\\\n2"""# toto ')['foo'] == "1\\\n2"


def test_checks_on_triple_quote():
assert toml.loads('foo="""1\n2"""# toto ')['foo'] == "1\n2"
assert toml.loads('foo="""1\n2\n"""# toto ')['foo'] == "1\n2\n"
assert toml.loads('foo=["""1\n2"""]# toto ')['foo'] == ["1\n2"]
assert toml.loads('foo=["""1\n2"""# toto\n]# toto')['foo'] == ["1\n2"]
15 changes: 8 additions & 7 deletions toml/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,18 +375,19 @@ def loads(s, _dict=dict, decoder=None):
if line == "" and (not multikey or multibackslash):
continue
if multikey:
closed = False
line_clean_end = line.rstrip()
if multilinestr[0] == '[':
closed = line_clean_end[-1] == ']'
elif len(line) > 2:
closed = line_clean_end[-3:] == multilinestr[:3]
if closed:
line = line_clean_end
if multibackslash:
multilinestr += line
else:
multilinestr += line
multibackslash = False
closed = False
if multilinestr[0] == '[':
closed = line[-1] == ']'
elif len(line) > 2:
closed = (line[-1] == multilinestr[0] and
line[-2] == multilinestr[0] and
line[-3] == multilinestr[0])
if closed:
try:
value, vtype = decoder.load_value(multilinestr)
Expand Down