Skip to content

Commit 36659d4

Browse files
committed
Catch out of range slice selector arguments.
1 parent 38e4e4e commit 36659d4

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- Raise a `JSONPathSyntaxError` for empty selector segments.
2222
- Raise a `JSONPathIndexError` if an index selector is out of range.
2323
- Raise a `JSONPathSyntaxError` for too many colons in a slice selector.
24+
- Raise a `JSONPathIndexError` if a slice selector argument is out of range.
2425

2526
## Version 0.4.0
2627

jsonpath/selectors.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ def __init__(
204204
stop: Optional[int] = None,
205205
step: Optional[int] = None,
206206
) -> None:
207-
# TODO: raise if start, stop or step are out of range
208207
super().__init__(env=env, token=token)
208+
self._check_range(start, stop, step)
209209
self.slice = slice(start, stop, step)
210210

211211
def __str__(self) -> str:
@@ -214,6 +214,13 @@ def __str__(self) -> str:
214214
step = self.slice.step if self.slice.step is not None else "1"
215215
return f"[{start}:{stop}:{step}]"
216216

217+
def _check_range(self, *indices: Optional[int]) -> None:
218+
for i in indices:
219+
if i is not None and (
220+
i < self.env.min_int_index or i > self.env.max_int_index
221+
):
222+
raise JSONPathIndexError("index out of range")
223+
217224
def _normalized_index(self, obj: Sequence[object], index: int) -> int:
218225
if index < 0 and len(obj) >= abs(index):
219226
return len(obj) + index

0 commit comments

Comments
 (0)