| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1190,7 +1190,11 @@ internal static bool PyString_Check(IntPtr ob) | |||
| 1190 | 1190 | ||
| 1191 | 1191 | internal static IntPtr PyString_FromString(string value) | |
| 1192 | 1192 | { | |
| 1193 | + #if PYTHON3 | ||
| 1194 | + return PyUnicode_FromKindAndData(_UCS, value, value.Length); | ||
| 1195 | + #elif PYTHON2 | ||
| 1193 | 1196 | return PyString_FromStringAndSize(value, value.Length); | |
| 1197 | + #endif | ||
| 1194 | 1198 | } | |
| 1195 | 1199 | ||
| 1196 | 1200 | #if PYTHON3 | |
@@ -1205,13 +1209,6 @@ internal static IntPtr PyBytes_AS_STRING(IntPtr ob) | |||
| 1205 | 1209 | return ob + BytesOffset.ob_sval; | |
| 1206 | 1210 | } | |
| 1207 | 1211 | ||
| 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 | - | ||
| 1215 | 1212 | [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] | |
| 1216 | 1213 | internal static extern IntPtr PyUnicode_FromStringAndSize(IntPtr value, int size); | |
| 1217 | 1214 | #elif PYTHON2 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -60,4 +60,19 @@ public string GetValue() | |||
| 60 | 60 | return value; | |
| 61 | 61 | } | |
| 62 | 62 | } | |
| 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 | + } | ||
| 63 | 78 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,11 +2,12 @@ | |||
| 2 | 2 | ||
| 3 | 3 | """Test CLR <-> Python type conversions.""" | |
| 4 | 4 | ||
| 5 | + from __future__ import unicode_literals | ||
| 5 | 6 | import System | |
| 6 | 7 | import pytest | |
| 7 | - from Python.Test import ConversionTest | ||
| 8 | + from Python.Test import ConversionTest, UnicodeString | ||
| 8 | 9 | ||
| 9 | - from ._compat import indexbytes, long, unichr | ||
| 10 | + from ._compat import indexbytes, long, unichr, text_type, PY2, PY3 | ||
| 10 | 11 | ||
| 11 | 12 | ||
| 12 | 13 | def test_bool_conversion(): | |
@@ -535,6 +536,14 @@ def test_string_conversion(): | |||
| 535 | 536 | ||
| 536 | 537 | with pytest.raises(TypeError): | |
| 537 | 538 | 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) | ||
| 538 | 547 | ||
| 539 | 548 | ||
| 540 | 549 | def test_interface_conversion(): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments