33using System . Runtime . InteropServices ;
44using System . Text ;
55
6- namespace Python . Runtime
6+ namespace Python . Runtime . Native
77{
88 /// <summary>
99 /// Abstract class defining boiler plate methods that
1010 /// Custom Marshalers will use.
1111 /// </summary>
1212 internal abstract class MarshalerBase : ICustomMarshaler
1313 {
14+ #if UCS2 && PYTHON2
15+ internal static Encoding PyEncoding = Encoding . Unicode ;
16+ internal static int UCS = 2 ;
17+ #else
18+ internal static Encoding PyEncoding = Encoding . UTF32 ;
19+ internal static int UCS = 4 ;
20+ #endif
21+
1422 public object MarshalNativeToManaged ( IntPtr pNativeData )
1523 {
1624 throw new NotImplementedException ( ) ;
@@ -42,7 +50,6 @@ public int GetNativeDataSize()
4250 internal class UcsMarshaler : MarshalerBase
4351 {
4452 private static readonly MarshalerBase Instance = new UcsMarshaler ( ) ;
45- private static readonly Encoding PyEncoding = Runtime . PyEncoding ;
4653
4754 public override IntPtr MarshalManagedToNative ( object managedObj )
4855 {
@@ -91,15 +98,15 @@ public static int GetUnicodeByteLength(IntPtr p)
9198 var len = 0 ;
9299 while ( true )
93100 {
94- #if UCS2
101+ #if UCS2 && PYTHON2
95102 int c = Marshal . ReadInt16 ( p , len * 2 ) ;
96103#else
97104 int c = Marshal . ReadInt32 ( p , len * 4 ) ;
98105#endif
99106
100107 if ( c == 0 )
101108 {
102- return len * Runtime . _UCS ;
109+ return len * UCS ;
103110 }
104111 checked
105112 {
@@ -157,7 +164,6 @@ public static string PtrToPy3UnicodePy2String(IntPtr p)
157164 internal class StrArrayMarshaler : MarshalerBase
158165 {
159166 private static readonly MarshalerBase Instance = new StrArrayMarshaler ( ) ;
160- private static readonly Encoding PyEncoding = Runtime . PyEncoding ;
161167
162168 public override IntPtr MarshalManagedToNative ( object managedObj )
163169 {
@@ -169,7 +175,7 @@ public override IntPtr MarshalManagedToNative(object managedObj)
169175 }
170176
171177 int totalStrLength = argv . Sum ( arg => arg . Length + 1 ) ;
172- int memSize = argv . Length * IntPtr . Size + totalStrLength * Runtime . _UCS ;
178+ int memSize = argv . Length * IntPtr . Size + totalStrLength * UCS ;
173179
174180 IntPtr mem = Marshal . AllocHGlobal ( memSize ) ;
175181 try
0 commit comments