@@ -511,7 +511,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
511
511
case TypeCode . Int32 :
512
512
{
513
513
// Python3 always use PyLong API
514
- long num = Runtime . PyLong_AsLongLong ( value ) ;
514
+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
515
515
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
516
516
{
517
517
goto convert_error ;
@@ -541,7 +541,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
541
541
goto type_error ;
542
542
}
543
543
544
- int num = Runtime . PyLong_AsLong ( value ) ;
544
+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
545
545
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
546
546
{
547
547
goto convert_error ;
@@ -567,7 +567,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
567
567
goto type_error ;
568
568
}
569
569
570
- int num = Runtime . PyLong_AsLong ( value ) ;
570
+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
571
571
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
572
572
{
573
573
goto convert_error ;
@@ -604,7 +604,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
604
604
}
605
605
goto type_error ;
606
606
}
607
- int num = Runtime . PyLong_AsLong ( value ) ;
607
+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
608
608
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
609
609
{
610
610
goto convert_error ;
@@ -619,7 +619,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
619
619
620
620
case TypeCode . Int16 :
621
621
{
622
- int num = Runtime . PyLong_AsLong ( value ) ;
622
+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
623
623
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
624
624
{
625
625
goto convert_error ;
@@ -634,18 +634,35 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
634
634
635
635
case TypeCode . Int64 :
636
636
{
637
- long num = ( long ) Runtime . PyLong_AsLongLong ( value ) ;
638
- if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
637
+ if ( Runtime . Is32Bit )
639
638
{
640
- goto convert_error ;
639
+ if ( ! Runtime . PyLong_Check ( value ) )
640
+ {
641
+ goto type_error ;
642
+ }
643
+ long num = Runtime . PyExplicitlyConvertToInt64 ( value ) ;
644
+ if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
645
+ {
646
+ goto convert_error ;
647
+ }
648
+ result = num ;
649
+ return true ;
650
+ }
651
+ else
652
+ {
653
+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
654
+ if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
655
+ {
656
+ goto convert_error ;
657
+ }
658
+ result = ( long ) num ;
659
+ return true ;
641
660
}
642
- result = num ;
643
- return true ;
644
661
}
645
662
646
663
case TypeCode . UInt16 :
647
664
{
648
- long num = Runtime . PyLong_AsLong ( value ) ;
665
+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
649
666
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
650
667
{
651
668
goto convert_error ;
@@ -660,43 +677,16 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
660
677
661
678
case TypeCode . UInt32 :
662
679
{
663
- op = value ;
664
- if ( Runtime . PyObject_TYPE ( value ) != Runtime . PyLongType )
665
- {
666
- op = Runtime . PyNumber_Long ( value ) ;
667
- if ( op == IntPtr . Zero )
668
- {
669
- goto convert_error ;
670
- }
671
- }
672
- if ( Runtime . Is32Bit || Runtime . IsWindows )
680
+ nuint num = Runtime . PyLong_AsUnsignedSize_t ( value ) ;
681
+ if ( num == unchecked ( ( nuint ) ( - 1 ) ) && Exceptions . ErrorOccurred ( ) )
673
682
{
674
- uint num = Runtime . PyLong_AsUnsignedLong32 ( op ) ;
675
- if ( num == uint . MaxValue && Exceptions . ErrorOccurred ( ) )
676
- {
677
- goto convert_error ;
678
- }
679
- result = num ;
683
+ goto convert_error ;
680
684
}
681
- else
685
+ if ( num > UInt32 . MaxValue )
682
686
{
683
- ulong num = Runtime . PyLong_AsUnsignedLong64 ( op ) ;
684
- if ( num == ulong . MaxValue && Exceptions . ErrorOccurred ( ) )
685
- {
686
- goto convert_error ;
687
- }
688
- try
689
- {
690
- result = Convert . ToUInt32 ( num ) ;
691
- }
692
- catch ( OverflowException )
693
- {
694
- // Probably wasn't an overflow in python but was in C# (e.g. if cpython
695
- // longs are 64 bit then 0xFFFFFFFF + 1 will not overflow in
696
- // PyLong_AsUnsignedLong)
697
- goto overflow ;
698
- }
687
+ goto overflow ;
699
688
}
689
+ result = ( uint ) num ;
700
690
return true ;
701
691
}
702
692
0 commit comments