forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethodbinding.cs
More file actions
193 lines (153 loc) · 7.01 KB
/
methodbinding.cs
File metadata and controls
193 lines (153 loc) · 7.01 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
// ==========================================================================
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
// ==========================================================================
using System;
using System.Reflection;
namespace Python.Runtime {
//========================================================================
// 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.
//========================================================================
internal class MethodBinding : ExtensionType {
internal MethodInfo info;
internal MethodObject m;
internal IntPtr target;
public MethodBinding(MethodObject m, IntPtr target) : base() {
Runtime.Incref(target);
this.target = target;
this.info = null;
this.m = m;
}
//====================================================================
// Implement binding of generic methods using the subscript syntax [].
//====================================================================
public static IntPtr mp_subscript(IntPtr tp, IntPtr idx) {
MethodBinding self = (MethodBinding)GetManagedObject(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) {
string e = "No match found for given type params";
return Exceptions.RaiseTypeError(e);
}
MethodBinding mb = new MethodBinding(self.m, self.target);
mb.info = mi;
Runtime.Incref(mb.pyHandle);
return mb.pyHandle;
}
//====================================================================
// MethodBinding __getattribute__ implementation.
//====================================================================
public static IntPtr tp_getattro(IntPtr ob, IntPtr key) {
MethodBinding self = (MethodBinding)GetManagedObject(ob);
if (!Runtime.PyString_Check(key)) {
Exceptions.SetError(Exceptions.TypeError, "string expected");
return IntPtr.Zero;
}
string name = Runtime.GetManagedString(key);
if (name == "__doc__") {
IntPtr doc = self.m.GetDocString();
Runtime.Incref(doc);
return doc;
}
// XXX deprecate __overloads__ soon...
if (name == "__overloads__" || name == "Overloads") {
OverloadMapper om = new OverloadMapper(self.m, self.target);
Runtime.Incref(om.pyHandle);
return om.pyHandle;
}
return Runtime.PyObject_GenericGetAttr(ob, key);
}
//====================================================================
// MethodBinding __call__ implementation.
//====================================================================
public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw) {
MethodBinding self = (MethodBinding)GetManagedObject(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)
{
int len = Runtime.PyTuple_Size(args);
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.
if ((self.target == IntPtr.Zero) && (!self.m.IsStatic()))
{
int len = Runtime.PyTuple_Size(args);
if (len < 1)
{
Exceptions.SetError(Exceptions.TypeError, "not enough arguments");
return IntPtr.Zero;
}
IntPtr uargs = Runtime.PyTuple_GetSlice(args, 1, len);
IntPtr inst = Runtime.PyTuple_GetItem(args, 0);
Runtime.Incref(inst);
IntPtr r = self.m.Invoke(inst, uargs, kw, self.info);
Runtime.Decref(inst);
Runtime.Decref(uargs);
return r;
}
return self.m.Invoke(self.target, args, kw, self.info);
}
//====================================================================
// MethodBinding __hash__ implementation.
//====================================================================
public static IntPtr tp_hash(IntPtr ob) {
MethodBinding self = (MethodBinding)GetManagedObject(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);
}
//====================================================================
// MethodBinding __repr__ implementation.
//====================================================================
public static IntPtr tp_repr(IntPtr ob) {
MethodBinding self = (MethodBinding)GetManagedObject(ob);
string type = (self.target == IntPtr.Zero) ? "unbound" : "bound";
string s = String.Format("<{0} method '{1}'>", type, self.m.name);
return Runtime.PyString_FromStringAndSize(s, s.Length);
}
//====================================================================
// MethodBinding dealloc implementation.
//====================================================================
public static new void tp_dealloc(IntPtr ob) {
MethodBinding self = (MethodBinding)GetManagedObject(ob);
Runtime.Decref(self.target);
ExtensionType.FinalizeObject(self);
}
}
}