@@ -511,7 +511,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
511511 case TypeCode . Int32 :
512512 {
513513 // Python3 always use PyLong API
514- long num = Runtime . PyLong_AsLongLong ( value ) ;
514+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
515515 if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
516516 {
517517 goto convert_error ;
@@ -541,7 +541,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
541541 goto type_error ;
542542 }
543543
544- int num = Runtime . PyLong_AsLong ( value ) ;
544+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
545545 if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
546546 {
547547 goto convert_error ;
@@ -567,7 +567,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
567567 goto type_error ;
568568 }
569569
570- int num = Runtime . PyLong_AsLong ( value ) ;
570+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
571571 if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
572572 {
573573 goto convert_error ;
@@ -604,7 +604,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
604604 }
605605 goto type_error ;
606606 }
607- int num = Runtime . PyLong_AsLong ( value ) ;
607+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
608608 if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
609609 {
610610 goto convert_error ;
@@ -619,7 +619,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
619619
620620 case TypeCode . Int16 :
621621 {
622- int num = Runtime . PyLong_AsLong ( value ) ;
622+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
623623 if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
624624 {
625625 goto convert_error ;
@@ -634,18 +634,35 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
634634
635635 case TypeCode . Int64 :
636636 {
637- long num = ( long ) Runtime . PyLong_AsLongLong ( value ) ;
638- if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
637+ if ( Runtime . Is32Bit )
639638 {
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 ;
641660 }
642- result = num ;
643- return true ;
644661 }
645662
646663 case TypeCode . UInt16 :
647664 {
648- long num = Runtime . PyLong_AsLong ( value ) ;
665+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
649666 if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
650667 {
651668 goto convert_error ;
@@ -660,43 +677,16 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
660677
661678 case TypeCode . UInt32 :
662679 {
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 ( ) )
673682 {
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 ;
680684 }
681- else
685+ if ( num > UInt32 . MaxValue )
682686 {
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 ;
699688 }
689+ result = ( uint ) num ;
700690 return true ;
701691 }
702692
0 commit comments