FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet-rawsvnmig/pythonnet/src/runtime/eventobject.cs at master · pythonnet/pythonnet-rawsvnmig · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Jun 4, 2019. It is now read-only.
pythonnet
/
pythonnet-rawsvnmig
Public archive
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Issues
0
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-rawsvnmig
/
pythonnet
/
src
/
runtime
/
eventobject.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
230 lines (176 loc) · 8.29 KB
Breadcrumbs
pythonnet-rawsvnmig
/
pythonnet
/
src
/
runtime
/
eventobject.cs
Copy path
File metadata and controls
230 lines (176 loc) · 8.29 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
228
229
230
// ==========================================================================
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
// ==========================================================================
using
System
;
using
System
.
Collections
;
using
System
.
Reflection
;
namespace
Python
.
Runtime
{
//========================================================================
// Implements a Python descriptor type that provides access to CLR events.
//========================================================================
internal
class
EventObject
:
ExtensionType
{
internal
string
name
;
internal
EventBinding
unbound
;
internal
EventInfo
info
;
internal
Hashtable
reg
;
public
EventObject
(
EventInfo
info
)
:
base
(
)
{
this
.
name
=
info
.
Name
;
this
.
info
=
info
;
}
//====================================================================
// Register a new Python object event handler with the event.
//====================================================================
internal
bool
AddEventHandler
(
IntPtr
target
,
IntPtr
handler
)
{
Object
obj
=
null
;
if
(
target
!=
IntPtr
.
Zero
)
{
CLRObject
co
=
(
CLRObject
)
ManagedType
.
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
=
this
.
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
!=
null
)
?
obj
:
this
.
info
.
ReflectedType
;
ArrayList
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
=
this
.
info
.
GetAddMethod
(
true
)
;
mi
.
Invoke
(
obj
,
BindingFlags
.
Default
,
null
,
args
,
null
)
;
return
true
;
}
//====================================================================
// Remove the given Python object event handler.
//====================================================================
internal
bool
RemoveEventHandler
(
IntPtr
target
,
IntPtr
handler
)
{
Object
obj
=
null
;
if
(
target
!=
IntPtr
.
Zero
)
{
CLRObject
co
=
(
CLRObject
)
ManagedType
.
GetManagedObject
(
target
)
;
obj
=
co
.
inst
;
}
IntPtr
hash
=
Runtime
.
PyObject_Hash
(
handler
)
;
if
(
Exceptions
.
ErrorOccurred
(
)
||
(
reg
==
null
)
)
{
Exceptions
.
SetError
(
Exceptions
.
ValueError
,
"unknown event handler"
)
;
return
false
;
}
object
key
=
(
obj
!=
null
)
?
obj
:
this
.
info
.
ReflectedType
;
ArrayList
list
=
reg
[
key
]
as
ArrayList
;
if
(
list
==
null
)
{
Exceptions
.
SetError
(
Exceptions
.
ValueError
,
"unknown event handler"
)
;
return
false
;
}
object
[
]
args
=
{
null
}
;
MethodInfo
mi
=
this
.
info
.
GetRemoveMethod
(
true
)
;
for
(
int
i
=
0
;
i
<
list
.
Count
;
i
++
)
{
Handler
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
;
}
//====================================================================
// Descriptor __get__ implementation. A getattr on an event returns
// a "bound" event that keeps a reference to the object instance.
//====================================================================
public
static
IntPtr
tp_descr_get
(
IntPtr
ds
,
IntPtr
ob
,
IntPtr
tp
)
{
EventObject
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
.
Incref
(
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
;
}
//====================================================================
// 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.
//====================================================================
public
static
new
int
tp_descr_set
(
IntPtr
ds
,
IntPtr
ob
,
IntPtr
val
)
{
EventBinding
e
=
GetManagedObject
(
val
)
as
EventBinding
;
if
(
e
!=
null
)
{
return
0
;
}
string
message
=
"cannot set event attributes"
;
Exceptions
.
RaiseTypeError
(
message
)
;
return
-
1
;
}
//====================================================================
// Descriptor __repr__ implementation.
//====================================================================
public
static
IntPtr
tp_repr
(
IntPtr
ob
)
{
EventObject
self
=
(
EventObject
)
GetManagedObject
(
ob
)
;
string
s
=
String
.
Format
(
"<event '{0}'>"
,
self
.
name
)
;
return
Runtime
.
PyString_FromString
(
s
)
;
}
//====================================================================
// Descriptor dealloc implementation.
//====================================================================
public
static
new
void
tp_dealloc
(
IntPtr
ob
)
{
EventObject
self
=
(
EventObject
)
GetManagedObject
(
ob
)
;
if
(
self
.
unbound
!=
null
)
{
Runtime
.
Decref
(
self
.
unbound
.
pyHandle
)
;
}
ExtensionType
.
FinalizeObject
(
self
)
;
}
}
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