3
3
using System . Runtime . InteropServices ;
4
4
using System . Text ;
5
5
6
- namespace Python . Runtime
6
+ namespace Python . Runtime . Native
7
7
{
8
8
/// <summary>
9
9
/// Abstract class defining boiler plate methods that
10
10
/// Custom Marshalers will use.
11
11
/// </summary>
12
12
internal abstract class MarshalerBase : ICustomMarshaler
13
13
{
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
+
14
22
public object MarshalNativeToManaged ( IntPtr pNativeData )
15
23
{
16
24
throw new NotImplementedException ( ) ;
@@ -42,7 +50,6 @@ public int GetNativeDataSize()
42
50
internal class UcsMarshaler : MarshalerBase
43
51
{
44
52
private static readonly MarshalerBase Instance = new UcsMarshaler ( ) ;
45
- private static readonly Encoding PyEncoding = Runtime . PyEncoding ;
46
53
47
54
public override IntPtr MarshalManagedToNative ( object managedObj )
48
55
{
@@ -91,15 +98,15 @@ public static int GetUnicodeByteLength(IntPtr p)
91
98
var len = 0 ;
92
99
while ( true )
93
100
{
94
- #if UCS2
101
+ #if UCS2 && PYTHON2
95
102
int c = Marshal . ReadInt16 ( p , len * 2 ) ;
96
103
#else
97
104
int c = Marshal . ReadInt32 ( p , len * 4 ) ;
98
105
#endif
99
106
100
107
if ( c == 0 )
101
108
{
102
- return len * Runtime . _UCS ;
109
+ return len * UCS ;
103
110
}
104
111
checked
105
112
{
@@ -157,7 +164,6 @@ public static string PtrToPy3UnicodePy2String(IntPtr p)
157
164
internal class StrArrayMarshaler : MarshalerBase
158
165
{
159
166
private static readonly MarshalerBase Instance = new StrArrayMarshaler ( ) ;
160
- private static readonly Encoding PyEncoding = Runtime . PyEncoding ;
161
167
162
168
public override IntPtr MarshalManagedToNative ( object managedObj )
163
169
{
@@ -169,7 +175,7 @@ public override IntPtr MarshalManagedToNative(object managedObj)
169
175
}
170
176
171
177
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 ;
173
179
174
180
IntPtr mem = Marshal . AllocHGlobal ( memSize ) ;
175
181
try
0 commit comments