Skip to content

Commit ea6da90

Browse files
committed
Raise for index selector out of range.
1 parent 076828b commit ea6da90

File tree

5 files changed

+16
-0
lines changed

5 files changed

+16
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Removed support for dotted index selectors.
2020
- Raise a `JSONPathSyntaxError` for unescaped whitespace and control characters.
2121
- Raise a `JSONPathSyntaxError` for empty selector segments.
22+
- Raise a `JSONPathIndexError` if an index selector is out of range.
2223

2324
## Version 0.4.0
2425

jsonpath/env.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class attributes `root_token`, `self_token` and `filter_context_token`.
8585
union_token: str = "|"
8686
filter_context_token: str = "#"
8787

88+
max_int_index = (2**53) - 1
89+
min_int_index = -(2**53) + 1
90+
8891
# Override these to customize path tokenization and parsing.
8992
lexer_class: Type[Lexer] = Lexer
9093
parser_class: Type[Parser] = Parser

jsonpath/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class JSONPathTypeError(JSONPathError):
4545
"""
4646

4747

48+
class JSONPathIndexError(JSONPathError):
49+
"""An exception raised when an array index is out of range."""
50+
51+
4852
class JSONPathNameError(JSONPathError):
4953
"""An exception raised when an unknown function extension is called."""
5054

jsonpath/selectors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from typing import TypeVar
1616
from typing import Union
1717

18+
from .exceptions import JSONPathIndexError
1819
from .exceptions import JSONPathTypeError
1920
from .match import JSONPathMatch
2021

@@ -113,6 +114,9 @@ def __init__(
113114
token: Token,
114115
index: int,
115116
) -> None:
117+
if index < env.min_int_index or index > env.max_int_index:
118+
raise JSONPathIndexError("index out of range", token=token)
119+
116120
super().__init__(env=env, token=token)
117121
self.index = index
118122
self._as_key = str(self.index)
@@ -200,6 +204,7 @@ def __init__(
200204
stop: Optional[int] = None,
201205
step: Optional[int] = None,
202206
) -> None:
207+
# TODO: raise if start, stop or step are out of range
203208
super().__init__(env=env, token=token)
204209
self.slice = slice(start, stop, step)
205210

tests/compliance.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,6 @@ def test_invalid_selectors(case: Case) -> None:
8787

8888
with pytest.raises(jsonpath.JSONPathError):
8989
jsonpath.compile(case.selector)
90+
91+
92+
# TODO: async invalid cases

0 commit comments

Comments
 (0)