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

Skip to content

Detect py arch #961

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 5 commits into from
Oct 1, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Fixed runtime that fails loading when using pythonnet in an environment
together with Nuitka
- Fixes bug where delegates get casts (dotnetcore)
- Determine size of interpreter longs at runtime

## [2.4.0][]

Expand Down
15 changes: 14 additions & 1 deletion src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,20 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
}
goto type_error;
}
uint ui = (uint)Runtime.PyLong_AsUnsignedLong(op);

uint ui;
try
{
ui = Convert.ToUInt32(Runtime.PyLong_AsUnsignedLong(op));
} 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)
Runtime.XDecref(op);
goto overflow;
}


if (Exceptions.ErrorOccurred())
{
Expand Down
34 changes: 30 additions & 4 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,8 +1036,21 @@ internal static bool PyLong_Check(IntPtr ob)
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyLong_FromLong(long value);

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

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "PyLong_FromUnsignedLong")]
internal static extern IntPtr PyLong_FromUnsignedLong64(ulong value);

internal static IntPtr PyLong_FromUnsignedLong(object value)
{
if(Is32Bit || IsWindows)
return PyLong_FromUnsignedLong32(Convert.ToUInt32(value));
else
return PyLong_FromUnsignedLong64(Convert.ToUInt64(value));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of fine, just use the already provided Runtime.Is32Bit and Runtime.IsWindows instead.


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

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern uint PyLong_AsUnsignedLong(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(IntPtr value);
Expand Down