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/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..a4bb60815 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, size); + return result; + } + finally + { + Marshal.FreeHGlobal(mem); + } + } + internal static IntPtr PyUnicode_FromUnicode(string s, int size) { return PyUnicode_FromKindAndString(4, s, size); @@ -1702,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, size); + return result; + } + finally + { + Marshal.FreeHGlobal(mem); + } + } [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyUnicodeUCS4_GetSize",