forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyType.cs
More file actions
324 lines (264 loc) · 11.1 KB
/
Copy pathPyType.cs
File metadata and controls
324 lines (264 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
using System;
using System.Diagnostics;
using System.Runtime.Serialization;
using Python.Runtime.Native;
namespace Python.Runtime
{
[Serializable]
public class PyType : PyObject
{
/// <summary>Creates heap type object from the <paramref name="spec"/>.</summary>
public PyType(TypeSpec spec, PyTuple? bases = null) : base(FromSpec(spec, bases)) { }
/// <summary>Wraps an existing type object.</summary>
public PyType(PyObject o) : base(FromObject(o)) { }
internal PyType(PyType o)
: base(o is not null ? o.Reference : throw new ArgumentNullException(nameof(o)))
{
}
internal PyType(BorrowedReference reference, bool prevalidated = false) : base(reference)
{
if (prevalidated) return;
if (!Runtime.PyType_Check(this))
throw new ArgumentException("object is not a type");
}
internal PyType(in StolenReference reference, bool prevalidated = false) : base(reference)
{
if (prevalidated) return;
if (!Runtime.PyType_Check(this))
throw new ArgumentException("object is not a type");
}
protected PyType(SerializationInfo info, StreamingContext context) : base(info, context) { }
internal new static PyType? FromNullableReference(BorrowedReference reference)
=> reference == null
? null
: new PyType(new NewReference(reference).Steal());
internal static PyType FromReference(BorrowedReference reference)
=> FromNullableReference(reference) ?? throw new ArgumentNullException(nameof(reference));
public string Name
{
get
{
var namePtr = new StrPtr
{
RawPointer = Util.ReadIntPtr(this, TypeOffset.tp_name),
};
return namePtr.ToString(System.Text.Encoding.UTF8)!;
}
}
/// <summary>Returns <c>true</c> when type is fully initialized</summary>
public bool IsReady => Flags.HasFlag(TypeFlags.Ready);
internal TypeFlags Flags
{
get => (TypeFlags)Util.ReadCLong(this, TypeOffset.tp_flags);
set => Util.WriteCLong(this, TypeOffset.tp_flags, (long)value);
}
internal PyDict Dict => new(Util.ReadRef(this, TypeOffset.tp_dict));
internal PyTuple MRO => new(GetMRO(this));
/// <summary>Checks if specified object is a Python type.</summary>
public static bool IsType(PyObject value)
{
if (value is null) throw new ArgumentNullException(nameof(value));
return Runtime.PyType_Check(value.obj);
}
/// <summary>Checks if specified object is a Python type.</summary>
internal static bool IsType(BorrowedReference value)
{
return Runtime.PyType_Check(value);
}
/// <summary>
/// Gets <see cref="PyType"/>, which represents the specified CLR type.
/// </summary>
public static PyType Get(Type clrType)
{
if (clrType is null)
{
throw new ArgumentNullException(nameof(clrType));
}
return new PyType(ClassManager.GetClass(clrType));
}
internal BorrowedReference BaseReference
{
get => GetBase(Reference);
set => Runtime.ReplaceReference(this, TypeOffset.tp_base, new NewReference(value).Steal());
}
internal IntPtr GetSlot(TypeSlotID slot)
{
IntPtr result = Runtime.PyType_GetSlot(this.Reference, slot);
return Exceptions.ErrorCheckIfNull(result);
}
internal static TypeFlags GetFlags(BorrowedReference type)
{
Debug.Assert(TypeOffset.tp_flags > 0);
return (TypeFlags)Util.ReadCLong(type, TypeOffset.tp_flags);
}
internal static void SetFlags(BorrowedReference type, TypeFlags flags)
{
Debug.Assert(TypeOffset.tp_flags > 0);
Util.WriteCLong(type, TypeOffset.tp_flags, (long)flags);
}
internal static BorrowedReference GetBase(BorrowedReference type)
{
Debug.Assert(IsType(type));
return Util.ReadRef(type, TypeOffset.tp_base);
}
internal static BorrowedReference GetBases(BorrowedReference type)
{
Debug.Assert(IsType(type));
return Util.ReadRef(type, TypeOffset.tp_bases);
}
internal static BorrowedReference GetMRO(BorrowedReference type)
{
Debug.Assert(IsType(type));
return Util.ReadRef(type, TypeOffset.tp_mro);
}
private static BorrowedReference FromObject(PyObject o)
{
if (o is null) throw new ArgumentNullException(nameof(o));
if (!IsType(o)) throw new ArgumentException("object is not a type");
return o.Reference;
}
private static StolenReference FromSpec(TypeSpec spec, PyTuple? bases = null)
{
if (spec is null) throw new ArgumentNullException(nameof(spec));
if ((spec.Flags & TypeFlags.HeapType) == 0)
throw new NotSupportedException("Only heap types are supported");
using var nativeSpec = new NativeTypeSpec(spec);
var basesRef = bases is null ? default : bases.Reference;
var result = Runtime.PyType_FromSpecWithBases(in nativeSpec, basesRef);
// Runtime.PyErr_Print();
return result.StealOrThrow();
}
}
}
using System;
using System.Diagnostics;
using System.Runtime.Serialization;
using Python.Runtime.Native;
namespace Python.Runtime
{
[Serializable]
public class PyType : PyObject
{
/// <summary>Creates heap type object from the <paramref name="spec"/>.</summary>
public PyType(TypeSpec spec, PyTuple? bases = null) : base(FromSpec(spec, bases)) { }
/// <summary>Wraps an existing type object.</summary>
public PyType(PyObject o) : base(FromObject(o)) { }
internal PyType(PyType o)
: base(o is not null ? o.Reference : throw new ArgumentNullException(nameof(o)))
{
}
internal PyType(BorrowedReference reference, bool prevalidated = false) : base(reference)
{
if (prevalidated) return;
if (!Runtime.PyType_Check(this))
throw new ArgumentException("object is not a type");
}
internal PyType(in StolenReference reference, bool prevalidated = false) : base(reference)
{
if (prevalidated) return;
if (!Runtime.PyType_Check(this))
throw new ArgumentException("object is not a type");
}
protected PyType(SerializationInfo info, StreamingContext context) : base(info, context) { }
internal new static PyType? FromNullableReference(BorrowedReference reference)
=> reference == null
? null
: new PyType(new NewReference(reference).Steal());
internal static PyType FromReference(BorrowedReference reference)
=> FromNullableReference(reference) ?? throw new ArgumentNullException(nameof(reference));
public string Name
{
get
{
var namePtr = new StrPtr
{
RawPointer = Util.ReadIntPtr(this, TypeOffset.tp_name),
};
return namePtr.ToString(Encodings.UTF8)!;
}
}
/// <summary>Returns <c>true</c> when type is fully initialized</summary>
public bool IsReady => Flags.HasFlag(TypeFlags.Ready);
internal TypeFlags Flags
{
get => (TypeFlags)Util.ReadCLong(this, TypeOffset.tp_flags);
set => Util.WriteCLong(this, TypeOffset.tp_flags, (long)value);
}
internal PyDict Dict => new(Util.ReadRef(this, TypeOffset.tp_dict));
internal PyTuple MRO => new(GetMRO(this));
/// <summary>Checks if specified object is a Python type.</summary>
public static bool IsType(PyObject value)
{
if (value is null) throw new ArgumentNullException(nameof(value));
return Runtime.PyType_Check(value.obj);
}
/// <summary>Checks if specified object is a Python type.</summary>
internal static bool IsType(BorrowedReference value)
{
return Runtime.PyType_Check(value);
}
/// <summary>
/// Gets <see cref="PyType"/>, which represents the specified CLR type.
/// </summary>
public static PyType Get(Type clrType)
{
if (clrType is null)
{
throw new ArgumentNullException(nameof(clrType));
}
return new PyType(ClassManager.GetClass(clrType));
}
internal BorrowedReference BaseReference
{
get => GetBase(Reference);
set => Runtime.ReplaceReference(this, TypeOffset.tp_base, new NewReference(value).Steal());
}
internal IntPtr GetSlot(TypeSlotID slot)
{
IntPtr result = Runtime.PyType_GetSlot(this.Reference, slot);
return Exceptions.ErrorCheckIfNull(result);
}
internal static TypeFlags GetFlags(BorrowedReference type)
{
Debug.Assert(TypeOffset.tp_flags > 0);
return (TypeFlags)Util.ReadCLong(type, TypeOffset.tp_flags);
}
internal static void SetFlags(BorrowedReference type, TypeFlags flags)
{
Debug.Assert(TypeOffset.tp_flags > 0);
Util.WriteCLong(type, TypeOffset.tp_flags, (long)flags);
}
internal static BorrowedReference GetBase(BorrowedReference type)
{
Debug.Assert(IsType(type));
return Util.ReadRef(type, TypeOffset.tp_base);
}
internal static BorrowedReference GetBases(BorrowedReference type)
{
Debug.Assert(IsType(type));
return Util.ReadRef(type, TypeOffset.tp_bases);
}
internal static BorrowedReference GetMRO(BorrowedReference type)
{
Debug.Assert(IsType(type));
return Util.ReadRef(type, TypeOffset.tp_mro);
}
private static BorrowedReference FromObject(PyObject o)
{
if (o is null) throw new ArgumentNullException(nameof(o));
if (!IsType(o)) throw new ArgumentException("object is not a type");
return o.Reference;
}
private static StolenReference FromSpec(TypeSpec spec, PyTuple? bases = null)
{
if (spec is null) throw new ArgumentNullException(nameof(spec));
if ((spec.Flags & TypeFlags.HeapType) == 0)
throw new NotSupportedException("Only heap types are supported");
using var nativeSpec = new NativeTypeSpec(spec);
var basesRef = bases is null ? default : bases.Reference;
var result = Runtime.PyType_FromSpecWithBases(in nativeSpec, basesRef);
// Runtime.PyErr_Print();
return result.StealOrThrow();
}
}
}