@@ -27,14 +27,13 @@ internal class ConstructorBinding : ExtensionType
27
27
private ConstructorBinder ctorBinder ;
28
28
29
29
[ NonSerialized ]
30
- private IntPtr repr ;
30
+ private PyObject ? repr ;
31
31
32
32
public ConstructorBinding ( Type type , PyType typeToCreate , ConstructorBinder ctorBinder )
33
33
{
34
34
this . type = type ;
35
35
this . typeToCreate = typeToCreate ;
36
36
this . ctorBinder = ctorBinder ;
37
- repr = IntPtr . Zero ;
38
37
}
39
38
40
39
/// <summary>
@@ -62,12 +61,13 @@ public ConstructorBinding(Type type, PyType typeToCreate, ConstructorBinder ctor
62
61
/// the attribute was accessed through, or None when the attribute is accessed through the owner.
63
62
/// This method should return the (computed) attribute value or raise an AttributeError exception.
64
63
/// </remarks>
65
- public static IntPtr tp_descr_get ( IntPtr op , IntPtr instance , IntPtr owner )
64
+ public static NewReference tp_descr_get ( BorrowedReference op , BorrowedReference instance , BorrowedReference owner )
66
65
{
67
- var self = ( ConstructorBinding ) GetManagedObject ( op ) ;
66
+ var self = ( ConstructorBinding ? ) GetManagedObject ( op ) ;
68
67
if ( self == null )
69
68
{
70
- return IntPtr . Zero ;
69
+ Exceptions . SetError ( Exceptions . AssertionError , "attempting to access destroyed object" ) ;
70
+ return default ;
71
71
}
72
72
73
73
// It doesn't seem to matter if it's accessed through an instance (rather than via the type).
@@ -77,8 +77,7 @@ public static IntPtr tp_descr_get(IntPtr op, IntPtr instance, IntPtr owner)
77
77
return Exceptions.RaiseTypeError("How in the world could that happen!");
78
78
}
79
79
}*/
80
- Runtime . XIncref ( self . pyHandle ) ;
81
- return self . pyHandle ;
80
+ return new NewReference ( self . pyHandle ) ;
82
81
}
83
82
84
83
/// <summary>
@@ -89,16 +88,16 @@ public static IntPtr tp_descr_get(IntPtr op, IntPtr instance, IntPtr owner)
89
88
/// Return element of o corresponding to the object key or NULL on failure.
90
89
/// This is the equivalent of the Python expression o[key].
91
90
/// </remarks>
92
- public static IntPtr mp_subscript ( IntPtr op , IntPtr key )
91
+ public static NewReference mp_subscript ( BorrowedReference op , BorrowedReference key )
93
92
{
94
- var self = ( ConstructorBinding ) GetManagedObject ( op ) ;
93
+ var self = ( ConstructorBinding ) GetManagedObject ( op ) ! ;
95
94
if ( ! self . type . Valid )
96
95
{
97
96
return Exceptions . RaiseTypeError ( self . type . DeletedMessage ) ;
98
97
}
99
98
Type tp = self . type . Value ;
100
99
101
- Type [ ] types = Runtime . PythonArgsToTypeArray ( key ) ;
100
+ Type [ ] ? types = Runtime . PythonArgsToTypeArray ( key ) ;
102
101
if ( types == null )
103
102
{
104
103
return Exceptions . RaiseTypeError ( "type(s) expected" ) ;
@@ -111,20 +110,18 @@ public static IntPtr mp_subscript(IntPtr op, IntPtr key)
111
110
return Exceptions . RaiseTypeError ( "No match found for constructor signature" ) ;
112
111
}
113
112
var boundCtor = new BoundContructor ( tp , self . typeToCreate , self . ctorBinder , ci ) ;
114
-
115
- return boundCtor . pyHandle ;
113
+ return new NewReference ( boundCtor . pyHandle ) ;
116
114
}
117
115
118
116
/// <summary>
119
117
/// ConstructorBinding __repr__ implementation [borrowed from MethodObject].
120
118
/// </summary>
121
- public static IntPtr tp_repr ( IntPtr ob )
119
+ public static NewReference tp_repr ( BorrowedReference ob )
122
120
{
123
- var self = ( ConstructorBinding ) GetManagedObject ( ob ) ;
124
- if ( self . repr != IntPtr . Zero )
121
+ var self = ( ConstructorBinding ) GetManagedObject ( ob ) ! ;
122
+ if ( self . repr is not null )
125
123
{
126
- Runtime . XIncref ( self . repr ) ;
127
- return self . repr ;
124
+ return new NewReference ( self . repr ) ;
128
125
}
129
126
MethodBase [ ] methods = self . ctorBinder . GetMethods ( ) ;
130
127
@@ -144,9 +141,10 @@ public static IntPtr tp_repr(IntPtr ob)
144
141
int idx = str . IndexOf ( "(" ) ;
145
142
doc += string . Format ( "{0}{1}" , name , str . Substring ( idx ) ) ;
146
143
}
147
- self . repr = Runtime . PyString_FromString ( doc ) ;
148
- Runtime . XIncref ( self . repr ) ;
149
- return self . repr ;
144
+ using var docStr = Runtime . PyString_FromString ( doc ) ;
145
+ if ( docStr . IsNull ( ) ) return default ;
146
+ self . repr = docStr . MoveToPyObject ( ) ;
147
+ return new NewReference ( self . repr ) ;
150
148
}
151
149
152
150
protected override void Clear ( )
@@ -155,14 +153,17 @@ protected override void Clear()
155
153
base . Clear ( ) ;
156
154
}
157
155
158
- public static int tp_traverse ( IntPtr ob , IntPtr visit , IntPtr arg )
156
+ public static int tp_traverse ( BorrowedReference ob , IntPtr visit , IntPtr arg )
159
157
{
160
- var self = ( ConstructorBinding ) GetManagedObject ( ob ) ;
161
- int res = PyVisit ( self . typeToCreate . Handle , visit , arg ) ;
158
+ var self = ( ConstructorBinding ) GetManagedObject ( ob ) ! ;
159
+ int res = PyVisit ( self . typeToCreate , visit , arg ) ;
162
160
if ( res != 0 ) return res ;
163
161
164
- res = PyVisit ( self . repr , visit , arg ) ;
165
- if ( res != 0 ) return res ;
162
+ if ( self . repr is not null )
163
+ {
164
+ res = PyVisit ( self . repr , visit , arg ) ;
165
+ if ( res != 0 ) return res ;
166
+ }
166
167
return 0 ;
167
168
}
168
169
}
@@ -182,15 +183,14 @@ internal class BoundContructor : ExtensionType
182
183
private PyType typeToCreate ; // The python type tells GetInstHandle which Type to create.
183
184
private ConstructorBinder ctorBinder ;
184
185
private ConstructorInfo ctorInfo ;
185
- private IntPtr repr ;
186
+ private PyObject ? repr ;
186
187
187
188
public BoundContructor ( Type type , PyType typeToCreate , ConstructorBinder ctorBinder , ConstructorInfo ci )
188
189
{
189
190
this . type = type ;
190
191
this . typeToCreate = typeToCreate ;
191
192
this . ctorBinder = ctorBinder ;
192
193
ctorInfo = ci ;
193
- repr = IntPtr . Zero ;
194
194
}
195
195
196
196
/// <summary>
@@ -200,45 +200,45 @@ public BoundContructor(Type type, PyType typeToCreate, ConstructorBinder ctorBin
200
200
/// <param name="args"> PyObject *args </param>
201
201
/// <param name="kw"> PyObject *kw </param>
202
202
/// <returns> A reference to a new instance of the class by invoking the selected ctor(). </returns>
203
- public static IntPtr tp_call ( IntPtr op , IntPtr args , IntPtr kw )
203
+ public static NewReference tp_call ( BorrowedReference op , BorrowedReference args , BorrowedReference kw )
204
204
{
205
- var self = ( BoundContructor ) GetManagedObject ( op ) ;
205
+ var self = ( BoundContructor ) GetManagedObject ( op ) ! ;
206
206
// Even though a call with null ctorInfo just produces the old behavior
207
207
/*if (self.ctorInfo == null) {
208
208
string msg = "Usage: Class.Overloads[CLR_or_python_Type, ...]";
209
209
return Exceptions.RaiseTypeError(msg);
210
210
}*/
211
211
// Bind using ConstructorBinder.Bind and invoke the ctor providing a null instancePtr
212
212
// which will fire self.ctorInfo using ConstructorInfo.Invoke().
213
- object obj = self . ctorBinder . InvokeRaw ( IntPtr . Zero , args , kw , self . ctorInfo ) ;
213
+ object ? obj = self . ctorBinder . InvokeRaw ( null , args , kw , self . ctorInfo ) ;
214
214
if ( obj == null )
215
215
{
216
216
// XXX set an error
217
- return IntPtr . Zero ;
217
+ return default ;
218
218
}
219
219
// Instantiate the python object that wraps the result of the method call
220
220
// and return the PyObject* to it.
221
- return CLRObject . GetReference ( obj , self . typeToCreate . Reference ) . DangerousMoveToPointer ( ) ;
221
+ return CLRObject . GetReference ( obj , self . typeToCreate ) ;
222
222
}
223
223
224
224
/// <summary>
225
225
/// BoundContructor __repr__ implementation [borrowed from MethodObject].
226
226
/// </summary>
227
- public static IntPtr tp_repr ( IntPtr ob )
227
+ public static NewReference tp_repr ( BorrowedReference ob )
228
228
{
229
- var self = ( BoundContructor ) GetManagedObject ( ob ) ;
230
- if ( self . repr != IntPtr . Zero )
229
+ var self = ( BoundContructor ) GetManagedObject ( ob ) ! ;
230
+ if ( self . repr is not null )
231
231
{
232
- Runtime . XIncref ( self . repr ) ;
233
- return self . repr ;
232
+ return new NewReference ( self . repr ) ;
234
233
}
235
234
string name = self . type . FullName ;
236
235
string str = self . ctorInfo . ToString ( ) ;
237
236
int idx = str . IndexOf ( "(" ) ;
238
237
str = string . Format ( "returns a new {0}{1}" , name , str . Substring ( idx ) ) ;
239
- self . repr = Runtime . PyString_FromString ( str ) ;
240
- Runtime . XIncref ( self . repr ) ;
241
- return self . repr ;
238
+ using var docStr = Runtime . PyString_FromString ( str ) ;
239
+ if ( docStr . IsNull ( ) ) return default ;
240
+ self . repr = docStr . MoveToPyObject ( ) ;
241
+ return new NewReference ( self . repr ) ;
242
242
}
243
243
244
244
protected override void Clear ( )
@@ -247,14 +247,17 @@ protected override void Clear()
247
247
base . Clear ( ) ;
248
248
}
249
249
250
- public static int tp_traverse ( IntPtr ob , IntPtr visit , IntPtr arg )
250
+ public static int tp_traverse ( BorrowedReference ob , IntPtr visit , IntPtr arg )
251
251
{
252
- var self = ( BoundContructor ) GetManagedObject ( ob ) ;
253
- int res = PyVisit ( self . typeToCreate . Handle , visit , arg ) ;
252
+ var self = ( BoundContructor ) GetManagedObject ( ob ) ! ;
253
+ int res = PyVisit ( self . typeToCreate , visit , arg ) ;
254
254
if ( res != 0 ) return res ;
255
255
256
- res = PyVisit ( self . repr , visit , arg ) ;
257
- if ( res != 0 ) return res ;
256
+ if ( self . repr is not null )
257
+ {
258
+ res = PyVisit ( self . repr , visit , arg ) ;
259
+ if ( res != 0 ) return res ;
260
+ }
258
261
return 0 ;
259
262
}
260
263
}
0 commit comments