diff options
author | Carson Radtke <carsonRadtke@users.noreply.github.com> | 2023-12-17 18:52:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-17 18:52:26 (GMT) |
commit | cfa25fe3e39e09612a6ba8409c46cf35a091de3c (patch) | |
tree | 2cf2db21aa5e11b36fa04b5d93b5aa22ac357bd7 /Lib/json | |
parent | 21d52995ea490328edf9be3ba072821cd445dd30 (diff) | |
download | cpython-cfa25fe3e39e09612a6ba8409c46cf35a091de3c.zip cpython-cfa25fe3e39e09612a6ba8409c46cf35a091de3c.tar.gz cpython-cfa25fe3e39e09612a6ba8409c46cf35a091de3c.tar.bz2 |
gh-113149: Improve error message when JSON has trailing comma (GH-113227)
Diffstat (limited to 'Lib/json')
-rw-r--r-- | Lib/json/decoder.py | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/Lib/json/decoder.py b/Lib/json/decoder.py index c5d9ae2..d69a45d 100644 --- a/Lib/json/decoder.py +++ b/Lib/json/decoder.py @@ -200,10 +200,13 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook, break elif nextchar != ',': raise JSONDecodeError("Expecting ',' delimiter", s, end - 1) + comma_idx = end - 1 end = _w(s, end).end() nextchar = s[end:end + 1] end += 1 if nextchar != '"': + if nextchar == '}': + raise JSONDecodeError("Illegal trailing comma before end of object", s, comma_idx) raise JSONDecodeError( "Expecting property name enclosed in double quotes", s, end - 1) if object_pairs_hook is not None: @@ -240,13 +243,17 @@ def JSONArray(s_and_end, scan_once, _w=WHITESPACE.match, _ws=WHITESPACE_STR): break elif nextchar != ',': raise JSONDecodeError("Expecting ',' delimiter", s, end - 1) + comma_idx = end - 1 try: if s[end] in _ws: end += 1 if s[end] in _ws: end = _w(s, end + 1).end() + nextchar = s[end:end + 1] except IndexError: pass + if nextchar == ']': + raise JSONDecodeError("Illegal trailing comma before end of array", s, comma_idx) return values, end |