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

Skip to content

Commit 85c85f2

Browse files
committed
Refactor PY2/PY3 Marshal in/out String/Unicode
1 parent 8fba051 commit 85c85f2

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

src/runtime/CustomMarshaler.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,52 @@ public static int GetUnicodeByteLength(IntPtr p)
9595
? Marshal.ReadInt16(p, len * 2)
9696
: Marshal.ReadInt32(p, len * 4);
9797

98-
if (c == 0) return len* Runtime.UCS;
99-
checked{ ++len; }
98+
if (c == 0)
99+
{
100+
return len * Runtime.UCS;
101+
}
102+
checked
103+
{
104+
++len;
105+
}
100106
}
101107
}
108+
109+
/// <summary>
110+
/// Utility function for Marshaling Unicode on PY3 and AnsiStr on PY2.
111+
/// Use on functions whose Input signatures changed between PY2/PY3.
112+
/// Ex. Py_SetPythonHome
113+
/// </summary>
114+
/// <param name="s">Managed String</param>
115+
/// <returns>
116+
/// Ptr to Native String ANSI(PY2)/Unicode(PY3/UCS2)/UTF32(PY3/UCS4.
117+
/// </returns>
118+
/// <remarks>
119+
/// You MUST deallocate the IntPtr of the Return when done with it.
120+
/// </remarks>
121+
public static IntPtr Py3UnicodePy2StringtoPtr(string s)
122+
{
123+
return Runtime.IsPython3
124+
? Instance.MarshalManagedToNative(s)
125+
: Marshal.StringToHGlobalAnsi(s);
126+
}
127+
128+
/// <summary>
129+
/// Utility function for Marshaling Unicode IntPtr on PY3 and
130+
/// AnsiStr IntPtr on PY2 to Managed Strings. Use on Python functions
131+
/// whose return type changed between PY2/PY3.
132+
/// Ex. Py_GetPythonHome
133+
/// </summary>
134+
/// <param name="p">Native Ansi/Unicode/UTF32 String</param>
135+
/// <returns>
136+
/// Managed String
137+
/// </returns>
138+
public static string PtrToPy3UnicodePy2String(IntPtr p)
139+
{
140+
return Runtime.IsPython3
141+
? PtrToStringUni(p)
142+
: Marshal.PtrToStringAnsi(p);
143+
}
102144
}
103145

104146

src/runtime/pythonengine.cs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,12 @@ public static string ProgramName
6161
get
6262
{
6363
IntPtr p = Runtime.Py_GetProgramName();
64-
string result = Runtime.IsPython3
65-
? UcsMarshaler.PtrToStringUni(p)
66-
: Marshal.PtrToStringAnsi(p);
67-
68-
return result ?? "";
64+
return UcsMarshaler.PtrToPy3UnicodePy2String(p) ?? "";
6965
}
7066
set
7167
{
7268
Marshal.FreeHGlobal(_programName);
73-
_programName = Runtime.IsPython3
74-
? UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
75-
: Marshal.StringToHGlobalAnsi(value);
69+
_programName = UcsMarshaler.Py3UnicodePy2StringtoPtr(value);
7670
Runtime.Py_SetProgramName(_programName);
7771
}
7872
}
@@ -82,18 +76,12 @@ public static string PythonHome
8276
get
8377
{
8478
IntPtr p = Runtime.Py_GetPythonHome();
85-
string result = Runtime.IsPython3
86-
? UcsMarshaler.PtrToStringUni(p)
87-
: Marshal.PtrToStringAnsi(p);
88-
89-
return result ?? "";
79+
return UcsMarshaler.PtrToPy3UnicodePy2String(p) ?? "";
9080
}
9181
set
9282
{
9383
Marshal.FreeHGlobal(_pythonHome);
94-
_pythonHome = Runtime.IsPython3
95-
? UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
96-
: Marshal.StringToHGlobalAnsi(value);
84+
_pythonHome = UcsMarshaler.Py3UnicodePy2StringtoPtr(value);
9785
Runtime.Py_SetPythonHome(_pythonHome);
9886
}
9987
}
@@ -103,18 +91,12 @@ public static string PythonPath
10391
get
10492
{
10593
IntPtr p = Runtime.Py_GetPath();
106-
string result = Runtime.IsPython3
107-
? UcsMarshaler.PtrToStringUni(p)
108-
: Marshal.PtrToStringAnsi(p);
109-
110-
return result ?? "";
94+
return UcsMarshaler.PtrToPy3UnicodePy2String(p) ?? "";
11195
}
11296
set
11397
{
11498
Marshal.FreeHGlobal(_pythonPath);
115-
_pythonPath = Runtime.IsPython3
116-
? UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
117-
: Marshal.StringToHGlobalAnsi(value);
99+
_pythonPath = UcsMarshaler.Py3UnicodePy2StringtoPtr(value);
118100
Runtime.Py_SetPath(_pythonPath);
119101
}
120102
}

0 commit comments

Comments
 (0)