Skip to content

Commit

Permalink
Catch value error
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Sep 13, 2024
1 parent e75796f commit b0771d5
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/jsonyx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (C) 2024 Nice Zombies
"""Customizable JSON library for Python."""
# TODO(Nice Zombies): add release date in changelog
# TODO(Nice Zomebies): explain how to use large numbers
# TODO(Nice Zombies): update raised exceptions
from __future__ import annotations

Expand Down
6 changes: 5 additions & 1 deletion src/jsonyx/_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,11 @@ def scan_value(filename: str, s: str, idx: int) -> tuple[Any, int]:
integer, frac, exp = number.groups()
end = number.end()
if not frac and not exp:
value = int(integer)
try:
value = int(integer)
except ValueError:
msg = "Number is too big"
raise _errmsg(msg, filename, s, idx, end) from None
else:
try:
value = parse_float(
Expand Down
46 changes: 40 additions & 6 deletions src/jsonyx/_manipulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ def _scan_query_value(self, s: str, idx: int = 0) -> tuple[Any, int]:
integer, frac, exp = number.groups()
end = number.end()
if not frac and not exp:
value = int(integer)
try:
value = int(integer)
except ValueError:
msg = "Number is too big"
raise _errmsg(msg, s, idx, end) from None
else:
try:
value = self._parse_float(
Expand Down Expand Up @@ -346,17 +350,47 @@ def _run_select_query(
end += 1
if match := _match_slice(query, end):
(start, stop, step), end = match.groups(), match.end()
key = slice(
start and int(start), stop and int(stop),
step and int(step),
)
try:
if start is not None:
start = int(start)
except ValueError:
msg = "Start is too big"
raise _errmsg(
msg, query, match.start(1), match.end(1),
) from None

try:
if stop is not None:
stop = int(stop)
except ValueError:
msg = "Stop is too big"
raise _errmsg(
msg, query, match.start(2), match.end(2),
) from None

try:
if step is not None:
step = int(step)
except ValueError:
msg = "Step is too big"
raise _errmsg(
msg, query, match.start(3), match.end(3),
) from None

key = slice(start, stop, step)
nodes = [
(target, key)
for node in nodes
for target in _get_query_targets(node, mapping=mapping)
]
elif match := _match_idx(query, end):
key, end = int(match.group()), match.end()
end = match.end()
try:
key = int(match.group())
except ValueError:
msg = "Index is too big"
raise _errmsg(msg, query, match.start(), end) from None

nodes = [
(target, key)
for node in nodes
Expand Down
16 changes: 12 additions & 4 deletions src/jsonyx/_speedups.c
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ _match_number_unicode(PyScannerObject *s, PyObject *pyfilename, PyObject *pystr,
if (PyErr_ExceptionMatches(PyExc_ArithmeticError)) {
PyErr_Clear();
raise_errmsg("Number is too big", pyfilename, pystr, start, idx);
return NULL;
goto bail;
}
}
else {
Expand All @@ -999,18 +999,26 @@ _match_number_unicode(PyScannerObject *s, PyObject *pyfilename, PyObject *pystr,
if (is_float) {
rval = PyFloat_FromString(numstr);
if (!isfinite(PyFloat_AS_DOUBLE(rval))) {
Py_DECREF(numstr);
Py_DECREF(rval);
raise_errmsg("Big numbers require decimal", pyfilename, pystr, start, idx);
return NULL;
goto bail;
}
}
else
else {
rval = PyLong_FromString(buf, NULL, 10);
if (PyErr_ExceptionMatches(PyExc_ValueError)) {
PyErr_Clear();
raise_errmsg("Number is too big", pyfilename, pystr, start, idx);
goto bail;
}
}
}
Py_DECREF(numstr);
*next_idx_ptr = idx;
return rval;
bail:
Py_DECREF(numstr);
return NULL;
}

static PyObject *
Expand Down
1 change: 1 addition & 0 deletions src/jsonyx/test/test_load_query_value.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (C) 2024 Nice Zombies
"""JSON load_query_value tests."""
# TODO(Nice Zombies): test integer conversion
from __future__ import annotations

__all__: list[str] = []
Expand Down
1 change: 1 addition & 0 deletions src/jsonyx/test/test_loads.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (C) 2024 Nice Zombies
"""JSON loads tests."""
# TODO(Nice Zombies): test integer conversion
from __future__ import annotations

__all__: list[str] = []
Expand Down
1 change: 1 addition & 0 deletions src/jsonyx/test/test_run_select_query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (C) 2024 Nice Zombies
"""JSON run_select_query tests."""
# TODO(Nice Zombies): add more tests
# TODO(Nice Zombies): test integer conversion
from __future__ import annotations

__all__: list[str] = []
Expand Down

0 comments on commit b0771d5

Please sign in to comment.