Skip to content

Commit 80c6419

Browse files
authored
Merge branch 'master' into py37
2 parents b3168cf + b6417ca commit 80c6419

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/runtime/runtime.cs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,11 @@ internal static bool PyString_Check(IntPtr ob)
11901190

11911191
internal static IntPtr PyString_FromString(string value)
11921192
{
1193+
#if PYTHON3
1194+
return PyUnicode_FromKindAndData(_UCS, value, value.Length);
1195+
#elif PYTHON2
11931196
return PyString_FromStringAndSize(value, value.Length);
1197+
#endif
11941198
}
11951199

11961200
#if PYTHON3
@@ -1205,13 +1209,6 @@ internal static IntPtr PyBytes_AS_STRING(IntPtr ob)
12051209
return ob + BytesOffset.ob_sval;
12061210
}
12071211

1208-
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
1209-
EntryPoint = "PyUnicode_FromStringAndSize")]
1210-
internal static extern IntPtr PyString_FromStringAndSize(
1211-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string value,
1212-
int size
1213-
);
1214-
12151212
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
12161213
internal static extern IntPtr PyUnicode_FromStringAndSize(IntPtr value, int size);
12171214
#elif PYTHON2

src/testing/conversiontest.cs

+15
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,19 @@ public string GetValue()
6060
return value;
6161
}
6262
}
63+
64+
public class UnicodeString
65+
{
66+
public string value = "안녕";
67+
68+
public string GetString()
69+
{
70+
return value;
71+
}
72+
73+
public override string ToString()
74+
{
75+
return value;
76+
}
77+
}
6378
}

src/tests/test_conversion.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

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

5+
from __future__ import unicode_literals
56
import System
67
import pytest
7-
from Python.Test import ConversionTest
8+
from Python.Test import ConversionTest, UnicodeString
89

9-
from ._compat import indexbytes, long, unichr
10+
from ._compat import indexbytes, long, unichr, text_type, PY2, PY3
1011

1112

1213
def test_bool_conversion():
@@ -535,6 +536,14 @@ def test_string_conversion():
535536

536537
with pytest.raises(TypeError):
537538
ConversionTest().StringField = 1
539+
540+
world = UnicodeString()
541+
test_unicode_str = u"안녕"
542+
assert test_unicode_str == text_type(world.value)
543+
assert test_unicode_str == text_type(world.GetString())
544+
# TODO: not sure what to do for Python 2 here (GH PR #670)
545+
if PY3:
546+
assert test_unicode_str == text_type(world)
538547

539548

540549
def test_interface_conversion():

0 commit comments

Comments
 (0)