Skip to content

Commit

Permalink
Display array base when nonzero (#1819)
Browse files Browse the repository at this point in the history
  • Loading branch information
BCSharp authored Nov 28, 2024
1 parent b48e24b commit aefaecb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions Src/IronPython/Runtime/Operations/ArrayOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,24 +214,29 @@ public static string __repr__(CodeContext/*!*/ context, [NotNone] Array/*!*/ sel
ret.Append("Array[");
Type elemType = self.GetType().GetElementType()!;
ret.Append(DynamicHelpers.GetPythonTypeFromType(elemType).Name);
ret.Append("]");
ret.Append(']');
ret.Append("((");
for (int i = 0; i < self.Length; i++) {
if (i > 0) ret.Append(", ");
ret.Append(PythonOps.Repr(context, self.GetValue(i + self.GetLowerBound(0))));
}
ret.Append("))");
ret.Append(')');
if (self.GetLowerBound(0) != 0) {
ret.Append(", base: ");
ret.Append(self.GetLowerBound(0));
}
ret.Append(')');
} else {
// multi dimensional arrays require multiple statements to construct so we just
// give enough info to identify the object and its type.
ret.Append("<");
ret.Append('<');
ret.Append(self.Rank);
ret.Append(" dimensional Array[");
Type elemType = self.GetType().GetElementType()!;
ret.Append(DynamicHelpers.GetPythonTypeFromType(elemType).Name);
ret.Append("] at ");
ret.Append(PythonOps.HexId(self));
ret.Append(">");
ret.Append('>');
}
return ret.ToString();
} finally {
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_nonzero_lowerbound(self):
self.assertEqual(a[2:4], System.Array[int]((2,3)))
self.assertEqual(a[-1], 4)

self.assertEqual(repr(a), 'Array[int]((0, 1, 2, 3, 4))')
self.assertEqual(repr(a), 'Array[int]((0, 1, 2, 3, 4), base: 5)')

a = System.Array.CreateInstance(int, (5,), (15,))
b = System.Array.CreateInstance(int, (5,), (20,))
Expand Down

0 comments on commit aefaecb

Please sign in to comment.