Skip to content

Commit d0d7545

Browse files
authored
Merge branch 'master' into losttech-perf-interop
2 parents bc8a992 + 60e6045 commit d0d7545

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
2626
- Fixed runtime that fails loading when using pythonnet in an environment
2727
together with Nuitka
2828
- Fixes bug where delegates get casts (dotnetcore)
29+
- Determine size of interpreter longs at runtime
2930

3031
## [2.4.0][]
3132

README.rst

+5
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,8 @@ https://github.com/pythonnet/pythonnet/wiki
113113
:target: http://stackoverflow.com/questions/tagged/python.net
114114
.. |conda-forge version| image:: https://img.shields.io/conda/vn/conda-forge/pythonnet.svg
115115
:target: https://anaconda.org/conda-forge/pythonnet
116+
117+
Resources
118+
---------
119+
Mailing list: https://mail.python.org/mailman/listinfo/pythondotnet
120+
Chat: https://gitter.im/pythonnet/pythonnet

src/runtime/converter.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,20 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
728728
}
729729
goto type_error;
730730
}
731-
uint ui = (uint)Runtime.PyLong_AsUnsignedLong(op);
731+
732+
uint ui;
733+
try
734+
{
735+
ui = Convert.ToUInt32(Runtime.PyLong_AsUnsignedLong(op));
736+
} catch (OverflowException)
737+
{
738+
// Probably wasn't an overflow in python but was in C# (e.g. if cpython
739+
// longs are 64 bit then 0xFFFFFFFF + 1 will not overflow in
740+
// PyLong_AsUnsignedLong)
741+
Runtime.XDecref(op);
742+
goto overflow;
743+
}
744+
732745

733746
if (Exceptions.ErrorOccurred())
734747
{

src/runtime/runtime.cs

+30-4
Original file line numberDiff line numberDiff line change
@@ -1036,8 +1036,21 @@ internal static bool PyLong_Check(IntPtr ob)
10361036
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
10371037
internal static extern IntPtr PyLong_FromLong(long value);
10381038

1039-
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1040-
internal static extern IntPtr PyLong_FromUnsignedLong(uint value);
1039+
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
1040+
EntryPoint = "PyLong_FromUnsignedLong")]
1041+
internal static extern IntPtr PyLong_FromUnsignedLong32(uint value);
1042+
1043+
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
1044+
EntryPoint = "PyLong_FromUnsignedLong")]
1045+
internal static extern IntPtr PyLong_FromUnsignedLong64(ulong value);
1046+
1047+
internal static IntPtr PyLong_FromUnsignedLong(object value)
1048+
{
1049+
if(Is32Bit || IsWindows)
1050+
return PyLong_FromUnsignedLong32(Convert.ToUInt32(value));
1051+
else
1052+
return PyLong_FromUnsignedLong64(Convert.ToUInt64(value));
1053+
}
10411054

10421055
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
10431056
internal static extern IntPtr PyLong_FromDouble(double value);
@@ -1054,8 +1067,21 @@ internal static bool PyLong_Check(IntPtr ob)
10541067
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
10551068
internal static extern int PyLong_AsLong(IntPtr value);
10561069

1057-
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1058-
internal static extern uint PyLong_AsUnsignedLong(IntPtr value);
1070+
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
1071+
EntryPoint = "PyLong_AsUnsignedLong")]
1072+
internal static extern uint PyLong_AsUnsignedLong32(IntPtr value);
1073+
1074+
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
1075+
EntryPoint = "PyLong_AsUnsignedLong")]
1076+
internal static extern ulong PyLong_AsUnsignedLong64(IntPtr value);
1077+
1078+
internal static object PyLong_AsUnsignedLong(IntPtr value)
1079+
{
1080+
if (Is32Bit || IsWindows)
1081+
return PyLong_AsUnsignedLong32(value);
1082+
else
1083+
return PyLong_AsUnsignedLong64(value);
1084+
}
10591085

10601086
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
10611087
internal static extern long PyLong_AsLongLong(IntPtr value);

0 commit comments

Comments
 (0)