aboutsummaryrefslogtreecommitdiffstats
path: root/sources
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-02-25 21:21:24 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-02-26 08:55:04 +0100
commit1b22fa59bffb2aedfdd106169b7501f0daeca3c5 (patch)
tree3367f9de7c31ba655813c3d2a7993fee7cb4b660 /sources
parent23ffa5906717e064f8fac27e3f402832fcdc1689 (diff)
Fix UNICODE conversion with Python3/Non-Limited API
The code does not work the same way in Python2/3. Port the Python 3 code from PySide6. Fixes: PYSIDE-1835 Change-Id: I2859180e103492f6b10b2f658db3055593b49e43 Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources')
-rw-r--r--sources/pyside2/PySide2/glue/qtcore.cpp26
1 files changed, 20 insertions, 6 deletions
diff --git a/sources/pyside2/PySide2/glue/qtcore.cpp b/sources/pyside2/PySide2/glue/qtcore.cpp
index f19cb057d..1d5b90ce5 100644
--- a/sources/pyside2/PySide2/glue/qtcore.cpp
+++ b/sources/pyside2/PySide2/glue/qtcore.cpp
@@ -1734,7 +1734,25 @@ Py_END_ALLOW_THREADS
// @snippet conversion-pylong-quintptr
// @snippet conversion-pyunicode
-#ifndef Py_LIMITED_API
+#if defined(Py_LIMITED_API)
+ wchar_t *temp = PyUnicode_AsWideCharString(%in, NULL);
+ %out = QString::fromWCharArray(temp);
+ PyMem_Free(temp);
+#elif defined(IS_PY3K)
+ void *data = PyUnicode_DATA(%in);
+ Py_ssize_t len = PyUnicode_GetLength(%in);
+ switch (PyUnicode_KIND(%in)) {
+ case PyUnicode_1BYTE_KIND:
+ %out = QString::fromLatin1(reinterpret_cast<const char *>(data));
+ break;
+ case PyUnicode_2BYTE_KIND:
+ %out = QString::fromUtf16(reinterpret_cast<const char16_t *>(data), len);
+ break;
+ case PyUnicode_4BYTE_KIND:
+ %out = QString::fromUcs4(reinterpret_cast<const char32_t *>(data), len);
+ break;
+ }
+#else // IS_PY3K
Py_UNICODE *unicode = PyUnicode_AS_UNICODE(%in);
# if defined(Py_UNICODE_WIDE)
// cast as Py_UNICODE can be a different type
@@ -1750,11 +1768,7 @@ Py_UNICODE *unicode = PyUnicode_AS_UNICODE(%in);
%out = QString::fromUtf16(reinterpret_cast<const ushort *>(unicode), PepUnicode_GetLength(%in));
# endif // Qt 6
# endif
-#else
-wchar_t *temp = PyUnicode_AsWideCharString(%in, NULL);
-%out = QString::fromWCharArray(temp);
-PyMem_Free(temp);
-#endif
+#endif // !IS_PY3K
// @snippet conversion-pyunicode
// @snippet conversion-pystring