◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
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`

### 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
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);
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);
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);
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);
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);
if (num == -1 && Exceptions.ErrorOccurred())
{
goto convert_error;
@@ -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())
{
goto convert_error;
}
result = num;
return true;
}

case TypeCode.UInt16:
{
long num = Runtime.PyLong_AsLong(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)
{
uint num = Runtime.PyLong_AsUnsignedLong32(op);
if (num == uint.MaxValue && Exceptions.ErrorOccurred())
{
goto convert_error;
}
result = num;
}
else
{
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;
}
}
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);
}
}
}
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);

[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);

[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):
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")


def test_object_in_param():
"""Test regression introduced by #151 in which Object method overloads
Expand Down
Toggle all file notes Toggle all file annotations