Skip to content
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Refactor Encoding check
This won't change during runtime.
  • Loading branch information
vmuriart committed Feb 28, 2017
commit 78939c599eecdeccad0384feb4f584fd36171024
4 changes: 2 additions & 2 deletions src/runtime/CustomMarshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public int GetNativeDataSize()
public class StrMarshaler : MarshalerBase
{
private static readonly MarshalerBase Instance = new StrMarshaler();
private static readonly Encoding PyEncoding = Runtime.UCS == 2 ? Encoding.Unicode : Encoding.UTF32;
private static readonly Encoding PyEncoding = Runtime.PyEncoding;

public override IntPtr MarshalManagedToNative(object managedObj)
{
Expand Down Expand Up @@ -82,7 +82,7 @@ public static ICustomMarshaler GetInstance(string cookie)
public class StrArrayMarshaler : MarshalerBase
{
private static readonly MarshalerBase Instance = new StrArrayMarshaler();
private static readonly Encoding PyEncoding = Runtime.UCS == 2 ? Encoding.Unicode : Encoding.UTF32;
private static readonly Encoding PyEncoding = Runtime.PyEncoding;

public override IntPtr MarshalManagedToNative(object managedObj)
{
Expand Down
9 changes: 6 additions & 3 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ public class Runtime
internal static bool IsPython2;
internal static bool IsPython3;

/// <summary>
/// Encoding to use to convert Unicode to/from Managed to Native
/// </summary>
internal static readonly Encoding PyEncoding = UCS == 2 ? Encoding.Unicode : Encoding.UTF32;

/// <summary>
/// Initialize the runtime...
/// </summary>
Expand Down Expand Up @@ -1730,15 +1735,13 @@ internal static string GetManagedString(IntPtr op)

if (type == Runtime.PyUnicodeType)
{
Encoding encoding = UCS == 2 ? Encoding.Unicode : Encoding.UTF32;

IntPtr p = PyUnicode_AsUnicode(op);
int length = PyUnicode_GetSize(op);

int size = length * UCS;
var buffer = new byte[size];
Marshal.Copy(p, buffer, 0, size);
return encoding.GetString(buffer, 0, size);
return PyEncoding.GetString(buffer, 0, size);
}

return null;
Expand Down