@@ -298,7 +298,7 @@ internal static bool ToManaged(IntPtr value, Type type,
298
298
{
299
299
type = type . GetElementType ( ) ;
300
300
}
301
- return Converter . ToManagedValue ( value , type , out result , setError ) ;
301
+ return Converter . ToManagedValue ( new BorrowedReference ( value ) , type , out result , setError ) ;
302
302
}
303
303
/// <summary>
304
304
/// Return a managed object for the given Python object, taking funny
@@ -315,13 +315,9 @@ internal static bool ToManaged(BorrowedReference value, Type type,
315
315
316
316
internal static bool ToManagedValue ( BorrowedReference value , Type obType ,
317
317
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 )
321
318
{
322
319
if ( obType == typeof ( PyObject ) )
323
320
{
324
- Runtime . XIncref ( value ) ; // PyObject() assumes ownership
325
321
result = new PyObject ( value ) ;
326
322
return true ;
327
323
}
@@ -330,7 +326,7 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
330
326
&& ! obType . IsAbstract
331
327
&& obType . GetConstructor ( new [ ] { typeof ( PyObject ) } ) is { } ctor )
332
328
{
333
- var untyped = new PyObject ( new BorrowedReference ( value ) ) ;
329
+ var untyped = new PyObject ( value ) ;
334
330
result = ToPyObjectSubclass ( ctor , untyped , setError ) ;
335
331
return result is not null ;
336
332
}
@@ -370,15 +366,15 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
370
366
throw new ArgumentException ( "We should never receive instances of other managed types" ) ;
371
367
}
372
368
373
- if ( value == Runtime . PyNone && ! obType . IsValueType )
369
+ if ( value . IsNone ( ) && ! obType . IsValueType )
374
370
{
375
371
result = null ;
376
372
return true ;
377
373
}
378
374
379
375
if ( obType . IsGenericType && obType . GetGenericTypeDefinition ( ) == typeof ( Nullable < > ) )
380
376
{
381
- if ( value == Runtime . PyNone )
377
+ if ( value . IsNone ( ) )
382
378
{
383
379
result = null ;
384
380
return true ;
@@ -421,15 +417,15 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
421
417
}
422
418
423
419
// 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 ) ;
425
421
if ( PyObjectConversions . TryDecode ( value , pyType , obType , out result ) )
426
422
{
427
423
return true ;
428
424
}
429
425
430
426
if ( Runtime . PyInt_Check ( value ) )
431
427
{
432
- result = new PyInt ( new BorrowedReference ( value ) ) ;
428
+ result = new PyInt ( value ) ;
433
429
return true ;
434
430
}
435
431
@@ -438,7 +434,6 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
438
434
return ToArray ( value , typeof ( object [ ] ) , out result , setError ) ;
439
435
}
440
436
441
- Runtime . XIncref ( value ) ; // PyObject() assumes ownership
442
437
result = new PyObject ( value ) ;
443
438
return true ;
444
439
}
@@ -492,7 +487,7 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
492
487
493
488
if ( DecodableByUser <
10000
span class=pl-kos>(obType ) )
494
489
{
495
- IntPtr pyType = Runtime . PyObject_TYPE ( value ) ;
490
+ var pyType = Runtime . PyObject_TYPE ( value ) ;
496
491
if ( PyObjectConversions . TryDecode ( value , pyType , obType , out result ) )
497
492
{
498
493
return true ;
@@ -583,12 +578,10 @@ internal static int ToInt32(BorrowedReference value)
583
578
return checked ( ( int ) num ) ;
584
579
}
585
580
586
- private static bool ToPrimitive ( BorrowedReference value , Type obType , out object ? result , bool setError )
587
- => ToPrimitive ( value . DangerousGetAddress ( ) , obType , out result , setError ) ;
588
581
/// <summary>
589
582
/// Convert a Python value to an instance of a primitive managed type.
590
583
/// </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 )
592
585
{
593
586
result = null ;
594
587
if ( obType . IsEnum )
@@ -649,7 +642,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object? result, b
649
642
650
643
case TypeCode . Byte :
651
644
{
652
- if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyBytesType ) )
645
+ if ( Runtime . PyObject_TypeCheck ( value , new BorrowedReference ( Runtime . PyBytesType ) ) )
653
646
{
654
647
if ( Runtime . PyBytes_Size ( value ) == 1 )
655
648
{
@@ -675,7 +668,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object? result, b
675
668
676
669
case TypeCode . SByte :
677
670
{
678
- if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyBytesType ) )
671
+ if ( Runtime . PyObject_TypeCheck ( value , new BorrowedReference ( Runtime . PyBytesType ) ) )
679
672
{
680
673
if ( Runtime . PyBytes_Size ( value ) == 1 )
681
674
{
@@ -701,7 +694,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object? result, b
701
694
702
695
case TypeCode . Char :
703
696
{
704
- if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyBytesType ) )
697
+ if ( Runtime . PyObject_TypeCheck ( value , new BorrowedReference ( Runtime . PyBytesType ) ) )
705
698
{
706
699
if ( Runtime . PyBytes_Size ( value ) == 1 )
707
700
{
@@ -711,11 +704,11 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object? result, b
711
704
}
712
705
goto type_error ;
713
706
}
714
- else if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyUnicodeType ) )
707
+ else if ( Runtime . PyObject_TypeCheck ( value , new BorrowedReference ( Runtime . PyUnicodeType ) ) )
715
708
{
716
709
if ( Runtime . PyUnicode_GetSize ( value ) == 1 )
717
710
{
718
- op = Runtime . PyUnicode_AsUnicode ( value ) ;
711
+ op = Runtime . PyUnicode_AsUnicode ( value ) . DangerousMoveToPointer ( ) ;
719
712
Char [ ] buff = new Char [ 1 ] ;
720
713
Marshal . Copy ( op , buff , 0 , 1 ) ;
721
714
result = buff [ 0 ] ;
@@ -892,14 +885,13 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object? result, b
892
885
return false ;
893
886
}
894
887
895
- private static void SetConversionError ( IntPtr value , Type target )
741A
888
+ private static void SetConversionError ( BorrowedReference value , Type target )
896
889
{
897
890
// PyObject_Repr might clear the error
898
891
Runtime . PyErr_Fetch ( out var causeType , out var causeVal , out var causeTrace ) ;
899
892
900
- IntPtr ob = Runtime . PyObject_Repr ( value ) ;
893
+ using var ob = Runtime . PyObject_Repr ( value ) ;
901
894
string src = Runtime . GetManagedString ( ob ) ;
902
- Runtime . XDecref ( ob ) ;
903
895
904
896
Runtime . PyErr_Restore ( causeType . StealNullable ( ) , causeVal . StealNullable ( ) , causeTrace . StealNullable ( ) ) ;
905
897
Exceptions . RaiseTypeError ( $ "Cannot convert { src } to { target } ") ;
@@ -911,12 +903,12 @@ private static void SetConversionError(IntPtr value, Type target)
911
903
/// The Python value must support the Python iterator protocol or and the
912
904
/// items in the sequence must be convertible to the target array type.
913
905
/// </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 )
915
907
{
916
908
Type elementType = obType . GetElementType ( ) ;
917
909
result = null ;
918
910
919
- using var IterObject = Runtime . PyObject_GetIter ( new BorrowedReference ( value ) ) ;
911
+ using var IterObject = Runtime . PyObject_GetIter ( value ) ;
920
912
if ( IterObject . IsNull ( ) )
921
913
{
922
914
if ( setError )
0 commit comments