Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60e6045

Browse files
jmlidbetterfilmor
authored andcommitted
Detect py arch (#961)
* Gets size of C long from Is32Bit and IsWindows
1 parent 46c7597 commit 60e6045

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
2525
- Fixed runtime that fails loading when using pythonnet in an environment
2626
together with Nuitka
2727
- Fixes bug where delegates get casts (dotnetcore)
28+
- Determine size of interpreter longs at runtime
2829

2930
## [2.4.0][]
3031

src/runtime/converter.cs

Lines changed: 14 additions & 1 deletion
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

Lines changed: 30 additions & 4 deletions
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)