Skip to content

Commit

Permalink
Test invalid string
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Jul 17, 2024
1 parent 634e927 commit f0b5b3d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/jsonyx/_accelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ scanstring_unicode(PyObject *pyfilename, PyObject *pystr, Py_ssize_t end, Py_ssi
break;
}
if (next == len) {
raise_errmsg("Invalid backslash escape", pyfilename, pystr, next);
raise_errmsg("Expecting escaped character", pyfilename, pystr, next);
goto bail;
}
c = PyUnicode_READ(kind, buf, next);
Expand All @@ -491,6 +491,9 @@ scanstring_unicode(PyObject *pyfilename, PyObject *pystr, Py_ssize_t end, Py_ssi
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case '\n':
raise_errmsg("Expecting escaped character", pyfilename, pystr, end - 1);
goto bail;
default: c = 0;
}
if (c == 0) {
Expand Down
9 changes: 7 additions & 2 deletions src/jsonyx/_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def _unescape_unicode(filename: str, s: str, pos: int) -> int:
raise JSONSyntaxError(msg, filename, s, pos)


def _scan_string(filename: str, s: str, end: int) -> ( # noqa: C901
# pylint: disable-next=R0912
def _scan_string(filename: str, s: str, end: int) -> ( # noqa: C901, PLR0912
tuple[str, int]
):
chunks: list[str] = []
Expand Down Expand Up @@ -105,14 +106,18 @@ def _scan_string(filename: str, s: str, end: int) -> ( # noqa: C901
try:
esc = s[end]
except IndexError:
msg = "Invalid backslash escape"
msg = "Expecting escaped character"
raise JSONSyntaxError(msg, filename, s, end) from None

# If not a unicode escape sequence, must be in the lookup table
if esc != "u":
try:
char = _UNESCAPE[esc]
except KeyError:
if esc == "\n":
msg = "Expecting escaped character"
raise JSONSyntaxError(msg, filename, s, end) from None

msg = "Invalid backslash escape"
raise JSONSyntaxError(msg, filename, s, end) from None

Expand Down
23 changes: 23 additions & 0 deletions src/jsonyx/test_jsonyx/test_loads.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,29 @@ def test_string(json: ModuleType, string: str, expected: Any) -> None:
assert json.loads(string) == expected


@pytest.mark.parametrize(("string", "msg", "colno"), [
('"', "Unterminated string", 1),
('"\n', "Unterminated string", 1),
('"\b"', "Unescaped control character", 2),
('"\\', "Expecting escaped character", 3),
('"\\\n', "Expecting escaped character", 3),
(r'"\a"', "Invalid backslash escape", 3),
(r'"\u"', "Expecting 4 hex digits", 4),
(r'"\ud800\u"', "Expecting 4 hex digits", 10),
])
def test_invalid_string(
json: ModuleType, string: str, msg: str, colno: int,
) -> None:
"""Test invalid JSON string."""
with pytest.raises(json.JSONSyntaxError) as exc_info:
json.loads(string)

exc: Any = exc_info.value
assert exc.msg == msg
assert exc.lineno == 1
assert exc.colno == colno


@pytest.mark.parametrize(("string", "expected"), [
# Empty array
("[]", []),
Expand Down

0 comments on commit f0b5b3d

Please sign in to comment.