From 6d2e5db4a67cfbe358f585ac0fc082edd7dddc00 Mon Sep 17 00:00:00 2001 From: jwalith4418-Mj <232281058+jwalith4418-Mj@users.noreply.github.com> Date: Mon, 2 Nov 2026 00:41:00 +0530 Subject: [PATCH] Return an error tuple when parsing empty input Keep loads() raising its existing ValueError while parse() follows its documented tuple-result contract for empty strings and bytes. Prepared with AI assistance; contributor review is required before upload. --- json5/lib.py | 2 +- tests/lib_test.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/json5/lib.py b/json5/lib.py index c7273d3..91c2b46 100644 --- a/json5/lib.py +++ b/json5/lib.py @@ -335,7 +335,7 @@ def parse( s = s.decode(encoding) if not s: - raise ValueError('Empty strings are not legal JSON5') + return None, 'Empty strings are not legal JSON5', 0 start = start or 0 parser = Parser(s, '', pos=start) ast, err, pos = parser.parse( diff --git a/tests/lib_test.py b/tests/lib_test.py index df83ffd..161928a 100644 --- a/tests/lib_test.py +++ b/tests/lib_test.py @@ -533,6 +533,14 @@ def check(self, s, obj, err, pos, consume_trailing=True, start=0): self.assertEqual(err, actual_err) self.assertEqual(pos, actual_pos) + def test_parse_empty_input(self): + for source in ('', b''): + with self.subTest(source=source): + self.assertEqual( + json5.parse(source), + (None, 'Empty strings are not legal JSON5', 0), + ) + def test_parse(self): self.check('4', 4, err=None, pos=1) self.check('4 ', 4, err=None, pos=2)