Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/core/IronPython.Modules/_collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,8 @@ private bool WalkDeque(DequeWalker walker) {
infinite.Add(this);
try {
StringBuilder sb = new StringBuilder();
sb.Append("deque([");
sb.Append(PythonOps.GetPythonTypeName(this));
sb.Append("([");
string comma = "";

lock (_lockObj) {
Expand Down Expand Up @@ -1188,7 +1189,7 @@ public override PythonDictionary copy(CodeContext/*!*/ context) {
}

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

static string ReprFactory(CodeContext context, object factory) {
var infinite = PythonOps.GetAndCheckInfinite(factory);
Expand Down
2 changes: 1 addition & 1 deletion src/core/IronPython.Modules/array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) {
#region ICodeFormattable Members

public virtual string/*!*/ __repr__(CodeContext/*!*/ context) {
string res = "array('" + typecode.ToString() + "'";
string res = PythonOps.GetPythonTypeName(this) + "('" + typecode.ToString() + "'";
if (_data.Count == 0) {
return res + ")";
}
Expand Down
10 changes: 8 additions & 2 deletions src/core/IronPython.StdLib/lib/test/test_defaultdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,14 @@ def __init__(self):
def _factory(self):
return []
d = sub()
self.assertTrue(repr(d).startswith(
"defaultdict(<bound method sub._factory of defaultdict(..."))
# IronPython: repr class name change adopted from Python 3.7
# original Python 3.4 test:
# self.assertTrue(repr(d).startswith(
# "defaultdict(<bound method sub._factory of defaultdict(..."))
# Python 3.7 test:
self.assertRegex(repr(d),
r"sub\(<bound method .*sub\._factory "
r"of sub\(\.\.\., \{\}\)>, \{\}\)")

# NOTE: printing a subclass of a builtin type does not call its
# tp_print slot. So this part is essentially the same test as above.
Expand Down
2 changes: 1 addition & 1 deletion src/core/IronPython/Runtime/ByteArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ public PythonTuple __reduce__(CodeContext context) {

private string Repr() {
lock (this) {
return "bytearray(" + _bytes.BytesRepr() + ")";
return PythonOps.GetPythonTypeName(this) + "(" + _bytes.BytesRepr() + ")";
}
}

Expand Down
22 changes: 22 additions & 0 deletions tests/suite/test_isinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,4 +974,26 @@ def test_tuple_new(self):
self.assertRaises(TypeError, tuple.__new__, str)
self.assertRaises(TypeError, tuple.__new__, str, 'abc')


def test_container_repr(self):
import array, collections
class MyArray(array.array): pass
class MyBytearray(bytearray): pass
class MyDeque(collections.deque): pass
class MyDefaultDict(collections.defaultdict): pass
class MySet(set): pass

if is_cli or sys.version_info >= (3, 7):
self.assertEqual(repr(MyArray('b')), "MyArray('b')")
self.assertEqual(repr(MyBytearray(b'x')), "MyBytearray(b'x')")
self.assertEqual(repr(MyDeque(['c'])), "MyDeque(['c'])")
self.assertTrue("MyDefaultDict" in repr(MyDefaultDict(list)))
else:
self.assertEqual(repr(MyArray('b')), "array('b')")
self.assertEqual(repr(MyBytearray(b'x')), "bytearray(b'x')")
self.assertEqual(repr(MyDeque(['c'])), "deque(['c'])")
self.assertTrue("defaultdict" in repr(MyDefaultDict(list)))
self.assertEqual(repr(MySet()), "MySet()")


run_test(__name__)
Loading