Skip to content

Commit 0916406

Browse files
committed
update based on review
1 parent c6ae6a4 commit 0916406

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

src/runtime/classbase.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -242,25 +242,18 @@ public static IntPtr tp_str(IntPtr ob)
242242

243243
//First check which type in the object hierarchy provides ToString()
244244
//ToString has two "official" overloads so loop over GetMethods to get the one without parameters
245-
var instType = co.inst.GetType();
246-
foreach (var method in instType.GetMethods())
247-
{
248-
249-
//TODO this could probably be done more cleanly with Linq
250-
if (!method.IsPublic) continue; //skip private/protected methods
251-
if (method.Name != "ToString") continue; //only look for ToString
252-
if (method.DeclaringType == typeof(object)) continue; //ignore default from object
253-
if (method.GetParameters().Length != 0) continue; //ignore Formatter overload of ToString
254-
245+
var method = type.GetMethod("ToString", new Type[]{});
246+
if (method.DeclaringTyppe != typeof(object))
247+
{
255248
//match! something other than object provides a parameter-less overload of ToString
256-
return Runtime.PyString_FromString(co.inst.ToString());
257-
}
249+
return Runtime.Pystring_FromString(co.inst.ToString());
250+
}
258251

259252
//If the object defines __repr__, call it.
260253
System.Reflection.MethodInfo reprMethodInfo = instType.GetMethod("__repr__");
261254
if (reprMethodInfo != null && reprMethodInfo.IsPublic)
262255
{
263-
var reprString = reprMethodInfo.Invoke(co.inst, null) as string;
256+
var reprString = (string)reprMethodInfo.Invoke(co.inst, null);
264257
return Runtime.PyString_FromString(reprString);
265258
}
266259

0 commit comments

Comments
 (0)