8000 Better error message · pythonnet/pythonnet@fbd5846 · GitHub
[go: up one dir, main page]

Skip to content

Commit fbd5846

Browse files
committed
Better error message
1 parent 133927b commit fbd5846

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/runtime/arrayobject.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
161161
{
162162
if (!Runtime.PyInt_Check(idx))
163163
{
164-
return Exceptions.RaiseTypeError("array index is not an integer");
164+
return RaiseTypeError(idx);
165165
}
166166
index = Runtime.PyInt_AsLong(idx);
167167

@@ -205,7 +205,7 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
205205
IntPtr op = Runtime.PyTuple_GetItem(idx, i);
206206
if (!Runtime.PyInt_Check(op))
207207
{
208-
return Exceptions.RaiseTypeError($"array index {i} is not an integer");
208+
return RaiseTypeError(op);
209209
}
210210
index = Runtime.PyInt_AsLong(op);
211211

@@ -263,7 +263,7 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
263263
{
264264
if (!Runtime.PyInt_Check(idx))
265265
{
266-
Exceptions.RaiseTypeError("array index is not an integer");
266+
RaiseTypeError(idx);
267267
return -1;
268268
}
269269
index = Runtime.PyInt_AsLong(idx);
@@ -306,7 +306,7 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
306306
IntPtr op = Runtime.PyTuple_GetItem(idx, i);
307307
if (!Runtime.PyInt_Check(op))
308308
{
309-
Exceptions.RaiseTypeError($"array index {i} is not an integer");
309+
RaiseTypeError(op);
310310
return -1;
311311
}
312312
index = Runtime.PyInt_AsLong(op);
@@ -338,6 +338,11 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
338338
return 0;
339339
}
340340

341+
private static IntPtr RaiseTypeError(IntPtr idx)
342+
{
343+
string tpName = Runtime.PyObject_GetTypeName(idx);
344+
return Exceptions.RaiseTypeError($"array index has type {tpName}, expected an integer");
345+
}
341346

342347
/// <summary>
343348
/// Implements __contains__ for array types.

0 commit comments

Comments
 (0)
0