@@ -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 RaiseIndexMustBeIntegerError ( idx ) ;
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 RaiseIndexMustBeIntegerError ( op ) ;
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+ RaiseIndexMustBeIntegerError ( idx ) ;
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+ RaiseIndexMustBeIntegerError ( op ) ;
310+ return - 1 ;
311+ }
294312 index = Runtime . PyInt_AsLong ( op ) ;
295313
296314 if ( Exceptions . ErrorOccurred ( ) )
@@ -320,6 +338,11 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
320338 return 0 ;
321339 }
322340
341+ private static IntPtr RaiseIndexMustBeIntegerError ( IntPtr idx )
342+ {
343+ string tpName = Runtime . PyObject_GetTypeName ( idx ) ;
344+ return Exceptions . RaiseTypeError ( $ "array index has type { tpName } , expected an integer") ;
345+ }
323346
324347 /// <summary>
325348 /// Implements __contains__ for array types.
0 commit comments