Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.

Commit f6c6e42

Browse files
authored
Merge branch 'master' into pyobject-finalizer
2 parents cfda491 + b6417ca commit f6c6e42

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

src/runtime/runtime.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,11 @@ internal static bool PyString_Check(IntPtr ob)
11951195

11961196
internal static IntPtr PyString_FromString(string value)
11971197
{
1198+
#if PYTHON3
1199+
return PyUnicode_FromKindAndData(_UCS, value, value.Length);
1200+
#elif PYTHON2
11981201
return PyString_FromStringAndSize(value, value.Length);
1202+
#endif
11991203
}
12001204

12011205
#if PYTHON3
@@ -1210,13 +1214,6 @@ internal static IntPtr PyBytes_AS_STRING(IntPtr ob)
12101214
return ob + BytesOffset.ob_sval;
12111215
}
12121216

1213-
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
1214-
EntryPoint = "PyUnicode_FromStringAndSize")]
1215-
internal static extern IntPtr PyString_FromStringAndSize(
1216-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string value,
1217-
int size
1218-
);
1219-
12201217
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
12211218
internal static extern IntPtr PyUnicode_FromStringAndSize(IntPtr value, int size);
12221219
#elif PYTHON2

src/testing/conversiontest.cs

Lines changed: 15 additions & 0 deletions
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

Lines changed: 11 additions & 2 deletions
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)