From dbd0aa395f480ef249cff9dc9add54a3035fc9e2 Mon Sep 17 00:00:00 2001 From: Bob Ippolito Date: Tue, 4 Apr 2023 13:37:21 -0700 Subject: SJ-PT-23-100: Fix inconsistencies in error messages between C and Python implementations --- CHANGES.txt | 2 ++ simplejson/_speedups.c | 8 ++++---- simplejson/decoder.py | 5 +++-- simplejson/tests/test_fail.py | 4 +++- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index b70200f..270b22e 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -6,6 +6,8 @@ Version 3.19.0 released 2023-04-XX an exception in Python 2.x; was probably unreachable (SJ-PT-23-02) * Backport the integer string length limitation from Python 3.11 to limit quadratic number parsing (SJ-PT-23-03) +* Fix inconsistencies with error messages between the C and Python + implementations (SJ-PT-23-100) Version 3.18.4 released 2023-03-14 diff --git a/simplejson/_speedups.c b/simplejson/_speedups.c index 43c589b..e08f4c4 100644 --- a/simplejson/_speedups.c +++ b/simplejson/_speedups.c @@ -1886,7 +1886,7 @@ _match_number_str(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssiz /* read a sign if it's there, make sure it's not the end of the string */ if (str[idx] == '-') { if (idx >= end_idx) { - raise_errmsg(ERR_EXPECTING_VALUE, pystr, idx); + raise_errmsg(ERR_EXPECTING_VALUE, pystr, start); return NULL; } idx++; @@ -1903,7 +1903,7 @@ _match_number_str(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssiz } /* no integer digits, error */ else { - raise_errmsg(ERR_EXPECTING_VALUE, pystr, idx); + raise_errmsg(ERR_EXPECTING_VALUE, pystr, start); return NULL; } @@ -1995,7 +1995,7 @@ _match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ /* read a sign if it's there, make sure it's not the end of the string */ if (PyUnicode_READ(kind, str, idx) == '-') { if (idx >= end_idx) { - raise_errmsg(ERR_EXPECTING_VALUE, pystr, idx); + raise_errmsg(ERR_EXPECTING_VALUE, pystr, start); return NULL; } idx++; @@ -2015,7 +2015,7 @@ _match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ } else { /* no integer digits, error */ - raise_errmsg(ERR_EXPECTING_VALUE, pystr, idx); + raise_errmsg(ERR_EXPECTING_VALUE, pystr, start); return NULL; } diff --git a/simplejson/decoder.py b/simplejson/decoder.py index c28e445..bbde82c 100644 --- a/simplejson/decoder.py +++ b/simplejson/decoder.py @@ -93,6 +93,7 @@ def py_scanstring(s, end, encoding=None, strict=True, if chunk is None: raise JSONDecodeError( "Unterminated string starting at", s, begin) + prev_end = end end = chunk.end() content, terminator = chunk.groups() # Content is contains zero or more unescaped string characters @@ -107,7 +108,7 @@ def py_scanstring(s, end, encoding=None, strict=True, elif terminator != '\\': if strict: msg = "Invalid control character %r at" - raise JSONDecodeError(msg, s, end) + raise JSONDecodeError(msg, s, prev_end) else: _append(terminator) continue @@ -178,7 +179,7 @@ def JSONObject(state, encoding, strict, scan_once, object_hook, return pairs, end + 1 elif nextchar != '"': raise JSONDecodeError( - "Expecting property name enclosed in double quotes", + "Expecting property name enclosed in double quotes or '}'", s, end) end += 1 while True: diff --git a/simplejson/tests/test_fail.py b/simplejson/tests/test_fail.py index 788f3a5..5f9a8f6 100644 --- a/simplejson/tests/test_fail.py +++ b/simplejson/tests/test_fail.py @@ -145,7 +145,7 @@ class TestFail(TestCase): ('["spam', 'Unterminated string starting at', 1), ('["spam"', "Expecting ',' delimiter", 7), ('["spam",', 'Expecting value', 8), - ('{', 'Expecting property name enclosed in double quotes', 1), + ('{', "Expecting property name enclosed in double quotes or '}'", 1), ('{"', 'Unterminated string starting at', 1), ('{"spam', 'Unterminated string starting at', 1), ('{"spam"', "Expecting ':' delimiter", 7), @@ -156,6 +156,8 @@ class TestFail(TestCase): ('"', 'Unterminated string starting at', 0), ('"spam', 'Unterminated string starting at', 0), ('[,', "Expecting value", 1), + ('--', 'Expecting value', 0), + ('"\x18d', "Invalid control character %r", 1), ] for data, msg, idx in test_cases: try: -- cgit v1.2.1