forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagedtype.cs
More file actions
327 lines (282 loc) · 11.1 KB
/
managedtype.cs
File metadata and controls
327 lines (282 loc) · 11.1 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#nullable enable
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(pyHandle);
internal BorrowedReference TypeReference => new(tpHandle);
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;
}
}
/// <summary>
/// Given a Python object, return the associated managed object or null.
/// </summary>
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 = (TypeFlags)Util.ReadCLong(tp, TypeOffset.tp_flags);
if ((flags & TypeFlags.HasClrInstance) != 0)
{
var gc = TryGetGCHandle(new BorrowedReference(ob));
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 = (TypeFlags)Util.ReadCLong(tp, TypeOffset.tp_flags);
if ((flags & TypeFlags.HasClrInstance) != 0)
{
var gc = GetGCHandle(new BorrowedReference(tp), Runtime.CLRMetaType);
return (ManagedType)gc.Target;
}
}
return null;
}
internal static bool IsInstanceOfManagedType(BorrowedReference ob)
=> IsInstanceOfManagedType(ob.DangerousGetAddressOrNull());
internal static bool IsInstanceOfManagedType(IntPtr ob)
{
if (ob != IntPtr.Zero)
{
IntPtr tp = Runtime.PyObject_TYPE(ob);
if (tp == Runtime.PyTypeType || tp == Runtime.PyCLRMetaType)
{
tp = ob;
}
return IsManagedType(new BorrowedReference(tp));
}
return false;
}
internal static bool IsManagedType(BorrowedReference type)
{
var flags = (TypeFlags)Util.ReadCLong(type.DangerousGetAddress(), TypeOffset.tp_flags);
return (flags & TypeFlags.HasClrInstance) != 0;
}
public bool IsClrMetaTypeInstance()
{
Debug.Assert(Runtime.PyCLRMetaType != IntPtr.Zero);
Debug.Assert(pyHandle != IntPtr.Zero);
return Runtime.PyObject_TYPE(pyHandle) == Runtime.PyCLRMetaType;
}
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 = Runtime.PyType_GetSlot(TypeReference, TypeSlotID.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 = Runtime.PyType_GetSlot(TypeReference, TypeSlotID.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);
int instanceDictOffset = Marshal.ReadInt32(type, TypeOffset.tp_dictoffset);
Debug.Assert(instanceDictOffset > 0);
return Marshal.ReadIntPtr(ob, instanceDictOffset);
}
protected static void SetObjectDict(IntPtr ob, IntPtr value)
{
IntPtr type = Runtime.PyObject_TYPE(ob);
int instanceDictOffset = Marshal.ReadInt32(type, TypeOffset.tp_dictoffset);
Debug.Assert(instanceDictOffset > 0);
Marshal.WriteIntPtr(ob, instanceDictOffset, value);
}
internal static void GetGCHandle(BorrowedReference reflectedClrObject, BorrowedReference type, out IntPtr handle)
{
Debug.Assert(reflectedClrObject != null);
Debug.Assert(IsManagedType(type) || type == Runtime.CLRMetaType);
Debug.Assert(Runtime.PyObject_TypeCheck(reflectedClrObject, type));
int gcHandleOffset = Marshal.ReadInt32(type.DangerousGetAddress(), Offsets.tp_clr_inst_offset);
Debug.Assert(gcHandleOffset > 0);
handle = Marshal.ReadIntPtr(reflectedClrObject.DangerousGetAddress(), gcHandleOffset);
}
internal static GCHandle? TryGetGCHandle(BorrowedReference reflectedClrObject, BorrowedReference type)
{
GetGCHandle(reflectedClrObject, type, out IntPtr handle);
return handle == IntPtr.Zero ? null : (GCHandle)handle;
}
internal static GCHandle? TryGetGCHandle(BorrowedReference reflectedClrObject)
{
BorrowedReference reflectedType = Runtime.PyObject_TYPE(reflectedClrObject);
return TryGetGCHandle(reflectedClrObject, reflectedType);
}
internal static GCHandle GetGCHandle(BorrowedReference reflectedClrObject)
=> TryGetGCHandle(reflectedClrObject) ?? throw new InvalidOperationException();
internal static GCHandle GetGCHandle(BorrowedReference reflectedClrObject, BorrowedReference type)
=> TryGetGCHandle(reflectedClrObject, type) ?? throw new InvalidOperationException();
internal static void InitGCHandle(BorrowedReference reflectedClrObject, BorrowedReference type, GCHandle handle)
{
Debug.Assert(TryGetGCHandle(reflectedClrObject) == null);
SetGCHandle(reflectedClrObject, type: type, handle);
}
internal static void InitGCHandle(BorrowedReference reflectedClrObject, GCHandle handle)
=> InitGCHandle(reflectedClrObject, Runtime.PyObject_TYPE(reflectedClrObject), handle);
internal static void SetGCHandle(BorrowedReference reflectedClrObject, BorrowedReference type, GCHandle newHandle)
{
Debug.Assert(Runtime.PyObject_TypeCheck(reflectedClrObject, type));
int offset = Marshal.ReadInt32(type.DangerousGetAddress(), Offsets.tp_clr_inst_offset);
Debug.Assert(offset > 0);
Marshal.WriteIntPtr(reflectedClrObject.DangerousGetAddress(), offset, (IntPtr)newHandle);
}
internal static void SetGCHandle(BorrowedReference reflectedClrObject, GCHandle newHandle)
=> SetGCHandle(reflectedClrObject, Runtime.PyObject_TYPE(reflectedClrObject), newHandle);
internal static class Offsets
{
static Offsets()
{
int pyTypeSize = Marshal.ReadInt32(Runtime.PyTypeType, TypeOffset.tp_basicsize);
if (pyTypeSize < 0) throw new InvalidOperationException();
tp_clr_inst_offset = pyTypeSize;
tp_clr_inst = tp_clr_inst_offset + IntPtr.Size;
}
public static int tp_clr_inst_offset { get; }
public static int tp_clr_inst { get; }
}
}
}