This repository was archived by the owner on Jul 22, 2023. It is now read-only.
forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomMarshaler.cs
More file actions
236 lines (203 loc) · 6.75 KB
/
CustomMarshaler.cs
File metadata and controls
236 lines (203 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace Python.Runtime
{
/// <summary>
/// Abstract class defining boiler plate methods that
/// Custom Marshalers will use.
/// </summary>
internal abstract class MarshalerBase : ICustomMarshaler
{
public object MarshalNativeToManaged(IntPtr pNativeData)
{
throw new NotImplementedException();
}
public abstract IntPtr MarshalManagedToNative(object managedObj);
public void CleanUpNativeData(IntPtr pNativeData)
{
Marshal.FreeHGlobal(pNativeData);
}
public void CleanUpManagedData(object managedObj)
{
// Let GC deal with it
}
public int GetNativeDataSize()
{
return IntPtr.Size;
}
}
/// <summary>
/// Custom Marshaler to deal with Managed String to Native
/// conversion differences on UCS2/UCS4.
/// </summary>
internal class UcsMarshaler : MarshalerBase
{
private static readonly MarshalerBase Instance = new UcsMarshaler();
private static readonly Encoding PyEncoding = Runtime.PyEncoding;
public override IntPtr MarshalManagedToNative(object managedObj)
{
var s = managedObj as string;
if (s == null)
{
return IntPtr.Zero;
}
byte[] bStr = PyEncoding.GetBytes(s + "\0");
IntPtr mem = Marshal.AllocHGlobal(bStr.Length);
try
{
Marshal.Copy(bStr, 0, mem, bStr.Length);
}
catch (Exception)
{
Marshal.FreeHGlobal(mem);
throw;
}
return mem;
}
public static ICustomMarshaler GetInstance(string cookie)
{
return Instance;
}
public static string PtrToStringUni(IntPtr p)
{
if (p == IntPtr.Zero)
{
return null;
}
int size = GetUnicodeByteLength(p);
var buffer = new byte[size];
Marshal.Copy(p, buffer, 0, size);
return PyEncoding.GetString(buffer, 0, size);
}
public static int GetUnicodeByteLength(IntPtr p)
{
var len = 0;
while (true)
{
int c = Runtime._UCS == 2
? Marshal.ReadInt16(p, len * 2)
: Marshal.ReadInt32(p, len * 4);
if (c == 0)
{
return len * Runtime._UCS;
}
checked
{
++len;
}
}
}
/// <summary>
/// Utility function for Marshaling Unicode on PY3 and AnsiStr on PY2.
/// Use on functions whose Input signatures changed between PY2/PY3.
/// Ex. Py_SetPythonHome
/// </summary>
/// <param name="s">Managed String</param>
/// <returns>
/// Ptr to Native String ANSI(PY2)/Unicode(PY3/UCS2)/UTF32(PY3/UCS4.
/// </returns>
/// <remarks>
/// You MUST deallocate the IntPtr of the Return when done with it.
/// </remarks>
public static IntPtr Py3UnicodePy2StringtoPtr(string s)
{
return Instance.MarshalManagedToNative(s);
}
/// <summary>
/// Utility function for Marshaling Unicode IntPtr on PY3 and
/// AnsiStr IntPtr on PY2 to Managed Strings. Use on Python functions
/// whose return type changed between PY2/PY3.
/// Ex. Py_GetPythonHome
/// </summary>
/// <param name="p">Native Ansi/Unicode/UTF32 String</param>
/// <returns>
/// Managed String
/// </returns>
public static string PtrToPy3UnicodePy2String(IntPtr p)
{
return PtrToStringUni(p);
}
}
/// <summary>
/// Custom Marshaler to deal with Managed String Arrays to Native
/// conversion differences on UCS2/UCS4.
/// </summary>
internal class StrArrayMarshaler : MarshalerBase
{
private static readonly MarshalerBase Instance = new StrArrayMarshaler();
private static readonly Encoding PyEncoding = Runtime.PyEncoding;
public override IntPtr MarshalManagedToNative(object managedObj)
{
var argv = managedObj as string[];
if (argv == null)
{
return IntPtr.Zero;
}
int totalStrLength = argv.Sum(arg => arg.Length + 1);
int memSize = argv.Length * IntPtr.Size + totalStrLength * Runtime._UCS;
IntPtr mem = Marshal.AllocHGlobal(memSize);
try
{
// Preparing array of pointers to strings
IntPtr curStrPtr = mem + argv.Length * IntPtr.Size;
for (var i = 0; i < argv.Length; i++)
{
byte[] bStr = PyEncoding.GetBytes(argv[i] + "\0");
Marshal.Copy(bStr, 0, curStrPtr, bStr.Length);
Marshal.WriteIntPtr(mem + i * IntPtr.Size, curStrPtr);
curStrPtr += bStr.Length;
}
}
catch (Exception)
{
Marshal.FreeHGlobal(mem);
throw;
}
return mem;
}
public static ICustomMarshaler GetInstance(string cookie)
{
return Instance;
}
}
/// <summary>
/// Custom Marshaler to deal with Managed String to Native
/// conversion on UTF-8. Use on functions that expect UTF-8 encoded
/// strings like `PyUnicode_FromStringAndSize`
/// </summary>
/// <remarks>
/// If instead we used `MarshalAs(UnmanagedType.LPWStr)` the output to
/// `foo` would be `f\x00o\x00o\x00`.
/// </remarks>
internal class Utf8Marshaler : MarshalerBase
{
private static readonly MarshalerBase Instance = new Utf8Marshaler();
private static readonly Encoding PyEncoding = Encoding.UTF8;
public override IntPtr MarshalManagedToNative(object managedObj)
{
var s = managedObj as string;
if (s == null)
{
return IntPtr.Zero;
}
byte[] bStr = PyEncoding.GetBytes(s + "\0");
IntPtr mem = Marshal.AllocHGlobal(bStr.Length);
try
{
Marshal.Copy(bStr, 0, mem, bStr.Length);
}
catch (Exception)
{
Marshal.FreeHGlobal(mem);
throw;
}
return mem;
}
public static ICustomMarshaler GetInstance(string cookie)
{
return Instance;
}
}
}