Repository navigation
Expand file tree
/
Copy pathReflectedEvent.cs
More file actions
258 lines (209 loc) · 9.41 KB
/
Copy pathReflectedEvent.cs
File metadata and controls
258 lines (209 loc) · 9.41 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
/* ****************************************************************************
*
* 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.Reflection;
using System.Runtime.CompilerServices;
using IronPython.Runtime.Exceptions;
using IronPython.Runtime.Operations;
using Microsoft.Scripting.Actions;
using Microsoft.Scripting.Generation;
using Microsoft.Scripting.Utils;
namespace IronPython.Runtime.Types {
/// <summary>
/// The unbound representation of an event property
/// </summary>
[PythonType("event#")]
public sealed class ReflectedEvent : PythonTypeDataSlot, ICodeFormattable {
private readonly bool _clsOnly;
private readonly EventTracker/*!*/ _tracker;
internal ReflectedEvent(EventTracker/*!*/ tracker, bool clsOnly) {
Assert.NotNull(tracker);
_clsOnly = clsOnly;
_tracker = tracker;
}
#region Internal APIs
internal override bool TryGetValue(CodeContext/*!*/ context, object instance, PythonType owner, out object value) {
Assert.NotNull(context, owner);
value = new BoundEvent(this, instance, owner);
return true;
}
internal override bool GetAlwaysSucceeds {
get {
return true;
}
}
internal override bool TrySetValue(CodeContext/*!*/ context, object instance, PythonType owner, object value) {
Assert.NotNull(context);
BoundEvent et = value as BoundEvent;
if (et == null || EventInfosDiffer(et)) {
BadEventChange bea = value as BadEventChange;
if (bea != null) {
PythonType dt = bea.Owner as PythonType;
if (dt != null) {
if (bea.Instance == null) {
throw new MissingMemberException(String.Format("attribute '{1}' of '{0}' object is read-only", dt.Name, _tracker.Name));
} else {
throw new MissingMemberException(String.Format("'{0}' object has no attribute '{1}'", dt.Name, _tracker.Name));
}
}
}
throw ReadOnlyException(DynamicHelpers.GetPythonTypeFromType(Info.DeclaringType));
}
return true;
}
private bool EventInfosDiffer(BoundEvent et) {
// if they're the same object they're the same...
if (et.Event.Info == this.Info) {
return false;
}
// otherwise compare based upon type & metadata token (they
// differ by ReflectedType)
if (et.Event.Info.DeclaringType != Info.DeclaringType ||
et.Event.Info.MetadataToken != Info.MetadataToken) {
return true;
}
return false;
}
internal override bool TryDeleteValue(CodeContext/*!*/ context, object instance, PythonType owner) {
Assert.NotNull(context, owner);
throw ReadOnlyException(DynamicHelpers.GetPythonTypeFromType(Info.DeclaringType));
}
internal override bool IsAlwaysVisible {
get {
return !_clsOnly;
}
}
#endregion
#region Public Python APIs
public string __doc__ {
get {
return DocBuilder.CreateAutoDoc(_tracker.Event);
}
}
public EventInfo/*!*/ Info {
[PythonHidden]
get {
return _tracker.Event;
}
}
public EventTracker/*!*/ Tracker {
[PythonHidden]
get {
return _tracker;
}
}
/// <summary>
/// BoundEvent is the object that gets returned when the user gets an event object. An
/// BoundEvent tracks where the event was received from and is used to verify we get
/// a proper add when dealing w/ statics events.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] // TODO: fix
public class BoundEvent {
private readonly ReflectedEvent/*!*/ _event;
private readonly PythonType/*!*/ _ownerType;
private readonly object _instance;
public ReflectedEvent/*!*/ Event {
get {
return _event;
}
}
public BoundEvent(ReflectedEvent/*!*/ reflectedEvent, object instance, PythonType/*!*/ ownerType) {
Assert.NotNull(reflectedEvent, ownerType);
_event = reflectedEvent;
_instance = instance;
_ownerType = ownerType;
}
// this one's correct, InPlaceAdd is wrong but we still have some dependencies on the wrong name.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2225:OperatorOverloadsHaveNamedAlternates")] // TODO: fix
[SpecialName]
public object op_AdditionAssignment(CodeContext/*!*/ context, object func) {
return InPlaceAdd(context, func);
}
[SpecialName]
public object InPlaceAdd(CodeContext/*!*/ context, object func) {
if (func == null || !PythonOps.IsCallable(context, func)) {
throw PythonOps.TypeError("event addition expected callable object, got {0}", PythonTypeOps.GetName(func));
}
if (_event.Tracker.IsStatic) {
if (_ownerType != DynamicHelpers.GetPythonTypeFromType(_event.Tracker.DeclaringType)) {
// mutating static event, only allow this from the type we're mutating, not sub-types
return new BadEventChange(_ownerType, _instance);
}
}
MethodInfo add = _event.Tracker.GetAddMethod();
// TODO (tomat): this used to use event.ReflectedType, is it still correct?
if (_instance != null) {
add = CompilerHelpers.TryGetCallableMethod(_instance.GetType(), add);
}
if (CompilerHelpers.IsVisible(add) || context.LanguageContext.DomainManager.Configuration.PrivateBinding) {
_event.Tracker.AddHandler(_instance, func, context.LanguageContext.DelegateCreator);
} else {
throw new TypeErrorException("Cannot add handler to a private event.");
}
return this;
}
[SpecialName]
public object InPlaceSubtract(CodeContext/*!*/ context, object func) {
Assert.NotNull(context);
if (func == null) {
throw PythonOps.TypeError("event subtraction expected callable object, got None");
}
if (_event.Tracker.IsStatic) {
if (_ownerType != DynamicHelpers.GetPythonTypeFromType(_event.Tracker.DeclaringType)) {
// mutating static event, only allow this from the type we're mutating, not sub-types
return new BadEventChange(_ownerType, _instance);
}
}
MethodInfo remove = _event.Tracker.GetRemoveMethod();
if (CompilerHelpers.IsVisible(remove) || context.LanguageContext.DomainManager.Configuration.PrivateBinding) {
_event.Tracker.RemoveHandler(_instance, func, PythonContext.GetContext(context).EqualityComparer);
} else {
throw new TypeErrorException("Cannot add handler to a private event.");
}
return this;
}
}
#endregion
#region Private Helpers
private class BadEventChange {
private readonly PythonType/*!*/ _ownerType;
private readonly object _instance;
public BadEventChange(PythonType/*!*/ ownerType, object instance) {
_ownerType = ownerType;
_instance = instance;
}
public PythonType Owner {
get {
return _ownerType;
}
}
public object Instance {
get {
return _instance;
}
}
}
private MissingMemberException/*!*/ ReadOnlyException(PythonType/*!*/ dt) {
Assert.NotNull(dt);
return new MissingMemberException(String.Format("attribute '{1}' of '{0}' object is read-only", dt.Name, _tracker.Name));
}
#endregion
#region ICodeFormattable Members
public string/*!*/ __repr__(CodeContext/*!*/ context) {
return string.Format("<event# {0} on {1}>", Info.Name, Info.DeclaringType.Name);
}
#endregion
}
}