-
Notifications
You must be signed in to change notification settings - Fork 757
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
Detect py arch #961
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2048616
Detect python interpreter arch
jmlidbetter d8040fa
Updates PyLong_FromUnsignedLong to be interpreter arch aware
jmlidbetter c337688
Makes PyLong_AsUnsignedLong interpreter arch aware
jmlidbetter fb1e1ad
Updates CHANGELOG.md
jmlidbetter ac9294d
Gets size of C long from Is32Bit and IsWindows
jmlidbetter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -192,6 +197,9 @@ internal static void Initialize(bool initSigs = false) | |
|
||
IntPtr op; | ||
IntPtr dict; | ||
|
||
InitializePythonArch(); | ||
|
||
if (IsPython3) | ||
{ | ||
op = PyImport_ImportModule("builtins"); | ||
|
@@ -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; | ||
} | ||
jmlidbetter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
internal static void Shutdown() | ||
{ | ||
AssemblyManager.Shutdown(); | ||
|
@@ -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)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is kind of fine, just use the already provided |
||
|
||
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] | ||
internal static extern IntPtr PyLong_FromDouble(double value); | ||
|
@@ -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); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.