Skip to content

Disable implicit conversion from PyFloat to .NET integer types #1343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ details about the cause of the failure
to the regular method return value (unless they are passed with `ref` or `out` keyword).
- BREAKING: Drop support for the long-deprecated CLR.* prefix.
- `PyObject` now implements `IEnumerable<PyObject>` in addition to `IEnumerable`
- floating point values passed from Python are no longer silently truncated
when .NET expects an integer [#1342][i1342]

### Fixed

Expand Down Expand Up @@ -807,3 +809,4 @@ This version improves performance on benchmarks significantly compared to 2.3.
[i755]: https://github.com/pythonnet/pythonnet/pull/755
[p534]: https://github.com/pythonnet/pythonnet/pull/534
[i449]: https://github.com/pythonnet/pythonnet/issues/449
[i1342]: https://github.com/pythonnet/pythonnet/issues/1342
1 change: 1 addition & 0 deletions src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<Platforms>AnyCPU</Platforms>
<RootNamespace>Python.Runtime</RootNamespace>
<AssemblyName>Python.Runtime</AssemblyName>
<LangVersion>9.0</LangVersion>
<PackageId>pythonnet</PackageId>
<PackageLicenseUrl>https://github.com/pythonnet/pythonnet/blob/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/pythonnet/pythonnet</RepositoryUrl>
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/arrayobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static IntPtr tp_new(IntPtr tpRaw, IntPtr args, IntPtr kw)
// create single dimensional array
if (Runtime.PyInt_Check(op))
{
dimensions[0] = Runtime.PyLong_AsLongLong(op);
dimensions[0] = Runtime.PyLong_AsSignedSize_t(op);
if (dimensions[0] == -1 && Exceptions.ErrorOccurred())
{
Exceptions.Clear();
Expand Down Expand Up @@ -84,7 +84,7 @@ static NewReference CreateMultidimensional(Type elementType, long[] dimensions,
return default;
}

dimensions[dimIndex] = Runtime.PyLong_AsLongLong(dimObj);
dimensions[dimIndex] = Runtime.PyLong_AsSignedSize_t(dimObj);
if (dimensions[dimIndex] == -1 && Exceptions.ErrorOccurred())
{
Exceptions.RaiseTypeError("array constructor expects integer dimensions");
Expand Down
78 changes: 34 additions & 44 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion src/runtime/pylong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public int ToInt32()
/// </remarks>
public long ToInt64()
{
return Runtime.PyLong_AsLongLong(obj);
return Runtime.PyExplicitlyConvertToInt64(obj);
}
}
}
39 changes: 18 additions & 21 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1251,30 +1251,27 @@ internal static IntPtr PyLong_FromUnsignedLong(object value)
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyLong_FromString(string value, IntPtr end, int radix);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern int PyLong_AsLong(IntPtr value);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "PyLong_AsUnsignedLong")]
internal static extern uint PyLong_AsUnsignedLong32(IntPtr value);

EntryPoint = "PyLong_AsSize_t")]
internal static extern nuint PyLong_AsUnsignedSize_t(IntPtr value);
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "PyLong_AsUnsignedLong")]
internal static extern ulong PyLong_AsUnsignedLong64(IntPtr value);

internal static object PyLong_AsUnsignedLong(IntPtr value)
{
if (Is32Bit || IsWindows)
return PyLong_AsUnsignedLong32(value);
else
return PyLong_AsUnsignedLong64(value);
}

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern long PyLong_AsLongLong(BorrowedReference value);
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern long PyLong_AsLongLong(IntPtr value);
EntryPoint = "PyLong_AsSsize_t")]
internal static extern nint PyLong_AsSignedSize_t(IntPtr value);
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "PyLong_AsSsize_t")]
internal static extern nint PyLong_AsSignedSize_t(BorrowedReference value);

/// <summary>
/// This function is a rename of PyLong_AsLongLong, which has a commonly undesired
/// behavior to convert everything (including floats) to integer type, before returning
/// the value as <see cref="Int64"/>.
///
/// <para>In most cases you need to check that value is an instance of PyLongObject
/// before using this function using <see cref="PyLong_Check(IntPtr)"/>.</para>
/// </summary>
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "PyLong_AsLongLong")]
internal static extern long PyExplicitlyConvertToInt64(IntPtr value);
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern ulong PyLong_AsUnsignedLongLong(IntPtr value);

Expand Down
2 changes: 1 addition & 1 deletion src/tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def test_uint32_conversion():
ob.UInt32Field = System.UInt32(0)
assert ob.UInt32Field == 0

with pytest.raises(ValueError):
with pytest.raises(TypeError):
ConversionTest().UInt32Field = "spam"

with pytest.raises(TypeError):
Expand Down
3 changes: 3 additions & 0 deletions src/tests/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,9 @@ def test_no_object_in_param():
with pytest.raises(TypeError):
MethodTest.TestOverloadedNoObject("test")

with pytest.raises(TypeError):
MethodTest.TestOverloadedNoObject(5.5)


def test_object_in_param():
"""Test regression introduced by #151 in which Object method overloads
Expand Down