@@ -16,54 +16,40 @@ internal class MethodBinding : ExtensionType
1616 {
1717 internal MaybeMethodInfo info ;
1818 internal MethodObject m ;
19- internal IntPtr target ;
20- internal IntPtr targetType ;
19+ internal PyObject ? target ;
20+ internal PyType targetType ;
2121
22- public MethodBinding ( MethodObject m , IntPtr target , IntPtr targetType )
22+ public MethodBinding ( MethodObject m , PyObject ? target , PyType ? targetType = null )
2323 {
24- Runtime . XIncref ( target ) ;
2524 this . target = target ;
2625
27- if ( targetType == IntPtr . Zero )
28- {
29- targetType = Runtime . PyObject_Type ( target ) ;
30- }
31- else
32- {
33- Runtime . XIncref ( targetType ) ;
34- }
35-
36- this . targetType = targetType ;
26+ this . targetType = targetType ?? target . GetPythonType ( ) ;
3727
3828 this . info = null ;
3929 this . m = m ;
4030 }
4131
42- public MethodBinding ( MethodObject m , IntPtr target ) : this ( m , target , IntPtr . Zero )
43- {
44- }
45-
4632 /// <summary>
4733 /// Implement binding of generic methods using the subscript syntax [].
4834 /// </summary>
49- public static IntPtr mp_subscript ( IntPtr tp , IntPtr idx )
35+ public static NewReference mp_subscript ( BorrowedReference tp , BorrowedReference idx )
5036 {
51- var self = ( MethodBinding ) GetManagedObject ( tp ) ;
37+ var self = ( MethodBinding ) GetManagedObject ( tp ) ! ;
5238
53- Type [ ] types = Runtime . PythonArgsToTypeArray ( idx ) ;
39+ Type [ ] ? types = Runtime . PythonArgsToTypeArray ( idx ) ;
5440 if ( types == null )
5541 {
5642 return Exceptions . RaiseTypeError ( "type(s) expected" ) ;
5743 }
5844
59- MethodInfo mi = MethodBinder . MatchParameters ( self . m . info , types ) ;
45+ MethodInfo ? mi = MethodBinder . MatchParameters ( self . m . info , types ) ;
6046 if ( mi == null )
6147 {
6248 return Exceptions . RaiseTypeError ( "No match found for given type params" ) ;
6349 }
6450
6551 var mb = new MethodBinding ( self . m , self . target ) { info = mi } ;
66- return mb . pyHandle ;
52+ return new NewReference ( mb . pyHandle ) ;
6753 }
6854
6955 PyObject Signature
@@ -131,37 +117,35 @@ public int Compare(Type a, Type b)
131117 /// <summary>
132118 /// MethodBinding __getattribute__ implementation.
133119 /// </summary>
134- public static IntPtr tp_getattro ( IntPtr ob , IntPtr key )
120+ public static NewReference tp_getattro ( BorrowedReference ob , BorrowedReference key )
135121 {
136- var self = ( MethodBinding ) GetManagedObject ( ob ) ;
122+ var self = ( MethodBinding ) GetManagedObject ( ob ) ! ;
137123
138124 if ( ! Runtime . PyString_Check ( key ) )
139125 {
140126 Exceptions . SetError ( Exceptions . TypeError , "string expected" ) ;
141- return IntPtr . Zero ;
127+ return default ;
142128 }
143129
144- string name = InternString . GetManagedString ( key ) ;
130+ string ? name = InternString . GetManagedString ( key ) ;
145131 switch ( name )
146132 {
147133 case "__doc__" :
148- IntPtr doc = self . m . GetDocString ( ) ;
149- Runtime . XIncref ( doc ) ;
150- return doc ;
134+ return self . m . GetDocString ( ) ;
151135 // FIXME: deprecate __overloads__ soon...
152136 case "__overloads__" :
153137 case "Overloads" :
154138 var om = new OverloadMapper ( self . m , self . target ) ;
155- return om . pyHandle ;
139+ return new NewReference ( om . pyHandle ) ;
156140 case "__signature__" when Runtime . InspectModule is not null :
157141 var sig = self . Signature ;
158142 if ( sig is null )
159143 {
160144 return Runtime . PyObject_GenericGetAttr ( ob , key ) ;
161145 }
162- return sig . NewReferenceOrNull ( ) . DangerousMoveToPointerOrNull ( ) ;
146+ return sig . NewReferenceOrNull ( ) ;
163147 case "__name__" :
164- return self . m . GetName ( ) . DangerousMoveToPointerOrNull ( ) ;
148+ return self . m . GetName ( ) ;
165149 default :
166150 return Runtime . PyObject_GenericGetAttr ( ob , key ) ;
167151 }
@@ -171,9 +155,9 @@ public static IntPtr tp_getattro(IntPtr ob, IntPtr key)
171155 /// <summary>
172156 /// MethodBinding __call__ implementation.
173157 /// </summary>
174- public static IntPtr tp_call ( IntPtr ob , IntPtr args , IntPtr kw )
158+ public static NewReference tp_call ( BorrowedReference ob , BorrowedReference args , BorrowedReference kw )
175159 {
176- var self = ( MethodBinding ) GetManagedObject ( ob ) ;
160+ var self = ( MethodBinding ) GetManagedObject ( ob ) ! ;
177161
178162 // This works around a situation where the wrong generic method is picked,
179163 // for example this method in the tests: string Overloaded<T>(int arg1, int arg2, string arg3)
@@ -183,11 +167,11 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
183167 if ( info . IsGenericMethod )
184168 {
185169 var len = Runtime . PyTuple_Size ( args ) ; //FIXME: Never used
186- Type [ ] sigTp = Runtime . PythonArgsToTypeArray ( args , true ) ;
170+ Type [ ] ? sigTp = Runtime . PythonArgsToTypeArray ( args , true ) ;
187171 if ( sigTp != null )
188172 {
189173 Type [ ] genericTp = info . GetGenericArguments ( ) ;
190- MethodInfo betterMatch = MethodBinder . MatchSignatureAndParameters ( self . m . info , genericTp , sigTp ) ;
174+ MethodInfo ? betterMatch = MethodBinder . MatchSignatureAndParameters ( self . m . info , genericTp , sigTp ) ;
191175 if ( betterMatch != null )
192176 {
193177 self . info = betterMatch ;
@@ -200,32 +184,32 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
200184 // as the first argument. Note that this is not supported if any
201185 // of the overloads are static since we can't know if the intent
202186 // was to call the static method or the unbound instance method.
203- var disposeList = new List < IntPtr > ( ) ;
187+ var disposeList = new List < PyObject > ( ) ;
204188 try
205189 {
206- IntPtr target = self . target ;
190+ PyObject ? target = self . target ;
207191
208- if ( target == IntPtr . Zero && ! self . m . IsStatic ( ) )
192+ if ( target is null && ! self . m . IsStatic ( ) )
209193 {
210194 var len = Runtime . PyTuple_Size ( args ) ;
211195 if ( len < 1 )
212196 {
213197 Exceptions . SetError ( Exceptions . TypeError , "not enough arguments" ) ;
214- return IntPtr . Zero ;
198+ return default ;
215199 }
216- target = Runtime . PyTuple_GetItem ( args , 0 ) ;
217- Runtime . XIncref ( target ) ;
200+ target = new PyObject ( Runtime . PyTuple_GetItem ( args , 0 ) ) ;
218201 disposeList . Add ( target ) ;
219202
220- args = Runtime . PyTuple_GetSlice ( args , 1 , len ) ;
221- disposeList . Add ( args ) ;
203+ var unboundArgs = Runtime . PyTuple_GetSlice ( args , 1 , len ) . MoveToPyObject ( ) ;
204+ disposeList . Add ( unboundArgs ) ;
205+ args = unboundArgs ;
222206 }
223207
224208 // if the class is a IPythonDerivedClass and target is not the same as self.targetType
225209 // (eg if calling the base class method) then call the original base class method instead
226210 // of the target method.
227211 IntPtr superType = IntPtr . Zero ;
228- if ( Runtime . PyObject_TYPE ( target ) != self . targetType )
212+ if ( target is not null && Runtime . PyObject_TYPE ( target ) != self . targetType )
229213 {
230214 var inst = GetManagedObject ( target ) as CLRObject ;
231215 if ( inst ? . inst is IPythonDerivedType )
@@ -234,15 +218,14 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
234218 if ( baseType != null && baseType . type . Valid )
235219 {
236220 string baseMethodName = "_" + baseType . type . Value . Name + "__" + self . m . name ;
237- IntPtr baseMethod = Runtime . PyObject_GetAttrString ( target , baseMethodName ) ;
238- if ( baseMethod != IntPtr . Zero )
221+ using var baseMethod = Runtime . PyObject_GetAttrString ( target , baseMethodName ) ;
222+ if ( ! baseMethod . IsNull ( ) )
239223 {
240- var baseSelf = GetManagedObject ( baseMethod ) as MethodBinding ;
224+ var baseSelf = GetManagedObject ( baseMethod . Borrow ( ) ) as MethodBinding ;
241225 if ( baseSelf != null )
242226 {
243227 self = baseSelf ;
244228 }
245- Runtime . XDecref ( baseMethod ) ;
246229 }
247230 else
248231 {
@@ -251,13 +234,13 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
251234 }
252235 }
253236 }
254- return self . m . Invoke ( target , args , kw , self . info . UnsafeValue ) ;
237+ return self . m . Invoke ( target is null ? BorrowedReference . Null : target , args , kw , self . info . UnsafeValue ) ;
255238 }
256239 finally
257240 {
258- foreach ( IntPtr ptr in disposeList )
241+ foreach ( var ptr in disposeList )
259242 {
260- Runtime . XDecref ( ptr ) ;
243+ ptr . Dispose ( ) ;
261244 }
262245 }
263246 }
@@ -266,12 +249,12 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
266249 /// <summary>
267250 /// MethodBinding __hash__ implementation.
268251 /// </summary>
269- public static nint tp_hash ( IntPtr ob )
252+ public static nint tp_hash ( BorrowedReference ob )
270253 {
271- var self = ( MethodBinding ) GetManagedObject ( ob ) ;
254+ var self = ( MethodBinding ) GetManagedObject ( ob ) ! ;
272255 nint x = 0 ;
273256
274- if ( self . target != IntPtr . Zero )
257+ if ( self . target is not null )
275258 {
276259 x = Runtime . PyObject_Hash ( self . target ) ;
277260 if ( x == - 1 )
@@ -292,18 +275,18 @@ public static nint tp_hash(IntPtr ob)
292275 /// <summary>
293276 /// MethodBinding __repr__ implementation.
294277 /// </summary>
295- public static IntPtr tp_repr ( IntPtr ob )
278+ public static NewReference tp_repr ( BorrowedReference ob )
296279 {
297- var self = ( MethodBinding ) GetManagedObject ( ob ) ;
298- string type = self . target == IntPtr . Zero ? "unbound" : "bound" ;
280+ var self = ( MethodBinding ) GetManagedObject ( ob ) ! ;
281+ string type = self . target is null ? "unbound" : "bound" ;
299282 string name = self . m . name ;
300283 return Runtime . PyString_FromString ( $ "<{ type } method '{ name } '>") ;
301284 }
302285
303286 protected override void Clear ( )
304287 {
305- Runtime . Py_CLEAR ( ref this . target ) ;
306- Runtime . Py_CLEAR ( ref this . targetType ) ;
288+ this . target = null ;
289+ this . targetType = null ! ;
307290 base . Clear ( ) ;
308291 }
309292
0 commit comments