mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	Issue #5067: improve some json error messages.
Patch by Serhiy Storchaka.
This commit is contained in:
		
							parent
							
								
									24319ac407
								
							
						
					
					
						commit
						2d24e94bbe
					
				
					 5 changed files with 14 additions and 12 deletions
				
			
		| 
						 | 
					@ -99,7 +99,7 @@ Using json.tool from the shell to validate and pretty-print::
 | 
				
			||||||
        "json": "obj"
 | 
					        "json": "obj"
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    $ echo '{1.2:3.4}' | python -mjson.tool
 | 
					    $ echo '{1.2:3.4}' | python -mjson.tool
 | 
				
			||||||
    Expecting property name: line 1 column 1 (char 1)
 | 
					    Expecting property name enclosed in double quotes: line 1 column 1 (char 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. highlight:: python3
 | 
					.. highlight:: python3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -97,7 +97,7 @@
 | 
				
			||||||
        "json": "obj"
 | 
					        "json": "obj"
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    $ echo '{ 1.2:3.4}' | python -m json.tool
 | 
					    $ echo '{ 1.2:3.4}' | python -m json.tool
 | 
				
			||||||
    Expecting property name: line 1 column 2 (char 2)
 | 
					    Expecting property name enclosed in double quotes: line 1 column 2 (char 2)
 | 
				
			||||||
"""
 | 
					"""
 | 
				
			||||||
__version__ = '2.0.9'
 | 
					__version__ = '2.0.9'
 | 
				
			||||||
__all__ = [
 | 
					__all__ = [
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -173,7 +173,8 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
 | 
				
			||||||
                pairs = object_hook(pairs)
 | 
					                pairs = object_hook(pairs)
 | 
				
			||||||
            return pairs, end + 1
 | 
					            return pairs, end + 1
 | 
				
			||||||
        elif nextchar != '"':
 | 
					        elif nextchar != '"':
 | 
				
			||||||
            raise ValueError(errmsg("Expecting property name", s, end))
 | 
					            raise ValueError(errmsg(
 | 
				
			||||||
 | 
					                "Expecting property name enclosed in double quotes", s, end))
 | 
				
			||||||
    end += 1
 | 
					    end += 1
 | 
				
			||||||
    while True:
 | 
					    while True:
 | 
				
			||||||
        key, end = scanstring(s, end, strict)
 | 
					        key, end = scanstring(s, end, strict)
 | 
				
			||||||
| 
						 | 
					@ -183,7 +184,7 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
 | 
				
			||||||
        if s[end:end + 1] != ':':
 | 
					        if s[end:end + 1] != ':':
 | 
				
			||||||
            end = _w(s, end).end()
 | 
					            end = _w(s, end).end()
 | 
				
			||||||
            if s[end:end + 1] != ':':
 | 
					            if s[end:end + 1] != ':':
 | 
				
			||||||
                raise ValueError(errmsg("Expecting : delimiter", s, end))
 | 
					                raise ValueError(errmsg("Expecting ':' delimiter", s, end))
 | 
				
			||||||
        end += 1
 | 
					        end += 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
| 
						 | 
					@ -211,12 +212,13 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
 | 
				
			||||||
        if nextchar == '}':
 | 
					        if nextchar == '}':
 | 
				
			||||||
            break
 | 
					            break
 | 
				
			||||||
        elif nextchar != ',':
 | 
					        elif nextchar != ',':
 | 
				
			||||||
            raise ValueError(errmsg("Expecting , delimiter", s, end - 1))
 | 
					            raise ValueError(errmsg("Expecting ',' delimiter", s, end - 1))
 | 
				
			||||||
        end = _w(s, end).end()
 | 
					        end = _w(s, end).end()
 | 
				
			||||||
        nextchar = s[end:end + 1]
 | 
					        nextchar = s[end:end + 1]
 | 
				
			||||||
        end += 1
 | 
					        end += 1
 | 
				
			||||||
        if nextchar != '"':
 | 
					        if nextchar != '"':
 | 
				
			||||||
            raise ValueError(errmsg("Expecting property name", s, end - 1))
 | 
					            raise ValueError(errmsg(
 | 
				
			||||||
 | 
					                "Expecting property name enclosed in double quotes", s, end - 1))
 | 
				
			||||||
    if object_pairs_hook is not None:
 | 
					    if object_pairs_hook is not None:
 | 
				
			||||||
        result = object_pairs_hook(pairs)
 | 
					        result = object_pairs_hook(pairs)
 | 
				
			||||||
        return result, end
 | 
					        return result, end
 | 
				
			||||||
| 
						 | 
					@ -250,7 +252,7 @@ def JSONArray(s_and_end, scan_once, _w=WHITESPACE.match, _ws=WHITESPACE_STR):
 | 
				
			||||||
        if nextchar == ']':
 | 
					        if nextchar == ']':
 | 
				
			||||||
            break
 | 
					            break
 | 
				
			||||||
        elif nextchar != ',':
 | 
					        elif nextchar != ',':
 | 
				
			||||||
            raise ValueError(errmsg("Expecting , delimiter", s, end))
 | 
					            raise ValueError(errmsg("Expecting ',' delimiter", s, end))
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            if s[end] in _ws:
 | 
					            if s[end] in _ws:
 | 
				
			||||||
                end += 1
 | 
					                end += 1
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,7 +7,7 @@
 | 
				
			||||||
        "json": "obj"
 | 
					        "json": "obj"
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    $ echo '{ 1.2:3.4}' | python -m json.tool
 | 
					    $ echo '{ 1.2:3.4}' | python -m json.tool
 | 
				
			||||||
    Expecting property name: line 1 column 2 (char 2)
 | 
					    Expecting property name enclosed in double quotes: line 1 column 2 (char 2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
"""
 | 
					"""
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -634,7 +634,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            /* read key */
 | 
					            /* read key */
 | 
				
			||||||
            if (str[idx] != '"') {
 | 
					            if (str[idx] != '"') {
 | 
				
			||||||
                raise_errmsg("Expecting property name", pystr, idx);
 | 
					                raise_errmsg("Expecting property name enclosed in double quotes", pystr, idx);
 | 
				
			||||||
                goto bail;
 | 
					                goto bail;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
 | 
					            key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
 | 
				
			||||||
| 
						 | 
					@ -655,7 +655,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
 | 
				
			||||||
            /* skip whitespace between key and : delimiter, read :, skip whitespace */
 | 
					            /* skip whitespace between key and : delimiter, read :, skip whitespace */
 | 
				
			||||||
            while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
 | 
					            while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
 | 
				
			||||||
            if (idx > end_idx || str[idx] != ':') {
 | 
					            if (idx > end_idx || str[idx] != ':') {
 | 
				
			||||||
                raise_errmsg("Expecting : delimiter", pystr, idx);
 | 
					                raise_errmsg("Expecting ':' delimiter", pystr, idx);
 | 
				
			||||||
                goto bail;
 | 
					                goto bail;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            idx++;
 | 
					            idx++;
 | 
				
			||||||
| 
						 | 
					@ -695,7 +695,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else if (str[idx] != ',') {
 | 
					            else if (str[idx] != ',') {
 | 
				
			||||||
                raise_errmsg("Expecting , delimiter", pystr, idx);
 | 
					                raise_errmsg("Expecting ',' delimiter", pystr, idx);
 | 
				
			||||||
                goto bail;
 | 
					                goto bail;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            idx++;
 | 
					            idx++;
 | 
				
			||||||
| 
						 | 
					@ -777,7 +777,7 @@ _parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssi
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else if (str[idx] != ',') {
 | 
					            else if (str[idx] != ',') {
 | 
				
			||||||
                raise_errmsg("Expecting , delimiter", pystr, idx);
 | 
					                raise_errmsg("Expecting ',' delimiter", pystr, idx);
 | 
				
			||||||
                goto bail;
 | 
					                goto bail;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            idx++;
 | 
					            idx++;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue