Skip to content

Commit

Permalink
Fix #204 triple quote detection incorrect on non stripped line
Browse files Browse the repository at this point in the history
triple quote """ or ''' ending a multiline string are not checked
correctly if line ends with comments or spacing or anything
  • Loading branch information
rudy-6-4 committed May 8, 2020
1 parent f4f0b84 commit 87c8c18
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
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

0 comments on commit 87c8c18

Please sign in to comment.