Repository navigation
Expand file tree
/
Copy path_functools.cs
More file actions
283 lines (235 loc) · 11.2 KB
/
Copy path_functools.cs
File metadata and controls
283 lines (235 loc) · 11.2 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
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* [email protected]. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
using IronPython.Runtime;
using IronPython.Runtime.Binding;
using IronPython.Runtime.Operations;
[assembly: PythonModule("_functools", typeof(IronPython.Modules.FunctionTools))]
namespace IronPython.Modules {
public static class FunctionTools {
public const string __doc__ = "provides functionality for manipulating callable objects";
public static object reduce(CodeContext/*!*/ context, SiteLocalStorage<CallSite<Func<CallSite, CodeContext, object, object, object, object>>> siteData, object func, object seq) {
return Builtin.reduce(context, siteData, func, seq);
}
public static object reduce(CodeContext/*!*/ context, SiteLocalStorage<CallSite<Func<CallSite, CodeContext, object, object, object, object>>> siteData, object func, object seq, object initializer) {
return Builtin.reduce(context, siteData, func, seq, initializer);
}
/// <summary>
/// Returns a new callable object with the provided initial set of arguments
/// bound to it. Calling the new function then appends to the additional
/// user provided arguments.
/// </summary>
[PythonType]
public class partial : IWeakReferenceable {
private const string _defaultDoc = "partial(func, *args, **keywords) - new function with partial application\n of the given arguments and keywords.\n";
private object/*!*/ _function; // the callable function to dispatch to
private object[]/*!*/ _args; // the initially provided arguments
private IDictionary<object, object> _keywordArgs; // the initially provided keyword arguments or null
private CodeContext/*!*/ _context; // code context from the caller who created us
private CallSite<Func<CallSite, CodeContext, object, object[], IDictionary<object, object>, object>> _dictSite; // the dictionary call site if ever called w/ keyword args
private CallSite<Func<CallSite, CodeContext, object, object[], object>> _splatSite; // the position only call site
private PythonDictionary _dict; // dictionary for storing extra attributes
private WeakRefTracker _tracker; // tracker so users can use Python weak references
private string _doc; // A custom docstring, if used
#region Constructors
/// <summary>
/// Creates a new partial object with the provided positional arguments.
/// </summary>
public partial(CodeContext/*!*/ context, object func, [NotNull]params object[]/*!*/ args)
: this(context, func, null, args) {
}
/// <summary>
/// Creates a new partial object with the provided positional and keyword arguments.
/// </summary>
public partial(CodeContext/*!*/ context, object func, [ParamDictionary]IDictionary<object, object> keywords, [NotNull]params object[]/*!*/ args) {
if (!PythonOps.IsCallable(context, func)) {
throw PythonOps.TypeError("the first argument must be callable");
}
_function = func;
_keywordArgs = keywords;
_args = args;
_context = context;
}
#endregion
#region Public Python API
[SpecialName, PropertyMethod, WrapperDescriptor]
public static object Get__doc__(CodeContext context, partial self) {
return self._doc ?? _defaultDoc;
}
[SpecialName, PropertyMethod, WrapperDescriptor]
public static void Set__doc__(partial self, object value) {
self._doc = value as string;
}
/// <summary>
/// Gets the function which will be called
/// </summary>
public object func {
get {
return _function;
}
}
/// <summary>
/// Gets the initially provided positional arguments.
/// </summary>
public object args {
get {
return PythonTuple.MakeTuple(_args);
}
}
/// <summary>
/// Gets the initially provided keyword arguments or None.
/// </summary>
public object keywords {
get {
return _keywordArgs;
}
}
/// <summary>
/// Gets or sets the dictionary used for storing extra attributes on the partial object.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public PythonDictionary __dict__ {
get {
return EnsureDict();
}
set {
_dict = value;
}
}
[SpecialName, PropertyMethod]
public void Delete__dict__() {
throw PythonOps.TypeError("partial's dictionary may not be deleted");
}
// This exists for subtypes because we don't yet automap DeleteMember onto __delattr__
public void __delattr__(string name) {
if (name == "__dict__") Delete__dict__();
if (_dict != null) {
_dict.Remove(name);
}
}
#endregion
#region Operator methods
/// <summary>
/// Calls func with the previously provided arguments and more positional arguments.
/// </summary>
[SpecialName]
public object Call(CodeContext/*!*/ context, params object[] args) {
if (_keywordArgs == null) {
EnsureSplatSite();
return _splatSite.Target(_splatSite, context, _function, ArrayUtils.AppendRange(_args, args));
}
EnsureDictSplatSite();
return _dictSite.Target(_dictSite, context, _function, ArrayUtils.AppendRange(_args, args), _keywordArgs);
}
/// <summary>
/// Calls func with the previously provided arguments and more positional arguments and keyword arguments.
/// </summary>
[SpecialName]
public object Call(CodeContext/*!*/ context, [ParamDictionary]IDictionary<object, object> dict, params object[] args) {
IDictionary<object, object> finalDict;
if (_keywordArgs != null) {
PythonDictionary pd = new PythonDictionary();
pd.update(context, _keywordArgs);
pd.update(context, dict);
finalDict = pd;
} else {
finalDict = dict;
}
EnsureDictSplatSite();
return _dictSite.Target(_dictSite, context, _function, ArrayUtils.AppendRange(_args, args), finalDict);
}
/// <summary>
/// Operator method to set arbitrary members on the partial object.
/// </summary>
[SpecialName]
public void SetMemberAfter(CodeContext/*!*/ context, string name, object value) {
EnsureDict();
_dict[name] = value;
}
/// <summary>
/// Operator method to get additional arbitrary members defined on the partial object.
/// </summary>
[SpecialName]
public object GetBoundMember(CodeContext/*!*/ context, string name) {
object value;
if (_dict != null && _dict.TryGetValue(name, out value)) {
return value;
}
return OperationFailed.Value;
}
/// <summary>
/// Operator method to delete arbitrary members defined in the partial object.
/// </summary>
[SpecialName]
public bool DeleteMember(CodeContext/*!*/ context, string name) {
switch (name) {
case "__dict__":
Delete__dict__();
break;
}
if (_dict == null) return false;
return _dict.Remove(name);
}
#endregion
#region Internal implementation details
private void EnsureSplatSite() {
if (_splatSite == null) {
Interlocked.CompareExchange(
ref _splatSite,
CallSite<Func<CallSite, CodeContext, object, object[], object>>.Create(
Binders.InvokeSplat(PythonContext.GetContext(_context))
),
null
);
}
}
private void EnsureDictSplatSite() {
if (_dictSite == null) {
Interlocked.CompareExchange(
ref _dictSite,
CallSite<Func<CallSite, CodeContext, object, object[], IDictionary<object, object>, object>>.Create(
Binders.InvokeKeywords(PythonContext.GetContext(_context))
),
null
);
}
}
private PythonDictionary EnsureDict() {
if (_dict == null) {
_dict = PythonDictionary.MakeSymbolDictionary();
}
return _dict;
}
#endregion
#region IWeakReferenceable Members
WeakRefTracker IWeakReferenceable.GetWeakRef() {
return _tracker;
}
bool IWeakReferenceable.SetWeakRef(WeakRefTracker value) {
return Interlocked.CompareExchange<WeakRefTracker>(ref _tracker, value, null) == null;
}
void IWeakReferenceable.SetFinalizer(WeakRefTracker value) {
_tracker = value;
}
#endregion
}
}
}