Skip to content

Commit d87584b

Browse files
committed
Fix Primitive Conversion to Int
1 parent ed6ab18 commit d87584b

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

src/runtime/converter.cs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,12 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
688688
case TypeCode.Int32:
689689
{
690690
// 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);
692697
if (num == -1 && Exceptions.ErrorOccurred())
693698
{
694699
goto convert_error;
@@ -796,7 +801,12 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
796801

797802
case TypeCode.Int16:
798803
{
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);
800810
if (num == -1 && Exceptions.ErrorOccurred())
801811
{
802812
goto convert_error;
@@ -827,7 +837,12 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
827837
}
828838
else
829839
{
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);
831846
if (num == -1 && Exceptions.ErrorOccurred())
832847
{
833848
goto convert_error;
@@ -839,7 +854,12 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
839854

840855
case TypeCode.UInt16:
841856
{
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);
843863
if (num == -1 && Exceptions.ErrorOccurred())
844864
{
845865
goto convert_error;
@@ -854,7 +874,12 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
854874

855875
case TypeCode.UInt32:
856876
{
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);
858883
if (num == unchecked((nuint)(-1)) && Exceptions.ErrorOccurred())
859884
{
860885
goto convert_error;
@@ -869,7 +894,12 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
869894

870895
case TypeCode.UInt64:
871896
{
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);
873903
if (num == ulong.MaxValue && Exceptions.ErrorOccurred())
874904
{
875905
goto convert_error;

0 commit comments

Comments
 (0)