1
1
#nullable enable
2
2
using System ;
3
3
using System . Diagnostics ;
4
- using System . Runtime . InteropServices ;
5
4
6
5
using Python . Runtime . Native ;
7
6
@@ -24,12 +23,16 @@ internal PyType(BorrowedReference reference, bool prevalidated = false) : base(r
24
23
{
25
24
if ( prevalidated ) return ;
26
25
27
- if ( ! Runtime . PyType_Check ( this . Handle ) )
26
+ if ( ! Runtime . PyType_Check ( this ) )
28
27
throw new ArgumentException ( "object is not a type" ) ;
29
28
}
30
29
31
- internal PyType ( in StolenReference reference ) : base ( EnsureIsType ( in reference ) )
30
+ internal PyType ( in StolenReference reference , bool prevalidated = false ) : base ( reference )
32
31
{
32
+ if ( prevalidated ) return ;
33
+
34
+ if ( ! Runtime . PyType_Check ( this ) )
35
+ throw new ArgumentException ( "object is not a type" ) ;
33
36
}
34
37
35
38
internal new static PyType ? FromNullableReference ( BorrowedReference reference )
@@ -46,7 +49,7 @@ public string Name
46
49
{
47
50
var namePtr = new StrPtr
48
51
{
49
- RawPointer = Marshal . ReadIntPtr ( Handle , TypeOffset . tp_name ) ,
52
+ RawPointer = Util . ReadIntPtr ( this , TypeOffset . tp_name ) ,
50
53
} ;
51
54
return namePtr . ToString ( System . Text . Encoding . UTF8 ) ! ;
52
55
}
@@ -57,8 +60,8 @@ public string Name
57
60
58
61
internal TypeFlags Flags
59
62
{
60
- get => ( TypeFlags ) Util . ReadCLong ( Handle , TypeOffset . tp_flags ) ;
61
- set => Util . WriteCLong ( Handle , TypeOffset . tp_flags , ( long ) value ) ;
63
+ get => ( TypeFlags ) Util . ReadCLong ( this , TypeOffset . tp_flags ) ;
64
+ set => Util . WriteCLong ( this , TypeOffset . tp_flags , ( long ) value ) ;
62
65
}
63
66
64
67
/// <summary>Checks if specified object is a Python type.</summary>
@@ -71,7 +74,7 @@ public static bool IsType(PyObject value)
71
74
/// <summary>Checks if specified object is a Python type.</summary>
72
75
internal static bool IsType ( BorrowedReference value )
73
76
{
74
- return Runtime . PyType_Check ( value . DangerousGetAddress ( ) ) ;
77
+ return Runtime . PyType_Check ( value ) ;
75
78
}
76
79
77
80
/// <summary>
@@ -90,16 +93,7 @@ public static PyType Get(Type clrType)
90
93
internal BorrowedReference BaseReference
91
94
{
92
95
get => GetBase ( Reference ) ;
93
- set
94
- {
95
- var old = BaseReference . DangerousGetAddressOrNull ( ) ;
96
- IntPtr @new = value . DangerousGetAddress ( ) ;
97
-
98
- Runtime . XIncref ( @new ) ;
99
- Marshal . WriteIntPtr ( Handle , TypeOffset . tp_base , @new ) ;
100
-
101
- Runtime . XDecref ( old ) ;
102
- }
96
+ set => Runtime . ReplaceReference ( this , TypeOffset . tp_base , new NewReference ( value ) . Steal ( ) ) ;
103
97
}
104
98
105
99
internal IntPtr GetSlot ( TypeSlotID slot )
@@ -122,37 +116,21 @@ internal static void SetFlags(BorrowedReference type, TypeFlags flags)
122
116
internal static BorrowedReference GetBase ( BorrowedReference type )
123
117
{
124
118
Debug . Assert ( IsType ( type ) ) ;
125
- IntPtr basePtr = Marshal . ReadIntPtr ( type . DangerousGetAddress ( ) , TypeOffset . tp_base ) ;
126
- return new BorrowedReference ( basePtr ) ;
119
+ return Util . ReadRef ( type , TypeOffset . tp_base ) ;
127
120
}
128
121
129
122
internal static BorrowedReference GetBases ( BorrowedReference type )
130
123
{
131
124
Debug . Assert ( IsType ( type ) ) ;
132
- IntPtr basesPtr = Marshal . ReadIntPtr ( type . DangerousGetAddress ( ) , TypeOffset . tp_bases ) ;
133
- return new BorrowedReference ( basesPtr ) ;
125
+ return Util . ReadRef ( type , TypeOffset . tp_bases ) ;
134
126
}
135
127
136
128
internal static BorrowedReference GetMRO ( BorrowedReference type )
137
129
{
138
130
Debug . Assert ( IsType ( type ) ) ;
139
- IntPtr basesPtr = Marshal . ReadIntPtr ( type . DangerousGetAddress ( ) , TypeOffset . tp_mro ) ;
140
- return new BorrowedReference ( basesPtr ) ;
131
+ return Util . ReadRef ( type , TypeOffset . tp_mro ) ;
141
132
}
142
133
143
- private static IntPtr EnsureIsType ( in StolenReference reference )
144
- {
145
- IntPtr address = reference . DangerousGetAddressOrNull ( ) ;
146
- if ( address == IntPtr . Zero )
147
- throw new ArgumentNullException ( nameof ( reference ) ) ;
148
- return EnsureIsType ( address ) ;
149
- }
150
-
151
- private static IntPtr EnsureIsType ( IntPtr ob )
152
- => Runtime . PyType_Check ( ob )
153
- ? ob
154
- : throw new ArgumentException ( "object is not a type" ) ;
155
-
156
134
private static BorrowedReference FromObject ( PyObject o )
157
135
{
158
136
if ( o is null ) throw new ArgumentNullException ( nameof ( o ) ) ;
@@ -161,22 +139,17 @@ private static BorrowedReference FromObject(PyObject o)
161
139
return o . Reference ;
162
140
}
163
141
164
- private static IntPtr FromSpec ( TypeSpec spec , PyTuple ? bases = null )
142
+ private static StolenReference FromSpec ( TypeSpec spec , PyTuple ? bases = null )
165
143
{
166
144
if ( spec is null ) throw new ArgumentNullException ( nameof ( spec ) ) ;
167
145
168
146
if ( ( spec . Flags & TypeFlags . HeapType ) == 0 )
169
147
throw new NotSupportedException ( "Only heap types are supported" ) ;
170
148
171
- var nativeSpec = new NativeTypeSpec ( spec ) ;
149
+ using var nativeSpec = new NativeTypeSpec ( spec ) ;
172
150
var basesRef = bases is null ? default : bases . Reference ;
173
151
var result = Runtime . PyType_FromSpecWithBases ( in nativeSpec , basesRef ) ;
174
-
175
- PythonException . ThrowIfIsNull ( result ) ;
176
-
177
- nativeSpec . Dispose ( ) ;
178
-
179
- return result . DangerousMoveToPointer ( ) ;
152
+ return result . StealOrThrow ( ) ;
180
153
}
181
154
}
182
155
}
0 commit comments