Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcfa25fe

Browse files
authored
gh-113149: Improve error message when JSON has trailing comma (GH-113227)
1 parent21d5299 commitcfa25fe

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

‎Lib/json/decoder.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,13 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
200200
break
201201
elifnextchar!=',':
202202
raiseJSONDecodeError("Expecting ',' delimiter",s,end-1)
203+
comma_idx=end-1
203204
end=_w(s,end).end()
204205
nextchar=s[end:end+1]
205206
end+=1
206207
ifnextchar!='"':
208+
ifnextchar=='}':
209+
raiseJSONDecodeError("Illegal trailing comma before end of object",s,comma_idx)
207210
raiseJSONDecodeError(
208211
"Expecting property name enclosed in double quotes",s,end-1)
209212
ifobject_pairs_hookisnotNone:
@@ -240,13 +243,17 @@ def JSONArray(s_and_end, scan_once, _w=WHITESPACE.match, _ws=WHITESPACE_STR):
240243
break
241244
elifnextchar!=',':
242245
raiseJSONDecodeError("Expecting ',' delimiter",s,end-1)
246+
comma_idx=end-1
243247
try:
244248
ifs[end]in_ws:
245249
end+=1
246250
ifs[end]in_ws:
247251
end=_w(s,end+1).end()
252+
nextchar=s[end:end+1]
248253
exceptIndexError:
249254
pass
255+
ifnextchar==']':
256+
raiseJSONDecodeError("Illegal trailing comma before end of array",s,comma_idx)
250257

251258
returnvalues,end
252259

‎Lib/test/test_json/test_fail.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ def test_unexpected_data(self):
143143
('{"spam":[}','Expecting value',9),
144144
('[42:',"Expecting ',' delimiter",3),
145145
('[42 "spam"',"Expecting ',' delimiter",4),
146-
('[42,]','Expecting value',4),
146+
('[42,]',"Illegal trailing comma before end of array",3),
147147
('{"spam":[42}',"Expecting ',' delimiter",11),
148148
('["]','Unterminated string starting at',1),
149149
('["spam":',"Expecting ',' delimiter",7),
150-
('["spam",]','Expecting value',8),
150+
('["spam",]',"Illegal trailing comma before end of array",7),
151151
('{:','Expecting property name enclosed in double quotes',1),
152152
('{,','Expecting property name enclosed in double quotes',1),
153153
('{42','Expecting property name enclosed in double quotes',1),
@@ -159,7 +159,9 @@ def test_unexpected_data(self):
159159
('[{"spam":]','Expecting value',9),
160160
('{"spam":42 "ham"',"Expecting ',' delimiter",11),
161161
('[{"spam":42]',"Expecting ',' delimiter",11),
162-
('{"spam":42,}','Expecting property name enclosed in double quotes',11),
162+
('{"spam":42,}',"Illegal trailing comma before end of object",10),
163+
('{"spam":42 , }',"Illegal trailing comma before end of object",11),
164+
('[123 , ]',"Illegal trailing comma before end of array",6),
163165
]
164166
fordata,msg,idxintest_cases:
165167
withself.assertRaises(self.JSONDecodeError)ascm:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve error message when a JSON array or object contains a trailing comma.
2+
Patch by Carson Radtke.

‎Modules/_json.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *memo, PyObject *pystr, Py_ss
662662
PyObject*key=NULL;
663663
inthas_pairs_hook= (s->object_pairs_hook!=Py_None);
664664
Py_ssize_tnext_idx;
665+
Py_ssize_tcomma_idx;
665666

666667
str=PyUnicode_DATA(pystr);
667668
kind=PyUnicode_KIND(pystr);
@@ -741,10 +742,16 @@ _parse_object_unicode(PyScannerObject *s, PyObject *memo, PyObject *pystr, Py_ss
741742
raise_errmsg("Expecting ',' delimiter",pystr,idx);
742743
gotobail;
743744
}
745+
comma_idx=idx;
744746
idx++;
745747

746748
/* skip whitespace after , delimiter */
747749
while (idx <=end_idx&&IS_WHITESPACE(PyUnicode_READ(kind,str,idx)))idx++;
750+
751+
if (idx <=end_idx&&PyUnicode_READ(kind,str,idx)=='}') {
752+
raise_errmsg("Illegal trailing comma before end of object",pystr,comma_idx);
753+
gotobail;
754+
}
748755
}
749756
}
750757

@@ -785,6 +792,7 @@ _parse_array_unicode(PyScannerObject *s, PyObject *memo, PyObject *pystr, Py_ssi
785792
PyObject*val=NULL;
786793
PyObject*rval;
787794
Py_ssize_tnext_idx;
795+
Py_ssize_tcomma_idx;
788796

789797
rval=PyList_New(0);
790798
if (rval==NULL)
@@ -822,10 +830,16 @@ _parse_array_unicode(PyScannerObject *s, PyObject *memo, PyObject *pystr, Py_ssi
822830
raise_errmsg("Expecting ',' delimiter",pystr,idx);
823831
gotobail;
824832
}
833+
comma_idx=idx;
825834
idx++;
826835

827836
/* skip whitespace after , */
828837
while (idx <=end_idx&&IS_WHITESPACE(PyUnicode_READ(kind,str,idx)))idx++;
838+
839+
if (idx <=end_idx&&PyUnicode_READ(kind,str,idx)==']') {
840+
raise_errmsg("Illegal trailing comma before end of array",pystr,comma_idx);
841+
gotobail;
842+
}
829843
}
830844
}
831845

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp