-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathEventObject.cs
More file actions
92 lines (77 loc) · 2.72 KB
/
EventObject.cs
File metadata and controls
92 lines (77 loc) · 2.72 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
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
namespace Python.Runtime
{
/// <summary>
/// Implements a Python descriptor type that provides access to CLR events.
/// </summary>
[Serializable]
internal class EventObject : ExtensionType
{
internal readonly string name;
internal readonly EventHandlerCollection reg;
public EventObject(EventInfo info)
{
Debug.Assert(!info.AddMethod.IsStatic);
this.name = info.Name;
this.reg = new EventHandlerCollection(info);
}
/// <summary>
/// Descriptor __get__ implementation. A getattr on an event returns
/// a "bound" event that keeps a reference to the object instance.
/// </summary>
public static NewReference tp_descr_get(BorrowedReference ds, BorrowedReference ob, BorrowedReference tp)
{
var self = GetManagedObject(ds) as EventObject;
if (self == null)
{
return Exceptions.RaiseTypeError("invalid argument");
}
if (ob == null)
{
return new NewReference(ds);
}
if (Runtime.PyObject_IsInstance(ob, tp) < 1)
{
return Exceptions.RaiseTypeError("invalid argument");
}
return new EventBinding(self.name, self.reg, new PyObject(ob)).Alloc();
}
/// <summary>
/// Descriptor __set__ implementation. This actually never allows you
/// to set anything; it exists solely to support the '+=' spelling of
/// event handler registration. The reason is that given code like:
/// 'ob.SomeEvent += method', Python will attempt to set the attribute
/// SomeEvent on ob to the result of the '+=' operation.
/// </summary>
public static int tp_descr_set(BorrowedReference ds, BorrowedReference ob, BorrowedReference val)
{
if (GetManagedObject(val) is EventBinding _)
{
return 0;
}
Exceptions.RaiseTypeError("cannot set event attributes");
return -1;
}
/// <summary>
/// Descriptor __repr__ implementation.
/// </summary>
public static NewReference tp_repr(BorrowedReference ob)
{
var self = (EventObject)GetManagedObject(ob)!;
return Runtime.PyString_FromString($"<event '{self.name}'>");
}
}
internal class Handler
{
public readonly nint hash;
public readonly Delegate del;
public Handler(nint hash, Delegate d)
{
this.hash = hash;
this.del = d;
}
}
}