@@ -517,7 +517,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
517517case TypeCode . Int32 :
518518{
519519// Python3 always use PyLong API
520- long num = Runtime . PyLong_AsLongLong ( value ) ;
520+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
521521if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
522522{
523523goto convert_error ;
@@ -547,7 +547,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
547547goto type_error ;
548548}
549549
550- int num = Runtime . PyLong_AsLong ( value ) ;
550+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
551551if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
552552{
553553goto convert_error ;
@@ -573,7 +573,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
573573goto type_error ;
574574}
575575
576- int num = Runtime . PyLong_AsLong ( value ) ;
576+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
577577if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
578578{
579579goto convert_error ;
@@ -610,7 +610,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
610610}
611611goto type_error ;
612612}
613- int num = Runtime . PyLong_AsLong ( value ) ;
613+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
614614if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
615615{
616616goto convert_error ;
@@ -625,7 +625,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
625625
626626case TypeCode . Int16 :
627627{
628- int num = Runtime . PyLong_AsLong ( value ) ;
628+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
629629if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
630630{
631631goto convert_error ;
@@ -640,18 +640,35 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
640640
641641case TypeCode . Int64 :
642642{
643- long num = ( long ) Runtime . PyLong_AsLongLong ( value ) ;
644- if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
643+ if ( Runtime . Is32Bit )
645644{
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 ;
647666}
648- result = num ;
649- return true ;
650667}
651668
652669case TypeCode . UInt16 :
653670{
654- long num = Runtime . PyLong_AsLong ( value ) ;
671+ nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
655672if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
656673{
657674goto convert_error ;
@@ -666,43 +683,16 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
666683
667684case TypeCode . UInt32 :
668685{
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 ( ) )
679688{
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 ;
686690}
687- else
691+ if ( num > UInt32 . MaxValue )
688692{
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 ;
705694}
695+ result = ( uint ) num ;
706696return true ;
707697}
708698