Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionssrc/runtime/arrayobject.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 deletionssrc/tests/test_array.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp