10000 Disable implicit conversion from PyFloat to array index · pythonnet/pythonnet@133927b · GitHub
[go: up one dir, main page]

Skip to content

Commit 133927b

Browse files
committed
Disable implicit conversion from PyFloat to array index
1 parent 0ee7631 commit 133927b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/runtime/arrayobject.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
159159

160160
if (rank == 1)
161161
{
162+
if (!Runtime.PyInt_Check(idx))
163+
{
164+
return Exceptions.RaiseTypeError("array index is not an integer");
165+
}
162166
index = Runtime.PyInt_AsLong(idx);
163167

164168
if (Exceptions.ErrorOccurred())
@@ -199,6 +203,10 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
199203
for (var i = 0; i < count; i++)
200204
{
201205
IntPtr op = Runtime.PyTuple_GetItem(idx, i);
206+
if (!Runtime.PyInt_Check(op))
207+
{
208+
return Exceptions.RaiseTypeError($"array index {i} is not an integer");
209+
}
202210
index = Runtime.PyInt_AsLong(op);
203211

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

254262
if (rank == 1)
255263
{
264+
if (!Runtime.PyInt_Check(idx))
265+
{
266+
Exceptions.RaiseTypeError("array index is not an integer");
267+
return -1;
268+
}
256269
index = Runtime.PyInt_AsLong(idx);
257270

258271
if (Exceptions.ErrorOccurred())
@@ -291,6 +304,11 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
291304
for (var i = 0; i < count; i++)
292305
{
293306
IntPtr op = Runtime.PyTuple_GetItem(idx, i);
307+
if (!Runtime.PyInt_Check(op))
308+
{
309+
Exceptions.RaiseTypeError($"array index {i} is not an integer");
310+
return -1;
311+
}
294312
index = Runtime.PyInt_AsLong(op);
295313

296314
if (Exceptions.ErrorOccurred())

0 commit comments

Comments
 (0)
0