8000 Detect py arch by jmlidbetter · Pull Request #961 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

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 4 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
86 changes: 82 additions & 4 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ public class Runtime
/// </summary>
public static string MachineName { get; private set; }

/// <summary>
/// Gets the architecture the python interpreter is using as reported by python's struct.calcsize("P")
/// </summary>
public static MachineType PythonArchitecture { get; private set; }

internal static bool IsPython2 = pyversionnumber < 30;
internal static bool IsPython3 = pyversionnumber >= 30;

Expand Down Expand Up @@ -192,6 +197,9 @@ internal static void Initialize(bool initSigs = false)

IntPtr op;
IntPtr dict;

InitializePythonArch();

if (IsPython3)
{
op = PyImport_ImportModule("builtins");
Expand Down Expand Up @@ -371,6 +379,43 @@ private static void InitializePlatformData()
Machine = MType;
}

/// <summary>
/// Initializes the architecture used within the python interpreter
///
/// For various reasons the python interpreter often has a different
/// architecture to that of the machine. For example on a 64 bit Windows
/// platform the CPython interpreter is compiled with 32 bit longs.
/// This method will allow pythonnet to determine at runtime how big
/// python's longs are.
/// </summary>
private static void InitializePythonArch()
{
IntPtr structModule = PyImport_ImportModule("struct");
IntPtr calcsizeMethod = PyObject_GetAttrString(structModule, "calcsize");
IntPtr methodArgs = PyTuple_New(1);
IntPtr pString = PyString_FromString("P");

if(PyTuple_SetItem(methodArgs, 0, pString) != 0)
{
PythonArchitecture = MachineType.Other;
return;
}

var result = PyLong_AsLong(PyObject_Call(calcsizeMethod, methodArgs, IntPtr.Zero));

switch(result){
case 4:
PythonArchitecture = MachineType.i386;
break;
case 8:
PythonArchitecture = MachineType.x86_64;
break;
default:
PythonArchitecture = MachineType.Other;
break;
}
}

internal static void Shutdown()
{
AssemblyManager.Shutdown();
Expand Down Expand Up @@ -1036,8 +1081,24 @@ 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(PythonArchitecture == MachineType.i386)
return PyLong_FromUnsignedLong32(Convert.ToUInt32(value));
else if(PythonArchitecture == MachineType.x86_64)
return PyLong_FromUnsignedLong64(Convert.ToUInt64(value));
else
// Couldn't determine interpreter arch, try 64 bit
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 +1115,25 @@ 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 (PythonArchitecture == MachineType.i386)
return PyLong_AsUnsignedLong32(value);
else if (PythonArchitecture == MachineType.x86_64)
return PyLong_AsUnsignedLong64(value);
else
return PyLong_AsUnsignedLong64(value);
}



[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern long PyLong_AsLongLong(IntPtr value);
Expand Down
0