Skip to content

Commit a963711

Browse files
committed
Raise IndexError if matrix index out of bounds
Closes #318
1 parent ae61088 commit a963711

2 files changed

Lines changed: 6 additions & 0 deletions

File tree

mpmath/matrices/matrices.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ def __getitem__(self, key):
454454
raise IndexError('Row index out of bounds')
455455
else:
456456
# Single row
457+
if key[0] >= self.__rows:
458+
raise IndexError('Row index out of bounds')
457459
rows = [key[0]]
458460

459461
# Columns
@@ -468,6 +470,8 @@ def __getitem__(self, key):
468470

469471
else:
470472
# Single column
473+
if key[1] >= self.__cols:
474+
raise IndexError('Column index out of bounds')
471475
columns = [key[1]]
472476

473477
# Create matrix slice

mpmath/tests/test_matrices.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def test_matrix_basic():
4949
assert A9 != A10
5050
assert nstr(A9)
5151
assert A9 != None # issue 283
52+
pytest.raises(IndexError, lambda: zeros(1,1)[:, 1]) # issue 318
53+
pytest.raises(IndexError, lambda: zeros(1,1)[1, :])
5254

5355
def test_matmul():
5456
"""

0 commit comments

Comments
 (0)