diff options
author | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2023-08-18 12:24:51 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-08-25 08:36:10 +0000 |
commit | 0175fcf3640228f96a9e51c8ab40cb899760507f (patch) | |
tree | 00d142bd62f17d6c789625560dd41b2c4206ac2d | |
parent | 9dadc22af61cecf8fb38fc107e273c45d0672500 (diff) |
PySide: Fix QLocale::system() in macOS
- QSystemLocale for macOS relies on CFBundleAllowMixedLocalizations/
CFBundleLocalizations in the Info.plist file for the C++
Application bundle, as seen from
1d3ae5f0e98f252214d20ce8561533891311a71f. Python being an
interpreted language, there is no application bundle unless the
application is deployed. As such a Python application in macOS
relies on the Info.plist file of the Python interepreter. This
Info.plist file is a read-only file and hence it is not
possible/recommended to patch the Info.plist file of the Python
interpreter at runtime.
The issue has been raised upstream in CPython and can be tracked
here: https://github.com/python/cpython/issues/108269
- A possible solution/hack is therefore to use to POSIX environment
variables for macOS, for Qt for Python. This is also what the Python
locale module does.
See: https://github.com/python/cpython/blob/3.11/Lib/locale.py#L534
For other Unix systems, QLocale::system() uses the POSIX environment
variables, just like Python's locale module.
- For Windows and Linux, QSystem::locale() remains unchanged.
- The idea here is to obtain the system locale from the Python locale
module, and use the result to initialize and return a QLocale
object.
- As an extra, for qrunnable_create fix the typo - snipped to
snippet.
Fixes: PYSIDE-2419
Change-Id: I12b16e824ddba21d1ebcd0695262c1dc0cc61eb2
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit 35c96115cb4fb71fcc263abf43099dd712187080)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | sources/pyside6/PySide6/QtCore/typesystem_core_common.xml | 21 | ||||
-rw-r--r-- | sources/pyside6/PySide6/glue/qtcore.cpp | 25 |
2 files changed, 45 insertions, 1 deletions
diff --git a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml index 4ecd1a48d..153de1bf2 100644 --- a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml +++ b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml @@ -1330,10 +1330,31 @@ <modify-function signature="toString(qulonglong)const" remove="all"/> <modify-function signature="toString(ushort)const" remove="all"/> <modify-function signature="toString(unsigned int)const" remove="all"/> + <modify-function signature="system()" remove="all"/> <!--### --> <extra-includes> <include file-name="QDate" location="global"/> </extra-includes> + <add-function signature="system()" return-type="QLocale" static="yes"> + <inject-code class="target" position="beginning" file="../glue/qtcore.cpp" + snippet="qlocale_system"/> + <inject-documentation mode="append" format="target"> + Returns a QLocale object initialized to the system locale. + + The system locale may use system-specific sources for locale data, where available, + otherwise falling back on QLocale's built-in database entry for the language, script and + territory the system reports. + + For example, on Windows, this locale will use the decimal/grouping characters and + date/time formats specified in the system configuration panel. + + .. note:: Qt for Python on macOS will not reflect the user's region and language + preferences though QLocale::system(), but will instead reflect the environment + variables POSIX uses to specify locale, similar to Python's locale module. + + See also c(). + </inject-documentation> + </add-function> <modify-function signature="toTime(QString,QLocale::FormatType)const"> <modify-argument index="2"> <rename to="format"/> diff --git a/sources/pyside6/PySide6/glue/qtcore.cpp b/sources/pyside6/PySide6/glue/qtcore.cpp index a706042b4..80aaca96a 100644 --- a/sources/pyside6/PySide6/glue/qtcore.cpp +++ b/sources/pyside6/PySide6/glue/qtcore.cpp @@ -1848,4 +1848,27 @@ auto callback = [callable]() -> void Py_INCREF(callable); %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(callback); %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); -// @snipped qrunnable_create +// @snippet qrunnable_create + +// @snippet qlocale_system +// For darwin systems, QLocale::system() involves looking at the Info.plist of the application +// bundle to detect the system localization. In the case of Qt for Python, the application bundle +// is the used Python framework. To enable retreival of localized string, the property list key +// CFBunldeAllowMixedLocalizations should be set to True inside the Info.plist file. Otherwise, +// CFBundleDevelopmentRegion will be used to find the language preference of the user, which in the +// case of Python is always english. +// This is a hack until CFBunldeAllowMixedLocalizations will be set in the Python framework +// installation in darwin systems. +// Upstream issue in CPython: https://github.com/python/cpython/issues/108269 +#ifdef Q_OS_DARWIN + Shiboken::AutoDecRef locale(PyImport_ImportModule("locale")); + Shiboken::AutoDecRef getLocale(PyObject_GetAttrString(locale, "getlocale")); + Shiboken::AutoDecRef systemLocale(PyObject_CallObject(getLocale, nullptr)); + Shiboken::AutoDecRef localeCode(PyUnicode_AsUTF8String(PyTuple_GetItem(systemLocale, 0))); + QString localeCodeStr = PySide::pyStringToQString(localeCode); + %RETURN_TYPE %0 = QLocale(localeCodeStr); +#else + %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(); +#endif +%PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); +// @snippet qlocale_system |