Skip to content

Handle C long size on Windows. #497

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 1 commit into from
Jun 26, 2017
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
3 changes: 2 additions & 1 deletion src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<Compile Include="runtime.cs" />
<Compile Include="typemanager.cs" />
<Compile Include="typemethod.cs" />
<Compile Include="Util.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(PythonInteropFile)' != '' ">
<Compile Include="$(PythonInteropFile)" />
Expand Down Expand Up @@ -164,4 +165,4 @@
<Copy SourceFiles="$(TargetAssembly)" DestinationFolder="$(PythonBuildDir)" />
<!--Copy SourceFiles="$(TargetAssemblyPdb)" Condition="Exists('$(TargetAssemblyPdb)')" DestinationFolder="$(PythonBuildDir)" /-->
</Target>
</Project>
</Project>
33 changes: 33 additions & 0 deletions src/runtime/Util.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Runtime.InteropServices;

namespace Python.Runtime
{
internal class Util
{
internal static Int64 ReadCLong(IntPtr tp, int offset)
{
// On Windows, a C long is always 32 bits.
if (Runtime.IsWindows || Runtime.Is32Bit)
{
return Marshal.ReadInt32(tp, offset);
}
else
{
return Marshal.ReadInt64(tp, offset);
}
}

internal static void WriteCLong(IntPtr type, int offset, Int64 flags)
{
if (Runtime.IsWindows || Runtime.Is32Bit)
{
Marshal.WriteInt32(type, offset, (Int32)(flags & 0xffffffffL));
}
else
{
Marshal.WriteInt64(type, offset, flags);
}
}
}
}
4 changes: 2 additions & 2 deletions src/runtime/clrobject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Runtime.InteropServices;

namespace Python.Runtime
Expand All @@ -11,7 +11,7 @@ internal CLRObject(object ob, IntPtr tp)
{
IntPtr py = Runtime.PyType_GenericAlloc(tp, 0);

var flags = (int)Marshal.ReadIntPtr(tp, TypeOffset.tp_flags);
long flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
if ((flags & TypeFlags.Subclass) != 0)
{
IntPtr dict = Marshal.ReadIntPtr(py, ObjectOffset.DictOffset(tp));
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/managedtype.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Runtime.InteropServices;

namespace Python.Runtime
Expand Down Expand Up @@ -28,7 +28,7 @@ internal static ManagedType GetManagedObject(IntPtr ob)
tp = ob;
}

var flags = (int)Marshal.ReadIntPtr(tp, TypeOffset.tp_flags);
var flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
if ((flags & TypeFlags.Managed) != 0)
{
IntPtr op = tp == ob
Expand Down Expand Up @@ -63,7 +63,7 @@ internal static bool IsManagedType(IntPtr ob)
tp = ob;
}

var flags = (int)Marshal.ReadIntPtr(tp, TypeOffset.tp_flags);
var flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
if ((flags & TypeFlags.Managed) != 0)
{
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/metatype.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Runtime.InteropServices;

namespace Python.Runtime
Expand Down Expand Up @@ -105,7 +105,7 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
flags |= TypeFlags.BaseType;
flags |= TypeFlags.Subclass;
flags |= TypeFlags.HaveGC;
Marshal.WriteIntPtr(type, TypeOffset.tp_flags, (IntPtr)flags);
Util.WriteCLong(type, TypeOffset.tp_flags, flags);

TypeManager.CopySlot(base_type, type, TypeOffset.tp_dealloc);

Expand Down Expand Up @@ -247,7 +247,7 @@ public static void tp_dealloc(IntPtr tp)
{
// Fix this when we dont cheat on the handle for subclasses!

var flags = (int)Marshal.ReadIntPtr(tp, TypeOffset.tp_flags);
var flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
if ((flags & TypeFlags.Subclass) == 0)
{
IntPtr gc = Marshal.ReadIntPtr(tp, TypeOffset.magic());
Expand Down
8 changes: 6 additions & 2 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ public class Runtime
internal static bool IsFinalizing;

internal static bool Is32Bit = IntPtr.Size == 4;

// .NET core: System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
internal static bool IsWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;

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

Expand Down Expand Up @@ -785,7 +789,7 @@ internal static bool PyObject_IsIterable(IntPtr pointer)
{
var ob_type = Marshal.ReadIntPtr(pointer, ObjectOffset.ob_type);
#if PYTHON2
long tp_flags = Marshal.ReadInt64(ob_type, TypeOffset.tp_flags);
long tp_flags = Util.ReadCLong(ob_type, TypeOffset.tp_flags);
if ((tp_flags & TypeFlags.HaveIter) == 0)
return false;
#endif
Expand Down Expand Up @@ -1442,7 +1446,7 @@ internal static bool PyIter_Check(IntPtr pointer)
{
var ob_type = Marshal.ReadIntPtr(pointer, ObjectOffset.ob_type);
#if PYTHON2
long tp_flags = Marshal.ReadInt64(ob_type, TypeOffset.tp_flags);
long tp_flags = Util.ReadCLong(ob_type, TypeOffset.tp_flags);
if ((tp_flags & TypeFlags.HaveIter) == 0)
return false;
#endif
Expand Down
10 changes: 5 additions & 5 deletions src/runtime/typemanager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
Expand Down Expand Up @@ -86,7 +86,7 @@ internal static IntPtr CreateType(Type impl)

int flags = TypeFlags.Default | TypeFlags.Managed |
TypeFlags.HeapType | TypeFlags.HaveGC;
Marshal.WriteIntPtr(type, TypeOffset.tp_flags, (IntPtr)flags);
Util.WriteCLong(type, TypeOffset.tp_flags, flags);

Runtime.PyType_Ready(type);

Expand Down Expand Up @@ -160,7 +160,7 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType)
flags |= TypeFlags.HeapType;
flags |= TypeFlags.BaseType;
flags |= TypeFlags.HaveGC;
Marshal.WriteIntPtr(type, TypeOffset.tp_flags, (IntPtr)flags);
Util.WriteCLong(type, TypeOffset.tp_flags, flags);

// Leverage followup initialization from the Python runtime. Note
// that the type of the new type must PyType_Type at the time we
Expand Down Expand Up @@ -323,7 +323,7 @@ internal static IntPtr CreateMetaType(Type impl)
flags |= TypeFlags.Managed;
flags |= TypeFlags.HeapType;
flags |= TypeFlags.HaveGC;
Marshal.WriteIntPtr(type, TypeOffset.tp_flags, (IntPtr)flags);
Util.WriteCLong(type, TypeOffset.tp_flags, flags);

// We need space for 3 PyMethodDef structs, each of them
// 4 int-ptrs in size.
Expand Down Expand Up @@ -380,7 +380,7 @@ internal static IntPtr BasicSubType(string name, IntPtr base_, Type impl)
flags |= TypeFlags.Managed;
flags |= TypeFlags.HeapType;
flags |= TypeFlags.HaveGC;
Marshal.WriteIntPtr(type, TypeOffset.tp_flags, (IntPtr)flags);
Util.WriteCLong(type, TypeOffset.tp_flags, flags);

CopySlot(base_, type, TypeOffset.tp_traverse);
CopySlot(base_, type, TypeOffset.tp_clear);
Expand Down