Skip to content

fixed bug of method PyString_FromString #670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Aug 24, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! fixed bug of method PyString_FromString
  • Loading branch information
yagweb committed May 11, 2018
commit e9451db9a3d126ec23e213cbb300550fd92ff33f
34 changes: 6 additions & 28 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,26 +1193,11 @@ internal static bool PyString_Check(IntPtr ob)

internal static IntPtr PyString_FromString(string value)
{
if (value == null)
{
return IntPtr.Zero;
}

byte[] bStr = Encoding.UTF8.GetBytes(value);
IntPtr mem = Marshal.AllocHGlobal(bStr.Length);
try
{
Marshal.Copy(bStr, 0, mem, bStr.Length);
}
catch (Exception)
{
Marshal.FreeHGlobal(mem);
throw;
}

IntPtr result = PyString_FromStringAndSize(mem, bStr.Length);
Marshal.FreeHGlobal(mem);
return result;
#if PYTHON3
return PyUnicode_FromKindAndData(_UCS, value, value.Length);
#elif PYTHON2
return PyString_FromStringAndSize(value, value.Length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe check here whether the string is ASCII? I'm not sure where this one is used, but we should probably distinguish the case where we actually want Unicode (and should also return that on Python 2) from the cases that should be ASCII-only on Python 2 (like identifiers).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@filmor can you file a separate issue for this with a suggested fix? this PR is now ready to merge!

#endif
}

#if PYTHON3
Expand All @@ -1226,14 +1211,7 @@ internal static IntPtr PyBytes_AS_STRING(IntPtr ob)
{
return ob + BytesOffset.ob_sval;
}

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "PyUnicode_FromStringAndSize")]
internal static extern IntPtr PyString_FromStringAndSize(
IntPtr value,
int size
);


[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyUnicode_FromStringAndSize(IntPtr value, int size);
#elif PYTHON2
Expand Down