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

Skip to content

Commit e9451db

Browse files
committed
fixup! fixed bug of method PyString_FromString
1 parent a0f4c33 commit e9451db

File tree

1 file changed

+6
-28
lines changed

1 file changed

+6
-28
lines changed

src/runtime/runtime.cs

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

11941194
internal static IntPtr PyString_FromString(string value)
11951195
{
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;
1196+
#if PYTHON3
1197+
return PyUnicode_FromKindAndData(_UCS, value, value.Length);
1198+
#elif PYTHON2
1199+
return PyString_FromStringAndSize(value, value.Length);
1200+
#endif
12161201
}
12171202

12181203
#if PYTHON3
@@ -1226,14 +1211,7 @@ internal static IntPtr PyBytes_AS_STRING(IntPtr ob)
12261211
{
12271212
return ob + BytesOffset.ob_sval;
12281213
}
1229-
1230-
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
1231-
EntryPoint = "PyUnicode_FromStringAndSize")]
1232-
internal static extern IntPtr PyString_FromStringAndSize(
1233-
IntPtr value,
1234-
int size
1235-
);
1236-
1214+
12371215
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
12381216
internal static extern IntPtr PyUnicode_FromStringAndSize(IntPtr value, int size);
12391217
#elif PYTHON2

0 commit comments

Comments
 (0)