Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 78939c5

Browse files
committed
Refactor Encoding check
This won't change during runtime.
1 parent edafdf2 commit 78939c5

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/runtime/CustomMarshaler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public int GetNativeDataSize()
4242
public class StrMarshaler : MarshalerBase
4343
{
4444
private static readonly MarshalerBase Instance = new StrMarshaler();
45-
private static readonly Encoding PyEncoding = Runtime.UCS == 2 ? Encoding.Unicode : Encoding.UTF32;
45+
private static readonly Encoding PyEncoding = Runtime.PyEncoding;
4646

4747
public override IntPtr MarshalManagedToNative(object managedObj)
4848
{
@@ -82,7 +82,7 @@ public static ICustomMarshaler GetInstance(string cookie)
8282
public class StrArrayMarshaler : MarshalerBase
8383
{
8484
private static readonly MarshalerBase Instance = new StrArrayMarshaler();
85-
private static readonly Encoding PyEncoding = Runtime.UCS == 2 ? Encoding.Unicode : Encoding.UTF32;
85+
private static readonly Encoding PyEncoding = Runtime.PyEncoding;
8686

8787
public override IntPtr MarshalManagedToNative(object managedObj)
8888
{

src/runtime/runtime.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ public class Runtime
170170
internal static bool IsPython2;
171171
internal static bool IsPython3;
172172

173+
/// <summary>
174+
/// Encoding to use to convert Unicode to/from Managed to Native
175+
/// </summary>
176+
internal static readonly Encoding PyEncoding = UCS == 2 ? Encoding.Unicode : Encoding.UTF32;
177+
173178
/// <summary>
174179
/// Initialize the runtime...
175180
/// </summary>
@@ -1730,15 +1735,13 @@ internal static string GetManagedString(IntPtr op)
17301735

17311736
if (type == Runtime.PyUnicodeType)
17321737
{
1733-
Encoding encoding = UCS == 2 ? Encoding.Unicode : Encoding.UTF32;
1734-
17351738
IntPtr p = PyUnicode_AsUnicode(op);
17361739
int length = PyUnicode_GetSize(op);
17371740

17381741
int size = length * UCS;
17391742
var buffer = new byte[size];
17401743
Marshal.Copy(p, buffer, 0, size);
1741-
return encoding.GetString(buffer, 0, size);
1744+
return PyEncoding.GetString(buffer, 0, size);
17421745
}
17431746

17441747
return null;

0 commit comments

Comments
 (0)