Skip to content

Disable implicit conversion from float to array index #1363

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

Merged
merged 1 commit into from
Jan 21, 2021
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
23 changes: 23 additions & 0 deletions src/runtime/arrayobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,

if (rank == 1)
{
if (!Runtime.PyInt_Check(idx))
{
return RaiseIndexMustBeIntegerError(idx);
}
index = Runtime.PyInt_AsLong(idx);

if (Exceptions.ErrorOccurred())
Expand Down Expand Up @@ -199,6 +203,10 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
for (var i = 0; i < count; i++)
{
IntPtr op = Runtime.PyTuple_GetItem(idx, i);
if (!Runtime.PyInt_Check(op))
{
return RaiseIndexMustBeIntegerError(op);
}
index = Runtime.PyInt_AsLong(op);

if (Exceptions.ErrorOccurred())
Expand Down Expand Up @@ -253,6 +261,11 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,

if (rank == 1)
{
if (!Runtime.PyInt_Check(idx))
{
RaiseIndexMustBeIntegerError(idx);
return -1;
}
index = Runtime.PyInt_AsLong(idx);

if (Exceptions.ErrorOccurred())
Expand Down Expand Up @@ -291,6 +304,11 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
for (var i = 0; i < count; i++)
{
IntPtr op = Runtime.PyTuple_GetItem(idx, i);
if (!Runtime.PyInt_Check(op))
{
RaiseIndexMustBeIntegerError(op);
return -1;
}
index = Runtime.PyInt_AsLong(op);

if (Exceptions.ErrorOccurred())
Expand Down Expand Up @@ -320,6 +338,11 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
return 0;
}

private static IntPtr RaiseIndexMustBeIntegerError(IntPtr idx)
{
string tpName = Runtime.PyObject_GetTypeName(idx);
return Exceptions.RaiseTypeError($"array index has type {tpName}, expected an integer");
}

/// <summary>
/// Implements __contains__ for array types.
Expand Down
35 changes: 33 additions & 2 deletions src/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,15 @@ def test_typed_array():

with pytest.raises(TypeError):
ob = Test.TypedArrayTest()
ob.items["wrong"] = "wrong"
ob.items["wrong"] = Spam("0")

with pytest.raises(TypeError):
ob = Test.TypedArrayTest()
_ = ob.items[0.5]

with pytest.raises(TypeError):
ob = Test.TypedArrayTest()
ob.items[0.5] = Spam("0")

def test_multi_dimensional_array():
"""Test multi-dimensional arrays."""
Expand Down Expand Up @@ -901,8 +908,23 @@ def test_multi_dimensional_array():

with pytest.raises(TypeError):
ob = Test.MultiDimensionalArrayTest()
ob[0, 0] = "wrong"
ob.items[0, 0] = "wrong"

with pytest.raises(TypeError):
ob = Test.MultiDimensionalArrayTest()
ob["0", 0] = 0

with pytest.raises(TypeError):
ob = Test.MultiDimensionalArrayTest()
ob.items["0", 0] = 0

with pytest.raises(TypeError):
ob = Test.MultiDimensionalArrayTest()
_ = ob.items[0.5, 0]

with pytest.raises(TypeError):
ob = Test.MultiDimensionalArrayTest()
ob.items[0.5, 0] = 0

def test_array_iteration():
"""Test array iteration."""
Expand Down Expand Up @@ -1189,6 +1211,15 @@ def test_create_array_from_shape():
with pytest.raises(ValueError):
Array[int](-1)

with pytest.raises(TypeError):
Array[int]('1')

with pytest.raises(ValueError):
Array[int](-1, -1)

with pytest.raises(TypeError):
Array[int]('1', '1')

def test_special_array_creation():
"""Test using the Array[<type>] syntax for creating arrays."""
from Python.Test import ISayHello1, InterfaceTest, ShortEnum
Expand Down