Skip to content

Commit 4a53453

Browse files
committed
Use actual container type name for repr
1 parent ce434b5 commit 4a53453

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

src/core/IronPython.Modules/_collections.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,8 @@ private bool WalkDeque(DequeWalker walker) {
920920
infinite.Add(this);
921921
try {
922922
StringBuilder sb = new StringBuilder();
923-
sb.Append("deque([");
923+
sb.Append(PythonOps.GetPythonTypeName(this));
924+
sb.Append("([");
924925
string comma = "";
925926

926927
lock (_lockObj) {
@@ -1188,7 +1189,7 @@ public override PythonDictionary copy(CodeContext/*!*/ context) {
11881189
}
11891190

11901191
public override string __repr__(CodeContext context) {
1191-
return string.Format("defaultdict({0}, {1})", ReprFactory(context, default_factory), base.__repr__(context));
1192+
return string.Format("{0}({1}, {2})", PythonOps.GetPythonTypeName(this), ReprFactory(context, default_factory), base.__repr__(context));
11921193

11931194
static string ReprFactory(CodeContext context, object factory) {
11941195
var infinite = PythonOps.GetAndCheckInfinite(factory);

src/core/IronPython.Modules/array.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) {
856856
#region ICodeFormattable Members
857857

858858
public virtual string/*!*/ __repr__(CodeContext/*!*/ context) {
859-
string res = "array('" + typecode.ToString() + "'";
859+
string res = PythonOps.GetPythonTypeName(this) + "('" + typecode.ToString() + "'";
860860
if (_data.Count == 0) {
861861
return res + ")";
862862
}

src/core/IronPython.StdLib/lib/test/test_defaultdict.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,14 @@ def __init__(self):
157157
def _factory(self):
158158
return []
159159
d = sub()
160-
self.assertTrue(repr(d).startswith(
161-
"defaultdict(<bound method sub._factory of defaultdict(..."))
160+
# IronPython: repr class name change adopted from Python 3.7
161+
# original Python 3.4 test:
162+
# self.assertTrue(repr(d).startswith(
163+
# "defaultdict(<bound method sub._factory of defaultdict(..."))
164+
# Python 3.7 test:
165+
self.assertRegex(repr(d),
166+
r"sub\(<bound method .*sub\._factory "
167+
r"of sub\(\.\.\., \{\}\)>, \{\}\)")
162168

163169
# NOTE: printing a subclass of a builtin type does not call its
164170
# tp_print slot. So this part is essentially the same test as above.

src/core/IronPython/Runtime/ByteArray.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ public PythonTuple __reduce__(CodeContext context) {
10791079

10801080
private string Repr() {
10811081
lock (this) {
1082-
return "bytearray(" + _bytes.BytesRepr() + ")";
1082+
return PythonOps.GetPythonTypeName(this) + "(" + _bytes.BytesRepr() + ")";
10831083
}
10841084
}
10851085

tests/suite/test_isinstance.py

+22
Original file line numberDiff line numberDiff line change
@@ -974,4 +974,26 @@ def test_tuple_new(self):
974974
self.assertRaises(TypeError, tuple.__new__, str)
975975
self.assertRaises(TypeError, tuple.__new__, str, 'abc')
976976

977+
978+
def test_container_repr(self):
979+
import array, collections
980+
class MyArray(array.array): pass
981+
class MyBytearray(bytearray): pass
982+
class MyDeque(collections.deque): pass
983+
class MyDefaultDict(collections.defaultdict): pass
984+
class MySet(set): pass
985+
986+
if is_cli or sys.version_info >= (3, 7):
987+
self.assertEqual(repr(MyArray('b')), "MyArray('b')")
988+
self.assertEqual(repr(MyBytearray(b'x')), "MyBytearray(b'x')")
989+
self.assertEqual(repr(MyDeque(['c'])), "MyDeque(['c'])")
990+
self.assertTrue("MyDefaultDict" in repr(MyDefaultDict(list)))
991+
else:
992+
self.assertEqual(repr(MyArray('b')), "array('b')")
993+
self.assertEqual(repr(MyBytearray(b'x')), "bytearray(b'x')")
994+
self.assertEqual(repr(MyDeque(['c'])), "deque(['c'])")
995+
self.assertTrue("defaultdict" in repr(MyDefaultDict(list)))
996+
self.assertEqual(repr(MySet()), "MySet()")
997+
998+
977999
run_test(__name__)

0 commit comments

Comments
 (0)