Skip to content

Commit 26c2dca

Browse files
authored
Correct error raised when slicing CLI arrays (#1827)
1 parent 476ebae commit 26c2dca

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

Src/IronPython/Runtime/Operations/ArrayOps.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,11 @@ private static Array GetSlice(Array data, Slice slice) {
429429
private static int[] TupleToIndices(Array a, IList<object?> tuple) {
430430
int[] indices = new int[tuple.Count];
431431
for (int i = 0; i < indices.Length; i++) {
432-
int iindex = Converter.ConvertToInt32(tuple[i]);
432+
object? oindex = tuple[i];
433+
if (a.Rank != 1 && oindex is Slice) {
434+
throw PythonOps.NotImplementedError("slice on multi-dimensional array");
435+
}
436+
int iindex = Converter.ConvertToInt32(oindex);
433437
indices[i] = i < a.Rank ? FixIndex(a, iindex, i) : int.MinValue;
434438
}
435439
return indices;

Tests/test_array.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,14 @@ def test_slice(self):
166166
def f(): array1[::2] = [x * 2 for x in range(11)]
167167
self.assertRaises(ValueError, f)
168168

169-
# slices on non-1-dim arrays are not supported
169+
# slices on non-1D arrays are not supported yet
170170
array2 = System.Array.CreateInstance(int, 20, 20)
171-
self.assertRaises(NotImplementedError, lambda: array2[:]) # TODO: TypeError?
172-
self.assertRaises(TypeError, lambda: array2[:, :]) # TODO: NotImplementedError? This would work in Numpy and Sympy
173-
self.assertRaises(TypeError, lambda: array2[:, :, :]) # OK
174-
171+
# TODO: memoryview can slice ND views with a single slice
172+
self.assertRaises(NotImplementedError, lambda: array2[:])
173+
# TODO: Numpy and Sympy can slice ND arrays with exactly N slices
174+
self.assertRaises(NotImplementedError, lambda: array2[:, :])
175+
# TODO: Error matches memoryview; if slicing of ND arrays were supported, TypeError here would be expected
176+
self.assertRaises(NotImplementedError, lambda: array2[:, :, :])
175177

176178
def test_creation(self):
177179
t = System.Array

0 commit comments

Comments
 (0)