@@ -688,7 +688,12 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
688
688
case TypeCode . Int32 :
689
689
{
690
690
// Python3 always use PyLong API
691
- nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
691
+ op = Runtime . PyNumber_Long ( value ) ;
692
+ if ( op == IntPtr . Zero && Exceptions . ErrorOccurred ( ) )
693
+ {
694
+ goto convert_error ;
695
+ }
696
+ nint num = Runtime . PyLong_AsSignedSize_t ( op ) ;
692
697
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
693
698
{
694
699
goto convert_error ;
@@ -796,7 +801,12 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
796
801
797
802
case TypeCode . Int16 :
798
803
{
799
- nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
804
+ op = Runtime . PyNumber_Long ( value ) ;
805
+ if ( op == IntPtr . Zero && Exceptions . ErrorOccurred ( ) )
806
+ {
807
+ goto convert_error ;
808
+ }
809
+ nint num = Runtime . PyLong_AsSignedSize_t ( op ) ;
800
810
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
801
811
{
802
812
goto convert_error ;
@@ -827,7 +837,12 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
827
837
}
828
838
else
829
839
{
830
- nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
840
+ op = Runtime . PyNumber_Long ( value ) ;
841
+ if ( op == IntPtr . Zero && Exceptions . ErrorOccurred ( ) )
842
+ {
843
+ goto convert_error ;
844
+ }
845
+ nint num = Runtime . PyLong_AsSignedSize_t ( op ) ;
831
846
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
832
847
{
833
848
goto convert_error ;
@@ -839,7 +854,12 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
839
854
840
855
case TypeCode . UInt16 :
841
856
{
842
- nint num = Runtime . PyLong_AsSignedSize_t ( value ) ;
857
+ op = Runtime . PyNumber_Long ( value ) ;
858
+ if ( op == IntPtr . Zero && Exceptions . ErrorOccurred ( ) )
859
+ {
860
+ goto convert_error ;
861
+ }
862
+ nint num = Runtime . PyLong_AsSignedSize_t ( op ) ;
843
863
if ( num == - 1 && Exceptions . ErrorOccurred ( ) )
844
864
{
845
865
goto convert_error ;
@@ -854,7 +874,12 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
854
874
855
875
case TypeCode . UInt32 :
856
876
{
857
- nuint num = Runtime . PyLong_AsUnsignedSize_t ( value ) ;
877
+ op = Runtime . PyNumber_Long ( value ) ;
878
+ if ( op == IntPtr . Zero && Exceptions . ErrorOccurred ( ) )
879
+ {
880
+ goto convert_error ;
881
+ }
882
+ nuint num = Runtime . PyLong_AsUnsignedSize_t ( op ) ;
858
883
if ( num == unchecked ( ( nuint ) ( - 1 ) ) && Exceptions . ErrorOccurred ( ) )
859
884
{
860
885
goto convert_error ;
@@ -869,7 +894,12 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
869
894
870
895
case TypeCode . UInt64 :
871
896
{
872
- ulong num = Runtime . PyLong_AsUnsignedLongLong ( value ) ;
897
+ op = Runtime . PyNumber_Long ( value ) ;
898
+ if ( op == IntPtr . Zero && Exceptions . ErrorOccurred ( ) )
899
+ {
900
+ goto convert_error ;
901
+ }
902
+ ulong num = Runtime . PyLong_AsUnsignedLongLong ( op ) ;
873
903
if ( num == ulong . MaxValue && Exceptions . ErrorOccurred ( ) )
874
904
{
875
905
goto convert_error ;