forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethod.cs
More file actions
163 lines (118 loc) · 6.03 KB
/
Method.cs
File metadata and controls
163 lines (118 loc) · 6.03 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
// 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;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
namespace IronPython.Runtime {
[PythonType("method"), DontMapGetMemberNamesToDir]
public sealed partial class Method : PythonTypeSlot, IWeakReferenceable, IPythonMembersList, IDynamicMetaObjectProvider, ICodeFormattable, Binding.IFastInvokable {
private WeakRefTracker _weakref;
// TODO: get rid of this constructor?
internal Method(object function, object instance, PythonType @class) {
if (instance == null) throw new ArgumentNullException();
__func__ = function;
__self__ = instance;
im_class = @class;
}
public Method(object function, object self) {
if (self == null) throw PythonOps.TypeError("self must not be None");
__func__ = function;
__self__ = self;
im_class = DynamicHelpers.GetPythonType(self);
}
internal string Name => (string)PythonOps.GetBoundAttr(DefaultContext.Default, __func__, "__name__");
public string __doc__ => PythonOps.GetBoundAttr(DefaultContext.Default, __func__, "__doc__") as string;
public object __func__ { get; }
public object __self__ { get; } // TODO: mark that this property is never null
internal PythonType im_class { get; } // TODO: get rid of this property?
[SpecialName]
public object Call(CodeContext/*!*/ context, params object[] args)
=> context.LanguageContext.CallSplat(this, args);
[SpecialName]
public object Call(CodeContext/*!*/ context, [ParamDictionary]IDictionary<object, object> kwArgs, params object[] args)
=> context.LanguageContext.CallWithKeywords(this, args, kwArgs);
#region Object Overrides
private string DeclaringClassAsString() => im_class == null ? "?" : im_class.Name;
public override bool Equals(object obj)
=> obj is Method other && PythonOps.IsOrEqualsRetBool(__self__, other.__self__) && PythonOps.EqualRetBool(__func__, other.__func__);
public override int GetHashCode()
=> PythonOps.Hash(DefaultContext.Default, __self__) ^ PythonOps.Hash(DefaultContext.Default, __func__);
#endregion
#region IWeakReferenceable Members
WeakRefTracker IWeakReferenceable.GetWeakRef() => _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() => PythonOps.GetStringMemberList(this);
IList<object> IPythonMembersList.GetMemberNames(CodeContext/*!*/ context) {
PythonList ret = TypeCache.Method.GetMemberNames(context);
ret.AddNoLockNoDups("__module__");
if (__func__ is PythonFunction pf) {
PythonDictionary dict = pf.__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) {
value = this;
return true;
}
internal override bool GetAlwaysSucceeds => true;
#endregion
#region ICodeFormattable Members
public string/*!*/ __repr__(CodeContext/*!*/ context) {
object name;
if (!PythonOps.TryGetBoundAttr(context, __func__, "__name__", out name)) {
name = "?";
}
return $"<bound method {DeclaringClassAsString()}.{name} of {PythonOps.Repr(context, __self__)}>";
}
#endregion
#region IDynamicMetaObjectProvider Members
DynamicMetaObject/*!*/ IDynamicMetaObjectProvider.GetMetaObject(Expression/*!*/ parameter)
=> new Binding.MetaMethod(parameter, BindingRestrictions.Empty, this);
#endregion
}
}