◐ Shell
clean mode source ↗

Disable implicit conversion from PyFloat to .NET integer types by lostmsu · Pull Request #1343 · pythonnet/pythonnet

Expand Up @@ -511,7 +511,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo case TypeCode.Int32: { // Python3 always use PyLong API long num = Runtime.PyLong_AsLongLong(value); nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; Expand Down Expand Up @@ -541,7 +541,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo goto type_error; }
int num = Runtime.PyLong_AsLong(value); nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; Expand All @@ -567,7 +567,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo goto type_error; }
int num = Runtime.PyLong_AsLong(value); nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; Expand Down Expand Up @@ -604,7 +604,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo } goto type_error; } int num = Runtime.PyLong_AsLong(value); nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; Expand All @@ -619,7 +619,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
case TypeCode.Int16: { int num = Runtime.PyLong_AsLong(value); nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; Expand All @@ -634,18 +634,35 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
case TypeCode.Int64: { long num = (long)Runtime.PyLong_AsLongLong(value); if (num == -1 && Exceptions.ErrorOccurred()) if (Runtime.Is32Bit) { goto convert_error; if (!Runtime.PyLong_Check(value)) { goto type_error; } long num = Runtime.PyExplicitlyConvertToInt64(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; } result = num; return true; } else { nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; } result = (long)num; return true; } result = num; return true; }
case TypeCode.UInt16: { long num = Runtime.PyLong_AsLong(value); nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; Expand All @@ -660,43 +677,16 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
case TypeCode.UInt32: { op = value; if (Runtime.PyObject_TYPE(value) != Runtime.PyLongType) { op = Runtime.PyNumber_Long(value); if (op == IntPtr.Zero) { goto convert_error; } } if (Runtime.Is32Bit || Runtime.IsWindows) nuint num = Runtime.PyLong_AsUnsignedSize_t(value); if (num == unchecked((nuint)(-1)) && Exceptions.ErrorOccurred()) { uint num = Runtime.PyLong_AsUnsignedLong32(op); if (num == uint.MaxValue && Exceptions.ErrorOccurred()) { goto convert_error; } result = num; goto convert_error; } else if (num > UInt32.MaxValue) { ulong num = Runtime.PyLong_AsUnsignedLong64(op); if (num == ulong.MaxValue && Exceptions.ErrorOccurred()) { goto convert_error; } try { result = Convert.ToUInt32(num); } catch (OverflowException) { // Probably wasn't an overflow in python but was in C# (e.g. if cpython // longs are 64 bit then 0xFFFFFFFF + 1 will not overflow in // PyLong_AsUnsignedLong) goto overflow; } goto overflow; } result = (uint)num; return true; }
Expand Down