Skip to content

Commit 65ceb12

Browse files
Disallow float and complex types as indices
Previously these were treated as booleans (any object can be treated as a boolean, since `bool(obj)` is always defined). Replaced raise TypeError with raise IndexError for indices of recognized type.
1 parent e3592bd commit 65ceb12

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

dpctl/tensor/_slicing.pxi

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ cdef bint _is_boolean(object x) except *:
8181
return True
8282
if isinstance(x, bool):
8383
return True
84-
if isinstance(x, int):
84+
if isinstance(x, (int, float, complex)):
8585
return False
8686
if _is_buffer(x):
8787
mbuf = memoryview(x)
@@ -204,7 +204,11 @@ def _basic_slice_meta(ind, shape : tuple, strides : tuple, offset : int):
204204
)
205205
array_count += 1
206206
else:
207-
raise TypeError
207+
raise IndexError(
208+
"Only integers, slices (`:`), ellipsis (`...`), "
209+
"dpctl.tensor.newaxis (`None`) and integer and "
210+
"boolean arrays are valid indices."
211+
)
208212
if ellipses_count > 1:
209213
raise IndexError(
210214
"an index can only have a single ellipsis ('...')")
@@ -283,6 +287,8 @@ def _basic_slice_meta(ind, shape : tuple, strides : tuple, offset : int):
283287
new_shape.extend(shape[k:k_new])
284288
new_strides.extend(strides[k:k_new])
285289
k = k_new
290+
else:
291+
raise IndexError
286292
new_shape.extend(shape[k:])
287293
new_strides.extend(strides[k:])
288294
new_shape_len += len(shape) - k
@@ -291,4 +297,8 @@ def _basic_slice_meta(ind, shape : tuple, strides : tuple, offset : int):
291297
# assert len(new_advanced_ind) == array_count
292298
return (tuple(new_shape), tuple(new_strides), new_offset, tuple(new_advanced_ind), new_advanced_start_pos)
293299
else:
294-
raise TypeError
300+
raise IndexError(
301+
"Only integers, slices (`:`), ellipsis (`...`), "
302+
"dpctl.tensor.newaxis (`None`) and integer and "
303+
"boolean arrays are valid indices."
304+
)

0 commit comments

Comments
 (0)