diff --git a/tests/test_api.py b/tests/test_api.py index 3623662..d9cbc9a 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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"] diff --git a/toml/decoder.py b/toml/decoder.py index a24c04a..559b762 100644 --- a/toml/decoder.py +++ b/toml/decoder.py @@ -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)