summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sources/pyside2/tests/QtCore/unicode_test.py5
-rw-r--r--sources/shiboken2/libshiboken/sbkstring.cpp10
2 files changed, 13 insertions, 2 deletions
diff --git a/sources/pyside2/tests/QtCore/unicode_test.py b/sources/pyside2/tests/QtCore/unicode_test.py
index 3326e2e0..f44a00c6 100644
--- a/sources/pyside2/tests/QtCore/unicode_test.py
+++ b/sources/pyside2/tests/QtCore/unicode_test.py
@@ -35,6 +35,7 @@ import unittest
import py3kcompat as py3k
from PySide2.QtCore import QObject
+from PySide2.QtCore import QByteArray
class UnicodeConversion(unittest.TestCase):
'''Test case for QString to/from Python Unicode conversion'''
@@ -57,6 +58,10 @@ class UnicodeConversion(unittest.TestCase):
obj.setObjectName(py3k.unicode_('├╝mlaut'))
self.assertEqual(obj.objectName(), py3k.unicode_('├╝mlaut'))
+ def testTranslateUnicode(self):
+ ba = QByteArray(py3k.unicode_('0123456789'))
+ self.assertEqual(ba.__str__(), py3k.unicode_('0123456789'))
+
if __name__ == '__main__':
unittest.main()
diff --git a/sources/shiboken2/libshiboken/sbkstring.cpp b/sources/shiboken2/libshiboken/sbkstring.cpp
index b9267438..6ca35f12 100644
--- a/sources/shiboken2/libshiboken/sbkstring.cpp
+++ b/sources/shiboken2/libshiboken/sbkstring.cpp
@@ -99,18 +99,24 @@ const char* toCString(PyObject* str, Py_ssize_t* len)
{
if (str == Py_None)
return NULL;
-#ifdef IS_PY3K
if (PyUnicode_Check(str)) {
if (len) {
// We need to encode the unicode string into utf8 to know the size of returned char*.
Shiboken::AutoDecRef uniStr(PyUnicode_AsUTF8String(str));
*len = PyBytes_GET_SIZE(uniStr.object());
}
+#ifdef IS_PY3K
// Return unicode from str instead of uniStr, because the lifetime of the returned pointer
// depends on the lifetime of str.
return _PepUnicode_AsString(str);
- }
+#else
+ str = PyUnicode_AsUTF8String(str);
+ if (str == NULL) {
+ return NULL;
+ }
+ return PyString_AsString(str);
#endif
+ }
if (PyBytes_Check(str)) {
if (len)
*len = PyBytes_GET_SIZE(str);