Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 86d6bef

Browse files
committed
Add __repr__ implement for delegate objects
1 parent da41e0d commit 86d6bef

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

src/runtime/methodbinding.cs

-9
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,4 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
356356
return self._caller.PyCall(self._target, args);
357357
}
358358
}
359-
360-
internal class DelegateGenericBinding : ExtensionType
361-
{
362-
363-
public DelegateGenericBinding()
364-
{
365-
366-
}
367-
}
368359
}

src/runtime/methodobject.cs

+18-4
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,13 @@ public static void ClearFreeList()
288288

289289
class DelegateCallableObject
290290
{
291-
Dictionary<int, List<Method.IMethodCaller>> _callers;
292-
private string _name;
291+
public string Name { get; private set; }
292+
293+
private Dictionary<int, List<Method.IMethodCaller>> _callers;
293294

294295
public DelegateCallableObject(string name)
295296
{
296-
_name = name;
297+
Name = name;
297298
_callers = new Dictionary<int, List<Method.IMethodCaller>>();
298299
}
299300

@@ -398,7 +399,6 @@ public IntPtr PyCall(IntPtr self, IntPtr args)
398399
}
399400
}
400401
return Exceptions.RaiseTypeError("No match found for given type params");
401-
//return IntPtr.Zero;
402402
}
403403

404404
internal static Type CreateDelegateType(Type type, Type returnType, Type[] paramTypes)
@@ -551,6 +551,12 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
551551
var self = (DelegateMethodObject)GetManagedObject(ob);
552552
return self._caller.PyCall(ob, args);
553553
}
554+
555+
public static IntPtr tp_repr(IntPtr ob)
556+
{
557+
var self = (DelegateMethodObject)GetManagedObject(ob);
558+
return Runtime.PyString_FromString($"<method '{self._caller.Name}'>");
559+
}
554560
}
555561

556562
internal class DelegateBoundMethodObject : ExtensionType
@@ -598,6 +604,14 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
598604
}
599605
FinalizeObject(self);
600606
}
607+
608+
public static IntPtr tp_repr(IntPtr ob)
609+
{
610+
var self = (DelegateBoundMethodObject)GetManagedObject(ob);
611+
string type = self.Target == IntPtr.Zero ? "unbound" : "bound";
612+
string name = self.Caller.Name;
613+
return Runtime.PyString_FromString($"<{type} method '{name}'>");
614+
}
601615
}
602616

603617
internal class DelegateGenericMethodObject : DelegateMethodObject

src/runtime/propertyobject.cs

+8
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,12 @@ internal interface IPropertyObject
171171
internal class DelegatePropertyObject : ExtensionType
172172
{
173173
private IPropertyObject _propCaller;
174+
private string _name;
174175

175176
public DelegatePropertyObject(PropertyInfo md)
176177
{
177178
_propCaller = CreateProp(md);
179+
_name = md.Name;
178180
}
179181

180182
private static IPropertyObject CreateProp(PropertyInfo pi)
@@ -214,6 +216,12 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
214216
var self = (DelegatePropertyObject)GetManagedObject(ds);
215217
return self._propCaller.OnDescrSet(ds, ob, val);
216218
}
219+
220+
public static IntPtr tp_repr(IntPtr ob)
221+
{
222+
var self = (DelegatePropertyObject)GetManagedObject(ob);
223+
return Runtime.PyString_FromString($"<property '{self._name}'>");
224+
}
217225
}
218226

219227
internal class StaticPropertyObject<T> : IPropertyObject

0 commit comments

Comments
 (0)