FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet_sample/src/runtime/managedtype.cs at master · usingsystem007/pythonnet_sample · GitHub
usingsystem007
pythonnet_sample
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet_sample
/
src
/
runtime
/
managedtype.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
268 lines (236 loc) · 7.86 KB
Breadcrumbs
pythonnet_sample
/
src
/
runtime
/
managedtype.cs
Copy path
File metadata and controls
268 lines (236 loc) · 7.86 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
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
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Diagnostics
;
using
System
.
Runtime
.
InteropServices
;
using
System
.
Linq
;
namespace
Python
.
Runtime
{
/// <summary>
/// Common base class for all objects that are implemented in managed
/// code. It defines the common fields that associate CLR and Python
/// objects and common utilities to convert between those identities.
/// </summary>
[
Serializable
]
internal
abstract
class
ManagedType
{
internal
enum
TrackTypes
{
Untrack
,
Extension
,
Wrapper
,
}
[
NonSerialized
]
internal
GCHandle
gcHandle
;
// Native handle
internal
IntPtr
pyHandle
;
// PyObject *
internal
IntPtr
tpHandle
;
// PyType *
internal
BorrowedReference
ObjectReference
=>
new
BorrowedReference
(
pyHandle
)
;
private
static
readonly
Dictionary
<
ManagedType
,
TrackTypes
>
_managedObjs
=
new
Dictionary
<
ManagedType
,
TrackTypes
>
(
)
;
internal
void
IncrRefCount
(
)
{
Runtime
.
XIncref
(
pyHandle
)
;
}
internal
void
DecrRefCount
(
)
{
Runtime
.
XDecref
(
pyHandle
)
;
}
internal
long
RefCount
{
get
{
var
gs
=
Runtime
.
PyGILState_Ensure
(
)
;
try
{
return
Runtime
.
Refcount
(
pyHandle
)
;
}
finally
{
Runtime
.
PyGILState_Release
(
gs
)
;
}
}
}
internal
GCHandle
AllocGCHandle
(
TrackTypes
track
=
TrackTypes
.
Untrack
)
{
gcHandle
=
GCHandle
.
Alloc
(
this
)
;
if
(
track
!=
TrackTypes
.
Untrack
)
{
_managedObjs
.
Add
(
this
,
track
)
;
}
return
gcHandle
;
}
internal
void
FreeGCHandle
(
)
{
_managedObjs
.
Remove
(
this
)
;
if
(
gcHandle
.
IsAllocated
)
{
gcHandle
.
Free
(
)
;
gcHandle
=
default
;
}
}
internal
static
ManagedType
GetManagedObject
(
BorrowedReference
ob
)
=>
GetManagedObject
(
ob
.
DangerousGetAddress
(
)
)
;
/// <summary>
/// Given a Python object, return the associated managed object or null.
/// </summary>
internal
static
ManagedType
GetManagedObject
(
IntPtr
ob
)
{
if
(
ob
!=
IntPtr
.
Zero
)
{
IntPtr
tp
=
Runtime
.
PyObject_TYPE
(
ob
)
;
if
(
tp
==
Runtime
.
PyTypeType
||
tp
==
Runtime
.
PyCLRMetaType
)
{
tp
=
ob
;
}
var
flags
=
Util
.
ReadCLong
(
tp
,
TypeOffset
.
tp_flags
)
;
if
(
(
flags
&
TypeFlags
.
Managed
)
!=
0
)
{
IntPtr
op
=
tp
==
ob
?
Marshal
.
ReadIntPtr
(
tp
,
TypeOffset
.
magic
(
)
)
:
Marshal
.
ReadIntPtr
(
ob
,
ObjectOffset
.
magic
(
tp
)
)
;
if
(
op
==
IntPtr
.
Zero
)
{
return
null
;
}
var
gc
=
(
GCHandle
)
op
;
return
(
ManagedType
)
gc
.
Target
;
}
}
return
null
;
}
/// <summary>
/// Given a Python object, return the associated managed object type or null.
/// </summary>
internal
static
ManagedType
GetManagedObjectType
(
IntPtr
ob
)
{
if
(
ob
!=
IntPtr
.
Zero
)
{
IntPtr
tp
=
Runtime
.
PyObject_TYPE
(
ob
)
;
var
flags
=
Util
.
ReadCLong
(
tp
,
TypeOffset
.
tp_flags
)
;
if
(
(
flags
&
TypeFlags
.
Managed
)
!=
0
)
{
tp
=
Marshal
.
ReadIntPtr
(
tp
,
TypeOffset
.
magic
(
)
)
;
var
gc
=
(
GCHandle
)
tp
;
return
(
ManagedType
)
gc
.
Target
;
}
}
return
null
;
}
internal
static
ManagedType
GetManagedObjectErr
(
IntPtr
ob
)
{
ManagedType
result
=
GetManagedObject
(
ob
)
;
if
(
result
==
null
)
{
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
"invalid argument, expected CLR type"
)
;
}
return
result
;
}
internal
static
bool
IsManagedType
(
BorrowedReference
ob
)
=>
IsManagedType
(
ob
.
DangerousGetAddressOrNull
(
)
)
;
internal
static
bool
IsManagedType
(
IntPtr
ob
)
{
if
(
ob
!=
IntPtr
.
Zero
)
{
IntPtr
tp
=
Runtime
.
PyObject_TYPE
(
ob
)
;
if
(
tp
==
Runtime
.
PyTypeType
||
tp
==
Runtime
.
PyCLRMetaType
)
{
tp
=
ob
;
}
var
flags
=
Util
.
ReadCLong
(
tp
,
TypeOffset
.
tp_flags
)
;
if
(
(
flags
&
TypeFlags
.
Managed
)
!=
0
)
{
return
true
;
}
}
return
false
;
}
public
bool
IsTypeObject
(
)
{
return
pyHandle
==
tpHandle
;
}
internal
static
IDictionary
<
ManagedType
,
TrackTypes
>
GetManagedObjects
(
)
{
return
_managedObjs
;
}
internal
static
void
ClearTrackedObjects
(
)
{
_managedObjs
.
Clear
(
)
;
}
internal
static
int
PyVisit
(
IntPtr
ob
,
IntPtr
visit
,
IntPtr
arg
)
{
if
(
ob
==
IntPtr
.
Zero
)
{
return
0
;
}
var
visitFunc
=
NativeCall
.
GetDelegate
<
Interop
.
ObjObjFunc
>
(
visit
)
;
return
visitFunc
(
ob
,
arg
)
;
}
/// <summary>
/// Wrapper for calling tp_clear
/// </summary>
internal
void
CallTypeClear
(
)
{
if
(
tpHandle
==
IntPtr
.
Zero
||
pyHandle
==
IntPtr
.
Zero
)
{
return
;
}
var
clearPtr
=
Marshal
.
ReadIntPtr
(
tpHandle
,
TypeOffset
.
tp_clear
)
;
if
(
clearPtr
==
IntPtr
.
Zero
)
{
return
;
}
var
clearFunc
=
NativeCall
.
GetDelegate
<
Interop
.
InquiryFunc
>
(
clearPtr
)
;
clearFunc
(
pyHandle
)
;
}
/// <summary>
/// Wrapper for calling tp_traverse
/// </summary>
internal
void
CallTypeTraverse
(
Interop
.
ObjObjFunc
visitproc
,
IntPtr
arg
)
{
if
(
tpHandle
==
IntPtr
.
Zero
||
pyHandle
==
IntPtr
.
Zero
)
{
return
;
}
var
traversePtr
=
Marshal
.
ReadIntPtr
(
tpHandle
,
TypeOffset
.
tp_traverse
)
;
if
(
traversePtr
==
IntPtr
.
Zero
)
{
return
;
}
var
traverseFunc
=
NativeCall
.
GetDelegate
<
Interop
.
ObjObjArgFunc
>
(
traversePtr
)
;
var
visiPtr
=
Marshal
.
GetFunctionPointerForDelegate
(
visitproc
)
;
traverseFunc
(
pyHandle
,
visiPtr
,
arg
)
;
}
protected
void
TypeClear
(
)
{
ClearObjectDict
(
pyHandle
)
;
}
internal
void
Save
(
InterDomainContext
context
)
{
OnSave
(
context
)
;
}
internal
void
Load
(
InterDomainContext
context
)
{
OnLoad
(
context
)
;
}
protected
virtual
void
OnSave
(
InterDomainContext
context
)
{
}
protected
virtual
void
OnLoad
(
InterDomainContext
context
)
{
}
protected
static
void
ClearObjectDict
(
IntPtr
ob
)
{
IntPtr
dict
=
GetObjectDict
(
ob
)
;
if
(
dict
==
IntPtr
.
Zero
)
{
return
;
}
SetObjectDict
(
ob
,
IntPtr
.
Zero
)
;
Runtime
.
XDecref
(
dict
)
;
}
protected
static
IntPtr
GetObjectDict
(
IntPtr
ob
)
{
IntPtr
type
=
Runtime
.
PyObject_TYPE
(
ob
)
;
return
Marshal
.
ReadIntPtr
(
ob
,
ObjectOffset
.
TypeDictOffset
(
type
)
)
;
}
protected
static
void
SetObjectDict
(
IntPtr
ob
,
IntPtr
value
)
{
IntPtr
type
=
Runtime
.
PyObject_TYPE
(
ob
)
;
Marshal
.
WriteIntPtr
(
ob
,
ObjectOffset
.
TypeDictOffset
(
type
)
,
value
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL