@@ -298,7 +298,7 @@ internal static bool ToManaged(IntPtr value, Type type,
298298 {
299299 type = type . GetElementType ( ) ;
300300 }
301- return Converter . ToManagedValue ( value , type , out result , setError ) ;
301+ return Converter . ToManagedValue ( new BorrowedReference ( value ) , type , out result , setError ) ;
302302 }
303303 /// <summary>
304304 /// Return a managed object for the given Python object, taking funny
@@ -315,13 +315,9 @@ internal static bool ToManaged(BorrowedReference value, Type type,
315315
316316 internal static bool ToManagedValue ( BorrowedReference value , Type obType ,
317317 out object ? result , bool setError )
318- => ToManagedValue ( value . DangerousGetAddress ( ) , obType , out result , setError ) ;
319- internal static bool ToManagedValue ( IntPtr value , Type obType ,
320- out object ? result , bool setError )
321318 {
322319 if ( obType == typeof ( PyObject ) )
323320 {
324- Runtime . XIncref ( value ) ; // PyObject() assumes ownership
325321 result = new PyObject ( value ) ;
326322 return true ;
327323 }
@@ -330,7 +326,7 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
330326 && ! obType . IsAbstract
331327 && obType . GetConstructor ( new [ ] { typeof ( PyObject ) } ) is { } ctor )
332328 {
333- var untyped = new PyObject ( new BorrowedReference ( value ) ) ;
329+ var untyped = new PyObject ( value ) ;
334330 result = ToPyObjectSubclass ( ctor , untyped , setError ) ;
335331 return result is not null ;
336332 }
@@ -370,15 +366,15 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
370366 throw new ArgumentException ( "We should never receive instances of other managed types" ) ;
371367 }
372368
373- if ( value == Runtime . PyNone && ! obType . IsValueType )
369+ if ( value . IsNone ( ) && ! obType . IsValueType )
374370 {
375371 result = null ;
376372 return true ;
377373 }
378374
379375 if ( obType . IsGenericType && obType . GetGenericTypeDefinition ( ) == typeof ( Nullable < > ) )
380376 {
381- if ( value == Runtime . PyNone )
377+ if ( value . IsNone ( ) )
382378 {
383379 result = null ;
384380 return true ;
@@ -421,15 +417,15 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
421417 }
422418
423419 // give custom codecs a chance to take over conversion of ints and sequences
424- IntPtr pyType = Runtime . PyObject_TYPE ( value ) ;
420+ var pyType = Runtime . PyObject_TYPE ( value ) ;
425421 if ( PyObjectConversions . TryDecode ( value , pyType , obType , out result ) )
426422 {
427423 return true ;
428424 }
429425
430426 if ( Runtime . PyInt_Check ( value ) )
431427 {
432- result = new PyInt ( new BorrowedReference ( value ) ) ;
428+ result = new PyInt ( value ) ;
433429 return true ;
434430 }
435431
@@ -438,7 +434,6 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
438434 return ToArray ( value , typeof ( object [ ] ) , out result , setError ) ;
439435 }
440436
441- Runtime . XIncref ( value ) ; // PyObject() assumes ownership
442437 result = new PyObject ( value ) ;
443438 return true ;
444439 }
@@ -492,7 +487,7 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
492487
493488 if ( DecodableByUser ( obType ) )
494489 {
495- IntPtr pyType = Runtime . PyObject_TYPE ( value ) ;
490+ var pyType = Runtime . PyObject_TYPE ( value ) ;
496491 if ( PyObjectConversions . TryDecode ( value , pyType , obType , out result ) )
497492 {
498493 return true ;
@@ -583,12 +578,10 @@ internal static int ToInt32(BorrowedReference value)
583578 return checked ( ( int ) num ) ;
584579 }
585580
586- private static bool ToPrimitive ( BorrowedReference value , Type obType , out object ? result , bool setError )
587- => ToPrimitive ( value . DangerousGetAddress ( ) , obType , out result , setError ) ;
588581 /// <summary>
589582 /// Convert a Python value to an instance of a primitive managed type.
590583 /// </summary>
591- private static bool ToPrimitive ( IntPtr value , Type obType , out object ? result , bool setError )
584+ private static bool ToPrimitive ( BorrowedReference value , Type obType , out object ? result , bool setError )
592585 {
593586 result = null ;
594587 if ( obType . IsEnum )
@@ -649,7 +642,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object? result, b
649642
650643 case TypeCode . Byte :
651644 {
652- if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyBytesType ) )
645+ if ( Runtime . PyObject_TypeCheck ( value , new BorrowedReference ( Runtime . PyBytesType ) ) )
653646 {
654647 if ( Runtime . PyBytes_Size ( value ) == 1 )
655648 {
@@ -675,7 +668,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object? result, b
675668
676669 case TypeCode . SByte :
677670 {
678- if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyBytesType ) )
671+ if ( Runtime . PyObject_TypeCheck ( value , new BorrowedReference ( Runtime . PyBytesType ) ) )
679672 {
680673 if ( Runtime . PyBytes_Size ( value ) == 1 )
681674 {
@@ -701,7 +694,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object? result, b
701694
702695 case TypeCode . Char :
703696 {
704- if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyBytesType ) )
697+ if ( Runtime . PyObject_TypeCheck ( value , new BorrowedReference ( Runtime . PyBytesType ) ) )
705698 {
706699 if ( Runtime . PyBytes_Size ( value ) == 1 )
707700 {
@@ -711,11 +704,11 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object? result, b
711704 }
712705 goto type_error ;
713706 }
714- else if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyUnicodeType ) )
707+ else if ( Runtime . PyObject_TypeCheck ( value , new BorrowedReference ( Runtime . PyUnicodeType ) ) )
715708 {
716709 if ( Runtime . PyUnicode_GetSize ( value ) == 1 )
717710 {
718- op = Runtime . PyUnicode_AsUnicode ( value ) ;
711+ op = Runtime . PyUnicode_AsUnicode ( value ) . DangerousMoveToPointer ( ) ;
719712 Char [ ] buff = new Char [ 1 ] ;
720713 Marshal . Copy ( op , buff , 0 , 1 ) ;
721714 result = buff [ 0 ] ;
@@ -892,14 +885,13 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object? result, b
892885 return false ;
893886 }
894887
895- private static void SetConversionError ( IntPtr value , Type target )
888+ private static void SetConversionError ( BorrowedReference value , Type target )
896889 {
897890 // PyObject_Repr might clear the error
898891 Runtime . PyErr_Fetch ( out var causeType , out var causeVal , out var causeTrace ) ;
899892
900- IntPtr ob = Runtime . PyObject_Repr ( value ) ;
893+ using var ob = Runtime . PyObject_Repr ( value ) ;
901894 string src = Runtime . GetManagedString ( ob ) ;
902- Runtime . XDecref ( ob ) ;
903895
904896 Runtime . PyErr_Restore ( causeType . StealNullable ( ) , causeVal . StealNullable ( ) , causeTrace . StealNullable ( ) ) ;
905897 Exceptions . RaiseTypeError ( $ "Cannot convert { src } to { target } ") ;
@@ -911,12 +903,12 @@ private static void SetConversionError(IntPtr value, Type target)
911903 /// The Python value must support the Python iterator protocol or and the
912904 /// items in the sequence must be convertible to the target array type.
913905 /// </summary>
914- private static bool ToArray ( IntPtr value , Type obType , out object ? result , bool setError )
906+ private static bool ToArray ( BorrowedReference value , Type obType , out object ? result , bool setError )
915907 {
916908 Type elementType = obType . GetElementType ( ) ;
917909 result = null ;
918910
919- using var IterObject = Runtime . PyObject_GetIter ( new BorrowedReference ( value ) ) ;
911+ using var IterObject = Runtime . PyObject_GetIter ( value ) ;
920912 if ( IterObject . IsNull ( ) )
921913 {
922914 if ( setError )
0 commit comments