summaryrefslogtreecommitdiffstats
path: root/sources/shiboken2/libshiboken/sbkstring.cpp
diff options
context:
space:
mode:
authorBoxiang Sun <daetalusun@gmail.com>2018-04-28 09:53:49 +0800
committerBoxiang Sun <daetalusun@gmail.com>2018-06-01 13:15:17 +0000
commitddbd93680730811a5020b4d429c2e3e11d6823e3 (patch)
tree4a273068d222560b2414f903faf35762802abede /sources/shiboken2/libshiboken/sbkstring.cpp
parent807183092b19b5df07a0630a78ebe534ba38a7b2 (diff)
Improve sbkstring::toCString to support unicode
If `unicode` is used in Python 2, shiboken cannot properly translate it to `const char *`. sbkstring did not supported a proper conversion of unicode types in Python2, and this change includes it. Task-number: PYSIDE-100 Change-Id: I3a70d935ad61b0e567e620c62754800370270a6b Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Diffstat (limited to 'sources/shiboken2/libshiboken/sbkstring.cpp')
-rw-r--r--sources/shiboken2/libshiboken/sbkstring.cpp10
1 files changed, 8 insertions, 2 deletions
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);