Skip to content

Commit

Permalink
Protect against out-of-range negative indices for RegularTicks.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Feb 1, 2024
1 parent 8ddcc9d commit 87b7525
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/delayedarray/RegularTicks.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ def __getitem__(self, i: int) -> int:
Returns:
Position of tick ``i``.
"""
if i >= self._len:
raise IndexError("'i' is out of range")
elif i < 0:
if i < 0:
i += self._len
if i < 0:
raise IndexError("'i' is out of range")
elif i >= self._len:
raise IndexError("'i' is out of range")
return min(self._final, self._spacing * (i + 1))
6 changes: 6 additions & 0 deletions tests/test_RegularTicks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import delayedarray
import pytest


def test_RegularTicks():
Expand All @@ -8,6 +9,11 @@ def test_RegularTicks():
assert list(out) == ref
assert out[-1] == 50 # check reverse indexing.

with pytest.raises(IndexError, match="out of range") as ex:
out[-10]
with pytest.raises(IndexError, match="out of range") as ex:
out[10]

out = delayedarray.RegularTicks(10, 50)
ref = list(range(10, 51, 10))
assert list(out) == ref
Expand Down

0 comments on commit 87b7525

Please sign in to comment.