@@ -517,7 +517,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
517
517
case TypeCode . Int32 :
518
518
{
519
519
// Python3 always use PyLong API
520
- long num = Runtime . PyLong_AsLongLong ( value ) ;
520
+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
521
521
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
522
522
{
523
523
goto convert_error ;
@@ -547,7 +547,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
547
547
goto type_error ;
548
548
}
549
549
550
- int num = Runtime . PyLong_AsLong ( value ) ;
550
+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
551
551
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
552
552
{
553
553
goto convert_error ;
@@ -573,7 +573,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
573
573
goto type_error ;
574
574
}
575
575
576
- int num = Runtime . PyLong_AsLong ( value ) ;
576
+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
577
577
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
578
578
{
579
579
goto convert_error ;
@@ -610,7 +610,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
610
610
}
611
611
goto type_error ;
612
612
}
613
- int num = Runtime . PyLong_AsLong ( value ) ;
613
+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
614
614
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
615
615
{
616
616
goto convert_error ;
@@ -625,7 +625,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
625
625
626
626
case TypeCode . Int16 :
627
627
{
628
- int num = Runtime . PyLong_AsLong ( value ) ;
628
+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
629
629
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
630
630
{
631
631
goto convert_error ;
@@ -640,18 +640,35 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
640
640
641
641
case TypeCode . Int64 :
642
642
{
643
- long num = ( long ) Runtime . PyLong_AsLongLong ( value ) ;
644
- if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
643
+ if ( Runtime . Is32Bit )
645
644
{
646
- goto convert_error ;
645
+ if ( ! Runtime . PyLong_Check ( value ) )
646
+ {
647
+ goto type_error ;
648
+ }
649
+ long num = Runtime . PyExplicitlyConvertToInt64 ( value ) ;
650
+ if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
651
+ {
652
+ goto convert_error ;
653
+ }
654
+ result = num ;
655
+ return true ;
656
+ }
657
+ else
658
+ {
659
+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
660
+ if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
661
+ {
662
+ goto convert_error ;
663
+ }
664
+ result = ( long ) num ;
665
+ return true ;
647
666
}
648
- result = num ;
649
- return true ;
650
667
}
651
668
652
669
case TypeCode . UInt16 :
653
670
{
654
- long num = Runtime . PyLong_AsLong ( value ) ;
671
+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
655
672
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
656
673
{
657
674
goto convert_error ;
@@ -666,43 +683,16 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
666
683
667
684
case TypeCode . UInt32 :
668
685
{
669
- op = value ;
670
- if ( Runtime . PyObject_TYPE ( value ) != Runtime . PyLongType )
671
- {
672
- op = Runtime . PyNumber_Long ( value ) ;
673
- if ( op == IntPtr . Zero )
674
- {
675
- goto convert_error ;
676
- }
677
- }
678
- if ( Runtime . Is32Bit || Runtime . IsWindows )
686
+ nuint num = Runtime . PyLong_AsUnsignedSize_t ( value ) ;
687
+ if ( num == unchecked ( ( nuint ) ( - 1 ) ) && Exceptions . ErrorOccurred ( ) )
679
688
{
680
- uint num = Runtime . PyLong_AsUnsignedLong32 ( op ) ;
681
- if ( num == uint . MaxValue && Exceptions . ErrorOccurred ( ) )
682
- {
683
- goto convert_error ;
684
- }
685
- result = num ;
689
+ goto convert_error ;
686
690
}
687
- else
691
+ if ( num > UInt32 . MaxValue )
688
692
{
689
- ulong num = Runtime . PyLong_AsUnsignedLong64 ( op ) ;
690
- if ( num == ulong . MaxValue && Exceptions . ErrorOccurred ( ) )
691
- {
692
- goto convert_error ;
693
- }
694
- try
695
- {
696
- result = Convert . ToUInt32 ( num ) ;
697
- }
698
- catch ( OverflowException )
699
- {
700
- // Probably wasn't an overflow in python but was in C# (e.g. if cpython
701
- // longs are 64 bit then 0xFFFFFFFF + 1 will not overflow in
702
- // PyLong_AsUnsignedLong)
703
- goto overflow ;
704
- }
693
+ goto overflow ;
705
694
}
695
+ result = ( uint ) num ;
706
696
return true ;
707
697
}
708
698
0 commit comments