FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Disable implicit conversion from float to array index by tminka · Pull Request #1363 · pythonnet/pythonnet · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (1) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
23 changes: 23 additions & 0 deletions src/runtime/arrayobject.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL