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

Skip to content

Commit a0f4c33

Browse files
committed
fixed bug of method PyString_FromString
1 parent 19d854c commit a0f4c33

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/runtime/runtime.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,26 @@ internal static bool PyString_Check(IntPtr ob)
11931193

11941194
internal static IntPtr PyString_FromString(string value)
11951195
{
1196-
return PyString_FromStringAndSize(value, value.Length);
1196+
if (value == null)
1197+
{
1198+
return IntPtr.Zero;
1199+
}
1200+
1201+
byte[] bStr = Encoding.UTF8.GetBytes(value);
1202+
IntPtr mem = Marshal.AllocHGlobal(bStr.Length);
1203+
try
1204+
{
1205+
Marshal.Copy(bStr, 0, mem, bStr.Length);
1206+
}
1207+
catch (Exception)
1208+
{
1209+
Marshal.FreeHGlobal(mem);
1210+
throw;
1211+
}
1212+
1213+
IntPtr result = PyString_FromStringAndSize(mem, bStr.Length);
1214+
Marshal.FreeHGlobal(mem);
1215+
return result;
11971216
}
11981217

11991218
#if PYTHON3
@@ -1211,7 +1230,7 @@ internal static IntPtr PyBytes_AS_STRING(IntPtr ob)
12111230
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
12121231
EntryPoint = "PyUnicode_FromStringAndSize")]
12131232
internal static extern IntPtr PyString_FromStringAndSize(
1214-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string value,
1233+
IntPtr value,
12151234
int size
12161235
);
12171236

0 commit comments

Comments
 (0)