Skip to content

Commit

Permalink
Correct error raised when slicing CLI arrays (#1827)
Browse files Browse the repository at this point in the history
  • Loading branch information
BCSharp authored Dec 4, 2024
1 parent 476ebae commit 26c2dca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Src/IronPython/Runtime/Operations/ArrayOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,11 @@ private static Array GetSlice(Array data, Slice slice) {
private static int[] TupleToIndices(Array a, IList<object?> tuple) {
int[] indices = new int[tuple.Count];
for (int i = 0; i < indices.Length; i++) {
int iindex = Converter.ConvertToInt32(tuple[i]);
object? oindex = tuple[i];
if (a.Rank != 1 && oindex is Slice) {
throw PythonOps.NotImplementedError("slice on multi-dimensional array");
}
int iindex = Converter.ConvertToInt32(oindex);
indices[i] = i < a.Rank ? FixIndex(a, iindex, i) : int.MinValue;
}
return indices;
Expand Down
12 changes: 7 additions & 5 deletions Tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,14 @@ def test_slice(self):
def f(): array1[::2] = [x * 2 for x in range(11)]
self.assertRaises(ValueError, f)

# slices on non-1-dim arrays are not supported
# slices on non-1D arrays are not supported yet
array2 = System.Array.CreateInstance(int, 20, 20)
self.assertRaises(NotImplementedError, lambda: array2[:]) # TODO: TypeError?
self.assertRaises(TypeError, lambda: array2[:, :]) # TODO: NotImplementedError? This would work in Numpy and Sympy
self.assertRaises(TypeError, lambda: array2[:, :, :]) # OK

# TODO: memoryview can slice ND views with a single slice
self.assertRaises(NotImplementedError, lambda: array2[:])
# TODO: Numpy and Sympy can slice ND arrays with exactly N slices
self.assertRaises(NotImplementedError, lambda: array2[:, :])
# TODO: Error matches memoryview; if slicing of ND arrays were supported, TypeError here would be expected
self.assertRaises(NotImplementedError, lambda: array2[:, :, :])

def test_creation(self):
t = System.Array
Expand Down

0 comments on commit 26c2dca

Please sign in to comment.