Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 25 additions & 1 deletion src/runtime/Types/ClassBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,31 @@ public static NewReference tp_repr(BorrowedReference ob)
using var args = Runtime.PyTuple_New(1);
Runtime.PyTuple_SetItem(args.Borrow(), 0, ob);
using var reprFunc = Runtime.PyObject_GetAttr(Runtime.PyBaseObjectType, PyIdentifier.__repr__);
return Runtime.PyObject_Call(reprFunc.Borrow(), args.Borrow(), null);
var defaultRepr = Runtime.PyObject_Call(reprFunc.Borrow(), args.Borrow(), null);
if (defaultRepr.IsNull() || instType.GetMethod("ToString", Type.EmptyTypes)?.DeclaringType == typeof(object))
{
return defaultRepr;
}

// like IronPython, show an overridden ToString() so containers of .NET objects print readably:
// <QuantConnect.Symbol object at 0x7f9b74b40608 [SPY]>
string text;
try
{
text = co.inst.ToString();
}
catch (Exception)
{
// repr() used to succeed for types whose ToString() throws, keep it that way
return defaultRepr;
}
if (text is null)
{
return defaultRepr;
}
var defaultReprString = Runtime.GetManagedString(defaultRepr.Borrow());
defaultRepr.Dispose();
return Runtime.PyString_FromString($"{defaultReprString.Substring(0, defaultReprString.Length - 1)} [{text}]>");
}
catch (Exception e)
{
Expand Down
16 changes: 16 additions & 0 deletions src/testing/ReprTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ public class Baz : BazMiddle

}

public class NullToString
{
public override string ToString()
{
return null;
}
}

public class ThrowingToString
{
public override string ToString()
{
throw new InvalidOperationException("ToString() failed");
}
}

public class Quux
{
public string ToString(string format)
Expand Down
28 changes: 28 additions & 0 deletions tests/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,38 @@ def test_system_string():
ob = System.String("hello")
assert str(ob) == "hello"
assert "<System.String object at " in ob.__repr__()
assert ob.__repr__().endswith(" [hello]>")

def test_str_only():
"""Test class implementing ToString() but not __repr__()"""
ob = ReprTest.Bar()
assert str(ob) == "I implement ToString() but not __repr__()!"
assert "<Python.Test.Bar object at " in ob.__repr__()
assert ob.__repr__().endswith(" [I implement ToString() but not __repr__()!]>")

def test_str_only_nested_in_list():
"""Test containers show the ToString() of elements implementing ToString() but not __repr__()"""
obs = [ReprTest.Bar(), ReprTest.Bar()]
assert str(obs).count("[I implement ToString() but not __repr__()!]>") == 2

def test_no_tostring_override():
"""Test object not overriding ToString() keeps the default repr"""
ob = System.Object()
assert repr(ob).startswith("<System.Object object at ")
assert repr(ob).endswith(">")
assert "[" not in repr(ob)

def test_tostring_returns_null():
"""Test class whose ToString() returns null"""
ob = ReprTest.NullToString()
assert "<Python.Test.NullToString object at " in repr(ob)
assert "[" not in repr(ob)

def test_tostring_throws():
"""Test class whose ToString() throws"""
ob = ReprTest.ThrowingToString()
assert "<Python.Test.ThrowingToString object at " in repr(ob)
assert "[" not in repr(ob)

def test_hierarchy1():
"""Test inheritance hierarchy with base & middle class implementing ToString"""
Expand All @@ -34,10 +60,12 @@ def test_hierarchy1():
ob2 = ReprTest.BazMiddle()
assert str(ob2) == "Middle class implementing ToString()!"
assert "<Python.Test.BazMiddle object at " in ob2.__repr__()
assert ob2.__repr__().endswith(" [Middle class implementing ToString()!]>")

ob3 = ReprTest.Baz()
assert str(ob3) == "Middle class implementing ToString()!"
assert "<Python.Test.Baz object at " in ob3.__repr__()
assert ob3.__repr__().endswith(" [Middle class implementing ToString()!]>")

def bad_tostring():
"""Test ToString that can't be used by str()"""
Expand Down
Loading