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
Changes from 1 commit
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
Next Next commit
Detect python interpreter arch
  • Loading branch information
jmlidbetter committed Sep 25, 2019
commit 2048616678e171f121935ccd48e7c71c377032be
45 changes: 45 additions & 0 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