diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index b08a56622..b4ddc7f7e 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -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); +#endif } #if PYTHON3 @@ -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 diff --git a/src/testing/conversiontest.cs b/src/testing/conversiontest.cs index 36294594c..06ab7cb4e 100644 --- a/src/testing/conversiontest.cs +++ b/src/testing/conversiontest.cs @@ -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; + } + } } diff --git a/src/tests/test_conversion.py b/src/tests/test_conversion.py index 53e5d8051..0ba10a80e 100644 --- a/src/tests/test_conversion.py +++ b/src/tests/test_conversion.py @@ -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(): @@ -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():