Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 802d669

Browse files
committed
Issue #5067: improve some json error messages.
Patch by Serhiy Storchaka.
2 parents 3a3dc17 + 2d24e94 commit 802d669

5 files changed

Lines changed: 14 additions & 12 deletions

File tree

Doc/library/json.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Using json.tool from the shell to validate and pretty-print::
9999
"json": "obj"
100100
}
101101
$ echo '{1.2:3.4}' | python -mjson.tool
102-
Expecting property name: line 1 column 1 (char 1)
102+
Expecting property name enclosed in double quotes: line 1 column 1 (char 1)
103103

104104
.. highlight:: python3
105105

Lib/json/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"json": "obj"
9898
}
9999
$ echo '{ 1.2:3.4}' | python -m json.tool
100-
Expecting property name: line 1 column 2 (char 2)
100+
Expecting property name enclosed in double quotes: line 1 column 2 (char 2)
101101
"""
102102
__version__ = '2.0.9'
103103
__all__ = [

Lib/json/decoder.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
172172
pairs = object_hook(pairs)
173173
return pairs, end + 1
174174
elif nextchar != '"':
175-
raise ValueError(errmsg("Expecting property name", s, end))
175+
raise ValueError(errmsg(
176+
"Expecting property name enclosed in double quotes", s, end))
176177
end += 1
177178
while True:
178179
key, end = scanstring(s, end, strict)
@@ -182,7 +183,7 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
182183
if s[end:end + 1] != ':':
183184
end = _w(s, end).end()
184185
if s[end:end + 1] != ':':
185-
raise ValueError(errmsg("Expecting : delimiter", s, end))
186+
raise ValueError(errmsg("Expecting ':' delimiter", s, end))
186187
end += 1
187188

188189
try:
@@ -210,12 +211,13 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
210211
if nextchar == '}':
211212
break
212213
elif nextchar != ',':
213-
raise ValueError(errmsg("Expecting , delimiter", s, end - 1))
214+
raise ValueError(errmsg("Expecting ',' delimiter", s, end - 1))
214215
end = _w(s, end).end()
215216
nextchar = s[end:end + 1]
216217
end += 1
217218
if nextchar != '"':
218-
raise ValueError(errmsg("Expecting property name", s, end - 1))
219+
raise ValueError(errmsg(
220+
"Expecting property name enclosed in double quotes", s, end - 1))
219221
if object_pairs_hook is not None:
220222
result = object_pairs_hook(pairs)
221223
return result, end
@@ -249,7 +251,7 @@ def JSONArray(s_and_end, scan_once, _w=WHITESPACE.match, _ws=WHITESPACE_STR):
249251
if nextchar == ']':
250252
break
251253
elif nextchar != ',':
252-
raise ValueError(errmsg("Expecting , delimiter", s, end))
254+
raise ValueError(errmsg("Expecting ',' delimiter", s, end))
253255
try:
254256
if s[end] in _ws:
255257
end += 1

Lib/json/tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"json": "obj"
88
}
99
$ echo '{ 1.2:3.4}' | python -m json.tool
10-
Expecting property name: line 1 column 2 (char 2)
10+
Expecting property name enclosed in double quotes: line 1 column 2 (char 2)
1111
1212
"""
1313
import sys

Modules/_json.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
646646

647647
/* read key */
648648
if (PyUnicode_READ(kind, str, idx) != '"') {
649-
raise_errmsg("Expecting property name", pystr, idx);
649+
raise_errmsg("Expecting property name enclosed in double quotes", pystr, idx);
650650
goto bail;
651651
}
652652
key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
@@ -667,7 +667,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
667667
/* skip whitespace between key and : delimiter, read :, skip whitespace */
668668
while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
669669
if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') {
670-
raise_errmsg("Expecting : delimiter", pystr, idx);
670+
raise_errmsg("Expecting ':' delimiter", pystr, idx);
671671
goto bail;
672672
}
673673
idx++;
@@ -707,7 +707,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
707707
break;
708708
}
709709
else if (PyUnicode_READ(kind, str, idx) != ',') {
710-
raise_errmsg("Expecting , delimiter", pystr, idx);
710+
raise_errmsg("Expecting ',' delimiter", pystr, idx);
711711
goto bail;
712712
}
713713
idx++;
@@ -797,7 +797,7 @@ _parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssi
797797
break;
798798
}
799799
else if (PyUnicode_READ(kind, str, idx) != ',') {
800-
raise_errmsg("Expecting , delimiter", pystr, idx);
800+
raise_errmsg("Expecting ',' delimiter", pystr, idx);
801801
goto bail;
802802
}
803803
idx++;

0 commit comments

Comments
 (0)