FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/eventobject.cs at refactor-qc-pythonnet5 · QuantConnect/pythonnet · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
QuantConnect
/
pythonnet
Public
forked from
pythonnet/pythonnet
Notifications
You must be signed in to change notification settings
Fork
31
Star
29
Code
Issues
5
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
eventobject.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
227 lines (192 loc) · 6.88 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
eventobject.cs
Copy path
File metadata and controls
227 lines (192 loc) · 6.88 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using
System
;
using
System
.
Collections
;
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
string
name
;
internal
EventBinding
unbound
;
internal
EventInfo
info
;
internal
Hashtable
reg
;
public
EventObject
(
EventInfo
info
)
{
this
.
name
=
info
.
Name
;
this
.
info
=
info
;
}
/// <summary>
/// Register a new Python object event handler with the event.
/// </summary>
internal
bool
AddEventHandler
(
IntPtr
target
,
IntPtr
handler
)
{
object
obj
=
null
;
if
(
target
!=
IntPtr
.
Zero
)
{
var
co
=
(
CLRObject
)
GetManagedObject
(
target
)
;
obj
=
co
.
inst
;
}
// Create a true delegate instance of the appropriate type to
// wrap the Python handler. Note that wrapper delegate creation
// always succeeds, though calling the wrapper may fail.
Type
type
=
info
.
EventHandlerType
;
Delegate
d
=
PythonEngine
.
DelegateManager
.
GetDelegate
(
type
,
handler
)
;
// Now register the handler in a mapping from instance to pairs
// of (handler hash, delegate) so we can lookup to remove later.
// All this is done lazily to avoid overhead until an event is
// actually subscribed to by a Python event handler.
if
(
reg
==
null
)
{
reg
=
new
Hashtable
(
)
;
}
object
key
=
obj
??
info
.
ReflectedType
;
var
list
=
reg
[
key
]
as
ArrayList
;
if
(
list
==
null
)
{
list
=
new
ArrayList
(
)
;
reg
[
key
]
=
list
;
}
list
.
Add
(
new
Handler
(
Runtime
.
PyObject_Hash
(
handler
)
,
d
)
)
;
// Note that AddEventHandler helper only works for public events,
// so we have to get the underlying add method explicitly.
object
[
]
args
=
{
d
}
;
MethodInfo
mi
=
info
.
GetAddMethod
(
true
)
;
mi
.
Invoke
(
obj
,
BindingFlags
.
Default
,
null
,
args
,
null
)
;
return
true
;
}
/// <summary>
/// Remove the given Python object event handler.
/// </summary>
internal
bool
RemoveEventHandler
(
IntPtr
target
,
IntPtr
handler
)
{
if
(
reg
==
null
)
{
Exceptions
.
SetError
(
Exceptions
.
ValueError
,
"unknown event handler"
)
;
return
false
;
}
object
obj
=
null
;
if
(
target
!=
IntPtr
.
Zero
)
{
var
co
=
(
CLRObject
)
GetManagedObject
(
target
)
;
obj
=
co
.
inst
;
}
nint
hash
=
Runtime
.
PyObject_Hash
(
handler
)
;
if
(
hash
==
-
1
&&
Exceptions
.
ErrorOccurred
(
)
)
{
return
false
;
}
object
key
=
obj
??
info
.
ReflectedType
;
var
list
=
reg
[
key
]
as
ArrayList
;
if
(
list
==
null
)
{
Exceptions
.
SetError
(
Exceptions
.
ValueError
,
"unknown event handler"
)
;
return
false
;
}
object
[
]
args
=
{
null
}
;
MethodInfo
mi
=
info
.
GetRemoveMethod
(
true
)
;
for
(
var
i
=
0
;
i
<
list
.
Count
;
i
++
)
{
var
item
=
(
Handler
)
list
[
i
]
;
if
(
item
.
hash
!=
hash
)
{
continue
;
}
args
[
0
]
=
item
.
del
;
try
{
mi
.
Invoke
(
obj
,
BindingFlags
.
Default
,
null
,
args
,
null
)
;
}
catch
{
continue
;
}
list
.
RemoveAt
(
i
)
;
return
true
;
}
Exceptions
.
SetError
(
Exceptions
.
ValueError
,
"unknown event handler"
)
;
return
false
;
}
/// <summary>
/// Descriptor __get__ implementation. A getattr on an event returns
/// a "bound" event that keeps a reference to the object instance.
/// </summary>
public
static
IntPtr
tp_descr_get
(
IntPtr
ds
,
IntPtr
ob
,
IntPtr
tp
)
{
var
self
=
GetManagedObject
(
ds
)
as
EventObject
;
EventBinding
binding
;
if
(
self
==
null
)
{
return
Exceptions
.
RaiseTypeError
(
"invalid argument"
)
;
}
// If the event is accessed through its type (rather than via
// an instance) we return an 'unbound' EventBinding that will
// be cached for future accesses through the type.
if
(
ob
==
IntPtr
.
Zero
)
{
if
(
self
.
unbound
==
null
)
{
self
.
unbound
=
new
EventBinding
(
self
,
IntPtr
.
Zero
)
;
}
binding
=
self
.
unbound
;
Runtime
.
XIncref
(
binding
.
pyHandle
)
;
return
binding
.
pyHandle
;
}
if
(
Runtime
.
PyObject_IsInstance
(
ob
,
tp
)
<
1
)
{
return
Exceptions
.
RaiseTypeError
(
"invalid argument"
)
;
}
binding
=
new
EventBinding
(
self
,
ob
)
;
return
binding
.
pyHandle
;
}
/// <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
new
static
int
tp_descr_set
(
IntPtr
ds
,
IntPtr
ob
,
IntPtr
val
)
{
var
e
=
GetManagedObject
(
val
)
as
EventBinding
;
if
(
e
!=
null
)
{
return
0
;
}
Exceptions
.
RaiseTypeError
(
"cannot set event attributes"
)
;
return
-
1
;
}
/// <summary>
/// Descriptor __repr__ implementation.
/// </summary>
public
static
IntPtr
tp_repr
(
IntPtr
ob
)
{
var
self
=
(
EventObject
)
GetManagedObject
(
ob
)
;
return
Runtime
.
PyString_FromString
(
$
"<event '
{
self
.
name
}
'>"
)
;
}
/// <summary>
/// Descriptor dealloc implementation.
/// </summary>
public
new
static
void
tp_dealloc
(
IntPtr
ob
)
{
var
self
=
(
EventObject
)
GetManagedObject
(
ob
)
;
if
(
self
.
unbound
!=
null
)
{
Runtime
.
XDecref
(
self
.
unbound
.
pyHandle
)
;
}
self
.
Dealloc
(
)
;
}
}
internal
class
Handler
{
public
IntPtr
hash
;
public
Delegate
del
;
public
Handler
(
IntPtr
hash
,
Delegate
d
)
{
this
.
hash
=
hash
;
this
.
del
=
d
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL