Repository navigation
Expand file tree
/
Copy pathMethod.cs
More file actions
266 lines (211 loc) · 8.67 KB
/
Copy pathMethod.cs
File metadata and controls
266 lines (211 loc) · 8.67 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.Linq.Expressions;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Dynamic;
using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
namespace IronPython.Runtime {
[PythonType("instancemethod"), DontMapGetMemberNamesToDir]
public sealed partial class Method : PythonTypeSlot, IWeakReferenceable, IPythonMembersList, IDynamicMetaObjectProvider, ICodeFormattable, Binding.IFastInvokable {
private readonly object _func;
private readonly object _inst;
private readonly object _declaringClass;
private WeakRefTracker _weakref;
public Method(object function, object instance, object @class) {
_func = function;
_inst = instance;
_declaringClass = @class;
}
public Method(object function, object instance) {
if (instance == null) {
throw PythonOps.TypeError("unbound methods must have a class provided");
}
_func = function;
_inst = instance;
}
internal string Name {
get { return (string)PythonOps.GetBoundAttr(DefaultContext.Default, _func, "__name__"); }
}
public string __doc__ {
get {
return PythonOps.GetBoundAttr(DefaultContext.Default, _func, "__doc__") as string;
}
}
public object im_func {
get {
return _func;
}
}
public object __func__ {
get {
return _func;
}
}
public object im_self {
get {
return _inst;
}
}
public object __self__ {
get {
return _inst;
}
}
public object im_class {
get {
// we could have an OldClass (or any other object) here if the user called the ctor directly
return PythonOps.ToPythonType(_declaringClass as PythonType) ?? _declaringClass;
}
}
[SpecialName]
public object Call(CodeContext/*!*/ context, params object[] args) {
return context.LanguageContext.CallSplat(this, args);
}
[SpecialName]
public object Call(CodeContext/*!*/ context, [ParamDictionary]IDictionary<object, object> kwArgs, params object[] args) {
return context.LanguageContext.CallWithKeywords(this, args, kwArgs);
}
private Exception BadSelf(object got) {
OldClass dt = im_class as OldClass;
string firstArg;
if (got == null) {
firstArg = "nothing";
} else {
firstArg = PythonOps.GetPythonTypeName(got) + " instance";
}
PythonType pt = im_class as PythonType;
return PythonOps.TypeError("unbound method {0}() must be called with {1} instance as first argument (got {2} instead)",
Name,
(dt != null) ? dt.Name : (pt != null) ? pt.Name : im_class,
firstArg);
}
/// <summary>
/// Validates that the current self object is usable for this method.
/// </summary>
internal object CheckSelf(CodeContext context, object self) {
if (!PythonOps.IsInstance(context, self, im_class)) {
throw BadSelf(self);
}
return self;
}
#region Object Overrides
private string DeclaringClassAsString() {
if (im_class == null) return "?";
if (im_class is PythonType dt) return dt.Name;
if (im_class is OldClass oc) return oc.Name;
return im_class.ToString();
}
public override bool Equals(object obj) {
if (!(obj is Method other)) return false;
return
(object.ReferenceEquals(_inst, other._inst) || PythonOps.EqualRetBool(_inst, other._inst)) &&
PythonOps.EqualRetBool(_func, other._func);
}
public override int GetHashCode() {
if (_inst == null) return PythonOps.Hash(DefaultContext.Default, _func);
return PythonOps.Hash(DefaultContext.Default, _inst) ^ PythonOps.Hash(DefaultContext.Default, _func);
}
#endregion
#region IWeakReferenceable Members
WeakRefTracker IWeakReferenceable.GetWeakRef() {
return _weakref;
}
bool IWeakReferenceable.SetWeakRef(WeakRefTracker value) {
_weakref = value;
return true;
}
void IWeakReferenceable.SetFinalizer(WeakRefTracker value) {
((IWeakReferenceable)this).SetWeakRef(value);
}
#endregion
#region Custom member access
[SpecialName]
public object GetCustomMember(CodeContext context, string name) {
switch (name) {
// Get the module name from the function and pass that out. Note that CPython's method has
// no __module__ attribute and this value can be gotten via a call to method.__getattribute__
// there as well.
case "__module__":
return PythonOps.GetBoundAttr(context, _func, "__module__");
case "__name__":
return PythonOps.GetBoundAttr(DefaultContext.Default, _func, "__name__");
default:
object value;
string symbol = name;
if (TypeCache.Method.TryGetBoundMember(context, this, symbol, out value) || // look on method
PythonOps.TryGetBoundAttr(context, _func, symbol, out value)) { // Forward to the func
return value;
}
return OperationFailed.Value;
}
}
[SpecialName]
public void SetMemberAfter(CodeContext context, string name, object value) {
TypeCache.Method.SetMember(context, this, name, value);
}
[SpecialName]
public void DeleteMember(CodeContext context, string name) {
TypeCache.Method.DeleteMember(context, this, name);
}
IList<string> IMembersList.GetMemberNames() {
return PythonOps.GetStringMemberList(this);
}
IList<object> IPythonMembersList.GetMemberNames(CodeContext/*!*/ context) {
List ret = TypeCache.Method.GetMemberNames(context);
ret.AddNoLockNoDups("__module__");
if (_func is PythonFunction pf) {
PythonDictionary dict = pf.func_dict;
// Check the func
foreach (KeyValuePair<object, object> kvp in dict) {
ret.AddNoLockNoDups(kvp.Key);
}
}
return ret;
}
#endregion
#region PythonTypeSlot Overrides
internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value) {
if (this.im_self == null) {
if (owner == null || owner == im_class || PythonOps.IsSubClass(context, owner, im_class)) {
value = new Method(_func, instance, owner);
return true;
}
}
value = this;
return true;
}
internal override bool GetAlwaysSucceeds {
get {
return true;
}
}
#endregion
#region ICodeFormattable Members
public string/*!*/ __repr__(CodeContext/*!*/ context) {
object name;
if (!PythonOps.TryGetBoundAttr(context, _func, "__name__", out name)) {
name = "?";
}
if (_inst != null) {
return string.Format("<bound method {0}.{1} of {2}>",
DeclaringClassAsString(),
name,
PythonOps.Repr(context, _inst));
} else {
return string.Format("<unbound method {0}.{1}>", DeclaringClassAsString(), name);
}
}
#endregion
#region IDynamicMetaObjectProvider Members
DynamicMetaObject/*!*/ IDynamicMetaObjectProvider.GetMetaObject(Expression/*!*/ parameter) {
return new Binding.MetaMethod(parameter, BindingRestrictions.Empty, this);
}
#endregion
}
}