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
11 changes: 4 additions & 7 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,11 @@ internal static bool PyString_Check(IntPtr ob)

internal static IntPtr PyString_FromString(string value)
{
#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 @@ -1205,13 +1209,6 @@ 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(
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string value,
int size
);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyUnicode_FromStringAndSize(IntPtr value, int size);
#elif PYTHON2
Expand Down
15 changes: 15 additions & 0 deletions src/testing/conversiontest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,19 @@ public string GetValue()
return value;
}
}

public class UnicodeString
{
public string value = "안녕";

public string GetString()
{
return value;
}

public override string ToString()
{
return value;
}
}
}
13 changes: 11 additions & 2 deletions src/tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

"""Test CLR <-> Python type conversions."""

from __future__ import unicode_literals
import System
import pytest
from Python.Test import ConversionTest
from Python.Test import ConversionTest, UnicodeString

from ._compat import indexbytes, long, unichr
from ._compat import indexbytes, long, unichr, text_type, PY2, PY3


def test_bool_conversion():
Expand Down Expand Up @@ -535,6 +536,14 @@ def test_string_conversion():

with pytest.raises(TypeError):
ConversionTest().StringField = 1

world = UnicodeString()
test_unicode_str = u"안녕"
assert test_unicode_str == text_type(world.value)
assert test_unicode_str == text_type(world.GetString())
# TODO: not sure what to do for Python 2 here (GH PR #670)
if PY3:
assert test_unicode_str == text_type(world)


def test_interface_conversion():
Expand Down