Skip to content
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
17 changes: 17 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ def test_malformed_array_still_raises() -> None:
Parser(content).parse()


def test_array_with_malformed_element_raises() -> None:
# An element that starts to parse like a value but then hits an
# unexpected char (e.g. an inline table missing its "=" or "}") must
# surface the error instead of being swallowed and silently dropped,
# which left the array truncated to whatever parsed before it.
for content in (
"a = [{1]",
"a = [{a]",
"a = [1, {2]",
"a = [{a = 1}, {2]",
"a = [[1, {x]]",
"a = [{a.b]",
):
with pytest.raises(UnexpectedCharError):
Parser(content).parse()


def test_parse_multiline_string_ignore_the_first_newline() -> None:
content = 'a = """\nfoo\n"""'
parser = Parser(content)
Expand Down
9 changes: 3 additions & 6 deletions tomlkit/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,12 +665,9 @@ def _parse_array(self) -> Array:
# exception eagerly computes a line/column, which scans the whole
# source. On a large file with many arrays this is a big, pure waste.
if not prev_value and self._current != "]":
try:
elems.append(self._parse_value())
prev_value = True
continue
except UnexpectedCharError:
pass
elems.append(self._parse_value())
prev_value = True
continue

# consume comma
if prev_value and self._current == ",":
Expand Down
Loading