diff --git a/tests/test_parser.py b/tests/test_parser.py index 375932e..338eb0d 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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) diff --git a/tomlkit/parser.py b/tomlkit/parser.py index b952b36..165bb2e 100644 --- a/tomlkit/parser.py +++ b/tomlkit/parser.py @@ -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 == ",":