diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-04-26 13:11:18 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-04-27 20:49:11 +0200 |
commit | 7809a047416eacb6f245160aa018127f5b0e8e81 (patch) | |
tree | 4432b0f042c09f9b1ad2e6587c0c24a923e50003 | |
parent | 70ffe0b5136cfce75c94fa09cd6070b7270f684d (diff) |
PySide6/Windows: Fix encoding of QCoreApplication.arguments()
Encode in the console's code page via wchar_t.
Fixes: PYSIDE-1425
Change-Id: Ideae87361409a61919ea6bf8d5594609dceaa1cb
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
-rw-r--r-- | sources/pyside6/tests/QtCore/CMakeLists.txt | 1 | ||||
-rw-r--r-- | sources/pyside6/tests/QtCore/qcoreapplication_argv_test.py | 50 | ||||
-rw-r--r-- | sources/shiboken6/libshiboken/helper.cpp | 27 |
3 files changed, 78 insertions, 0 deletions
diff --git a/sources/pyside6/tests/QtCore/CMakeLists.txt b/sources/pyside6/tests/QtCore/CMakeLists.txt index bc3d28a7d..e9717d9d0 100644 --- a/sources/pyside6/tests/QtCore/CMakeLists.txt +++ b/sources/pyside6/tests/QtCore/CMakeLists.txt @@ -56,6 +56,7 @@ PYSIDE_TEST(qcalendar_test.py) PYSIDE_TEST(qcbor_test.py) PYSIDE_TEST(qcollator_test.py) PYSIDE_TEST(qcommandlineparser_test.py) +PYSIDE_TEST(qcoreapplication_argv_test.py) PYSIDE_TEST(qcoreapplication_instance_test.py) PYSIDE_TEST(qcoreapplication_test.py) PYSIDE_TEST(qdatastream_test.py) diff --git a/sources/pyside6/tests/QtCore/qcoreapplication_argv_test.py b/sources/pyside6/tests/QtCore/qcoreapplication_argv_test.py new file mode 100644 index 000000000..e013fdb71 --- /dev/null +++ b/sources/pyside6/tests/QtCore/qcoreapplication_argv_test.py @@ -0,0 +1,50 @@ +############################################################################# +## +## Copyright (C) 2021 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of Qt for Python. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import os +import sys +import unittest + +from pathlib import Path +sys.path.append(os.fspath(Path(__file__).resolve().parents[1])) +from init_paths import init_test_paths +init_test_paths(False) + +from PySide6.QtCore import QCoreApplication + + +class TestQCoreApplication(unittest.TestCase): + def testConsoleEncoding(self): + """PYSIDE-1425, console encoding on Windows.""" + arg0 = "Üllrich Ümläut" + app = QCoreApplication([arg0]) + self.assertEqual(app.arguments()[0], arg0) + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken6/libshiboken/helper.cpp b/sources/shiboken6/libshiboken/helper.cpp index 1e5252668..3f8d7b8d0 100644 --- a/sources/shiboken6/libshiboken/helper.cpp +++ b/sources/shiboken6/libshiboken/helper.cpp @@ -157,6 +157,29 @@ std::ostream &operator<<(std::ostream &str, const debugPyObject &o) return str; } +#ifdef _WIN32 +// Converts a Unicode string to a string encoded in the Windows console's +// code page via wchar_t for use with argv (PYSIDE-1425). +// FIXME: Remove once Windows console uses UTF-8 +static char *toWindowsConsoleEncoding(PyObject *unicode) +{ + wchar_t *buf = PyUnicode_AsWideCharString(unicode, nullptr); + if (buf == nullptr) + return nullptr; + const int required = WideCharToMultiByte(CP_ACP, 0, buf, -1, + nullptr, 0, nullptr, nullptr); + if (required == 0) { + PyMem_Free(buf); + return nullptr; + } + char *result = new char[required]; + WideCharToMultiByte(CP_ACP, 0, buf, -1, + result, required, nullptr, nullptr); + PyMem_Free(buf); + return result; +} +#endif // _WIN32 + // PySide-510: Changed from PySequence to PyList, which is correct. bool listToArgcArgv(PyObject *argList, int *argc, char ***argv, const char *defaultAppName) { @@ -192,7 +215,11 @@ bool listToArgcArgv(PyObject *argList, int *argc, char ***argv, const char *defa PyObject *item = PyList_GET_ITEM(args.object(), i); char *string = nullptr; if (Shiboken::String::check(item)) { +#ifdef _WIN32 + string = toWindowsConsoleEncoding(item); +#else string = strdup(Shiboken::String::toCString(item)); +#endif } (*argv)[i] = string; } |