From 50e7915fb0dd793f364d3002fcc29d0c2138d9c9 Mon Sep 17 00:00:00 2001 From: dse Date: Sat, 7 Jan 2017 13:36:27 +0400 Subject: [PATCH 1/3] Mono dependency removed from Linux build. --- src/runtime/monosupport.cs | 44 -------------------------------------- src/runtime/runtime.cs | 30 ++++++++++++++++++++------ 2 files changed, 24 insertions(+), 50 deletions(-) delete mode 100644 src/runtime/monosupport.cs diff --git a/src/runtime/monosupport.cs b/src/runtime/monosupport.cs deleted file mode 100644 index fc39f9065..000000000 --- a/src/runtime/monosupport.cs +++ /dev/null @@ -1,44 +0,0 @@ -#if UCS4 -using System; -using System.Runtime.InteropServices; -using System.Text; -using Mono.Unix; - -namespace Python.Runtime -{ - public class Utf32Marshaler : ICustomMarshaler - { - private static Utf32Marshaler instance = new Utf32Marshaler(); - - public static ICustomMarshaler GetInstance(string s) - { - return instance; - } - - public void CleanUpManagedData(object o) - { - } - - public void CleanUpNativeData(IntPtr pNativeData) - { - UnixMarshal.FreeHeap(pNativeData); - } - - public int GetNativeDataSize() - { - return IntPtr.Size; - } - - public IntPtr MarshalManagedToNative(object obj) - { - var s = obj as string; - return s == null ? IntPtr.Zero : UnixMarshal.StringToHeap(s, Encoding.UTF32); - } - - public object MarshalNativeToManaged(IntPtr pNativeData) - { - return UnixMarshal.PtrToString(pNativeData, Encoding.UTF32); - } - } -} -#endif diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index c6f3551df..b5ac02031 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -3,10 +3,6 @@ using System.Security; using System.Text; -#if UCS4 -using Mono.Unix; -#endif - namespace Python.Runtime { [SuppressUnmanagedCodeSecurity()] @@ -1656,10 +1652,32 @@ internal unsafe static extern IntPtr ExactSpelling = true)] internal unsafe static extern IntPtr PyUnicode_FromKindAndString(int kind, - [MarshalAs(UnmanagedType.CustomMarshaler, - MarshalTypeRef = typeof(Utf32Marshaler))] string s, + IntPtr s, int size); + internal static unsafe IntPtr PyUnicode_FromKindAndString(int kind, + string s, + int size) + { + var bufLength = Math.Max(s.Length, size) * 4; + + IntPtr mem = Marshal.AllocHGlobal(bufLength); + try + { + fixed (char* ps = s) + { + Encoding.UTF32.GetBytes(ps, s.Length, (byte*)mem, bufLength); + } + + var result = PyUnicode_FromKindAndString(kind, mem, bufLength); + return result; + } + finally + { + Marshal.FreeHGlobal(mem); + } + } + internal static IntPtr PyUnicode_FromUnicode(string s, int size) { return PyUnicode_FromKindAndString(4, s, size); From 198f6545f5ed4c718fb818679df220e049751f84 Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Sun, 5 Feb 2017 11:37:37 -0700 Subject: [PATCH 2/3] Remove mono dependency from PY2 as well. --- src/runtime/Python.Runtime.csproj | 1 - src/runtime/runtime.cs | 25 ++++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/runtime/Python.Runtime.csproj b/src/runtime/Python.Runtime.csproj index 86b6c0ca9..01cbcb35f 100644 --- a/src/runtime/Python.Runtime.csproj +++ b/src/runtime/Python.Runtime.csproj @@ -116,7 +116,6 @@ - diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index b5ac02031..25a1ea6c1 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -1720,9 +1720,28 @@ internal unsafe static extern IntPtr EntryPoint = "PyUnicodeUCS4_FromUnicode", ExactSpelling = true)] internal unsafe static extern IntPtr - PyUnicode_FromUnicode( - [MarshalAs(UnmanagedType.CustomMarshaler, - MarshalTypeRef = typeof(Utf32Marshaler))] string s, int size); + PyUnicode_FromUnicode(IntPtr s, int size); + + internal static unsafe IntPtr PyUnicode_FromUnicode(string s, int size) + { + var bufLength = Math.Max(s.Length, size) * 4; + + IntPtr mem = Marshal.AllocHGlobal(bufLength); + try + { + fixed (char* ps = s) + { + Encoding.UTF32.GetBytes(ps, s.Length, (byte*)mem, bufLength); + } + + var result = PyUnicode_FromUnicode(mem, bufLength); + return result; + } + finally + { + Marshal.FreeHGlobal(mem); + } + } [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyUnicodeUCS4_GetSize", From d2ba88a10b2a747fc36b502c3a75478909d38df7 Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Mon, 6 Feb 2017 11:30:25 -0700 Subject: [PATCH 3/3] Replace bufLength with size --- src/runtime/runtime.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 25a1ea6c1..a4bb60815 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -1669,7 +1669,7 @@ internal static unsafe IntPtr PyUnicode_FromKindAndString(int kind, Encoding.UTF32.GetBytes(ps, s.Length, (byte*)mem, bufLength); } - var result = PyUnicode_FromKindAndString(kind, mem, bufLength); + var result = PyUnicode_FromKindAndString(kind, mem, size); return result; } finally @@ -1734,7 +1734,7 @@ internal static unsafe IntPtr PyUnicode_FromUnicode(string s, int size) Encoding.UTF32.GetBytes(ps, s.Length, (byte*)mem, bufLength); } - var result = PyUnicode_FromUnicode(mem, bufLength); + var result = PyUnicode_FromUnicode(mem, size); return result; } finally