forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmethodbinding.cs
More file actions
318 lines (282 loc) · 12.1 KB
/
methodbinding.cs
File metadata and controls
318 lines (282 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Python.Runtime
{
/// <summary>
/// Implements a Python binding type for CLR methods. These work much like
/// standard Python method bindings, but the same type is used to bind
/// both static and instance methods.
/// </summary>
internal class MethodBinding : ExtensionType
{
internal MethodInfo info;
internal MethodObject m;
internal IntPtr target;
internal IntPtr targetType;
internal BorrowedReference Target => new BorrowedReference(this.target);
internal BorrowedReference TargetType => new BorrowedReference(this.targetType);
public MethodBinding(MethodObject m, IntPtr target, IntPtr targetType)
{
if (target != IntPtr.Zero) Runtime.XIncref(target);
this.target = target;
Runtime.XIncref(targetType);
if (targetType == IntPtr.Zero)
{
targetType = Runtime.PyObject_Type(target);
}
this.targetType = targetType;
this.info = null;
this.m = m;
}
public MethodBinding(MethodObject m, IntPtr target) : this(m, target, IntPtr.Zero)
{
}
/// <summary>
/// Implement binding of generic methods using the subscript syntax [].
/// </summary>
public static IntPtr mp_subscript(IntPtr tp, IntPtr idx)
{
var self = GetInstance(tp);
Type[] types = Runtime.PythonArgsToTypeArray(idx);
if (types == null)
{
return Exceptions.RaiseTypeError("type(s) expected");
}
MethodInfo mi = MethodBinder.MatchParameters(self.m.info, types);
if (mi == null)
{
return Exceptions.RaiseTypeError("No match found for given type params");
}
var mb = new MethodBinding(self.m, self.target) { info = mi };
return mb.pyHandle;
}
PyObject Singature {
get {
var infos = this.info == null ? this.m.info : new[] {this.info};
var type = infos.Select(i => i.DeclaringType)
.OrderByDescending(t => t, new TypeSpecificityComparer())
.First();
infos = infos.Where(info => info.DeclaringType == type).ToArray();
// this is a primitive version
// the overload with the maximum number of parameters should be used
var primary = infos.OrderByDescending(i => i.GetParameters().Length).First();
var primaryParameters = primary.GetParameters();
PyObject signatureClass = Runtime.InspectModule.GetAttr("Signature");
var primaryReturn = primary.ReturnParameter;
var parameters = new PyList();
var parameterClass = primaryParameters.Length > 0 ? Runtime.InspectModule.GetAttr("Parameter") : null;
var positionalOrKeyword = parameterClass?.GetAttr("POSITIONAL_OR_KEYWORD");
for (int i = 0; i < primaryParameters.Length; i++) {
var parameter = primaryParameters[i];
var alternatives = infos.Select(info => {
ParameterInfo[] altParamters = info.GetParameters();
return i < altParamters.Length ? altParamters[i] : null;
}).Where(p => p != null);
var defaultValue = alternatives
.Select(alternative => alternative.DefaultValue != DBNull.Value ? alternative.DefaultValue.ToPython() : null)
.FirstOrDefault(v => v != null) ?? parameterClass.GetAttr("empty");
if (alternatives.Any(alternative => alternative.Name != parameter.Name)) {
return signatureClass.Invoke();
}
var args = new PyTuple(new []{ parameter.Name.ToPython(), positionalOrKeyword});
var kw = new PyDict();
if (defaultValue != null) {
kw["default"] = defaultValue;
}
var parameterInfo = parameterClass.Invoke(args: args, kw: kw);
parameters.Append(parameterInfo);
}
// TODO: add return annotation
return signatureClass.Invoke(parameters);
}
}
struct TypeSpecificityComparer : IComparer<Type> {
public int Compare(Type a, Type b) {
if (a == b) return 0;
if (a.IsSubclassOf(b)) return 1;
if (b.IsSubclassOf(a)) return -1;
throw new NotSupportedException();
}
}
static MethodBinding GetInstance(IntPtr ob)
=> GetInstance(new BorrowedReference(ob));
static MethodBinding GetInstance(BorrowedReference ob)
=> GetManagedObject<MethodBinding>(ob);
/// <summary>
/// MethodBinding __getattribute__ implementation.
/// </summary>
public static IntPtr tp_getattro(IntPtr ob, IntPtr key)
{
var self = GetInstance(ob);
if (!Runtime.PyString_Check(key))
{
Exceptions.SetError(Exceptions.TypeError, "string expected");
return IntPtr.Zero;
}
string name = Runtime.GetManagedString(key);
switch (name)
{
case "__doc__":
IntPtr doc = self.m.GetDocString();
Runtime.XIncref(doc);
return doc;
// FIXME: deprecate __overloads__ soon...
case "__overloads__":
case "Overloads":
var om = new OverloadMapper(self.m, self.target);
return om.pyHandle;
case "__signature__":
var sig = self.Singature;
if (sig is null)
{
return Runtime.PyObject_GenericGetAttr(ob, key);
}
return sig.Reference.DangerousIncRefOrNull();
case "__name__":
var pyName = self.m.GetName();
return pyName == IntPtr.Zero
? IntPtr.Zero
: Runtime.SelfIncRef(pyName);
default:
return Runtime.PyObject_GenericGetAttr(ob, key);
}
}
/// <summary>
/// MethodBinding __call__ implementation.
/// </summary>
public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
{
var self = GetInstance(ob);
// This works around a situation where the wrong generic method is picked,
// for example this method in the tests: string Overloaded<T>(int arg1, int arg2, string arg3)
if (self.info != null)
{
if (self.info.IsGenericMethod)
{
var len = Runtime.PyTuple_Size(args); //FIXME: Never used
Type[] sigTp = Runtime.PythonArgsToTypeArray(args, true);
if (sigTp != null)
{
Type[] genericTp = self.info.GetGenericArguments();
MethodInfo betterMatch = MethodBinder.MatchSignatureAndParameters(self.m.info, genericTp, sigTp);
if (betterMatch != null)
{
self.info = betterMatch;
}
}
}
}
// This supports calling a method 'unbound', passing the instance
// as the first argument. Note that this is not supported if any
// of the overloads are static since we can't know if the intent
// was to call the static method or the unbound instance method.
var disposeList = new List<IntPtr>();
try
{
var target = self.target;
if (target == IntPtr.Zero && !self.m.IsStatic())
{
var len = Runtime.PyTuple_Size(args);
if (len < 1)
{
Exceptions.SetError(Exceptions.TypeError, "not enough arguments");
return IntPtr.Zero;
}
target = Runtime.PyTuple_GetItem(args, 0);
Runtime.XIncref(target);
disposeList.Add(target);
args = Runtime.PyTuple_GetSlice(args, 1, len);
disposeList.Add(args);
}
// if the class is a IPythonDerivedClass and target is not the same as self.targetType
// (eg if calling the base class method) then call the original base class method instead
// of the target method.
IntPtr superType = IntPtr.Zero;
if (Runtime.PyObject_TYPE(target) != self.targetType)
{
var inst = ManagedType.GetManagedObject(target) as CLRObject;
if (inst?.inst is IPythonDerivedType)
{
var baseType = ManagedType.GetManagedObject(self.TargetType) as ClassBase;
if (baseType != null)
{
string baseMethodName = "_" + baseType.type.Name + "__" + self.m.name;
using var baseMethod = Runtime.PyObject_GetAttrString(self.Target, baseMethodName);
if (!baseMethod.IsNull())
{
BorrowedReference baseMethodType = Runtime.PyObject_TYPE(baseMethod);
BorrowedReference methodBindingTypeHandle = TypeManager.GetTypeHandle(typeof(MethodBinding));
if (baseMethodType == methodBindingTypeHandle)
{
self = GetInstance(baseMethod);
}
}
else
{
Runtime.PyErr_Clear();
}
}
}
}
return self.m.Invoke(target, args, kw, self.info);
}
finally
{
foreach (IntPtr ptr in disposeList)
{
Runtime.XDecref(ptr);
}
}
}
/// <summary>
/// MethodBinding __hash__ implementation.
/// </summary>
public static IntPtr tp_hash(IntPtr ob)
{
var self = GetInstance(ob);
long x = 0;
long y = 0;
if (self.target != IntPtr.Zero)
{
x = Runtime.PyObject_Hash(self.target).ToInt64();
if (x == -1)
{
return new IntPtr(-1);
}
}
y = Runtime.PyObject_Hash(self.m.pyHandle).ToInt64();
if (y == -1)
{
return new IntPtr(-1);
}
x ^= y;
if (x == -1)
{
x = -1;
}
return new IntPtr(x);
}
/// <summary>
/// MethodBinding __repr__ implementation.
/// </summary>
public static IntPtr tp_repr(IntPtr ob)
{
var self = GetInstance(ob);
string type = self.target == IntPtr.Zero ? "unbound" : "bound";
string name = self.m.name;
return Runtime.PyString_FromString($"<{type} method '{name}'>");
}
/// <summary>
/// MethodBinding dealloc implementation.
/// </summary>
public new static void tp_dealloc(IntPtr ob)
{
var self = GetInstance(ob);
Runtime.XDecref(self.target);
Runtime.XDecref(self.targetType);
FinalizeObject(self);
}
}
}