Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct error raised when slicing CLI arrays #1827

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading