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

Skip to content

Commit 9e07d3a

Browse files
authored
Merge branch 'master' into PR/ObjectAcceptsAll
2 parents 6776411 + 60e6045 commit 9e07d3a

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-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

README.rst

Lines changed: 5 additions & 0 deletions
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

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

730743
if (Exceptions.ErrorOccurred())
731744
{

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)