Skip to content

Commit

Permalink
Fixed calling repr on non pythonnet types e..g ironpython
Browse files Browse the repository at this point in the history
  • Loading branch information
eirannejad committed May 26, 2024
1 parent 2914f98 commit 19b64fb
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/runtime/Types/ClassBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,24 @@ public static NewReference tp_repr(BorrowedReference ob)
}

var instType = co.inst.GetType();

//if __repr__ is defined, use it
var methodInfo = instType.GetMethod("__repr__");
if (methodInfo != null && methodInfo.IsPublic)
// NOTE:
// Only do this for types from this assembly otherwise the signature
// of __repr__ might be different (e.g. IronPython types) and
// .Invoke will throw a parameter mismatch exception
if (ReferenceEquals(instType.Assembly, typeof(ClassBase).Assembly))
{
if (methodInfo.Invoke(co.inst, null) is string reprString)
//if __repr__ is defined, use it
var methodInfo = instType.GetMethod("__repr__");
if (methodInfo != null && methodInfo.IsPublic)
{
return Runtime.PyString_FromString(reprString);
}
else
{
return new NewReference(Runtime.PyNone);
if (methodInfo.Invoke(co.inst, null) is string reprString)
{
return Runtime.PyString_FromString(reprString);
}
else
{
return new NewReference(Runtime.PyNone);
}
}
}

Expand Down

0 comments on commit 19b64fb

Please sign in to comment.