Skip to content

Commit

Permalink
PySide: Fix QLocale::system() in macOS
Browse files Browse the repository at this point in the history
- 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: python/cpython#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
Pick-to: 6.5 6.2
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>
  • Loading branch information
Shyamnath Premnadh authored and Shyamnath Premnadh committed Aug 25, 2023
1 parent f3f8939 commit 35c9611
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
21 changes: 21 additions & 0 deletions sources/pyside6/PySide6/QtCore/typesystem_core_common.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1332,10 +1332,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"/>
Expand Down
25 changes: 24 additions & 1 deletion sources/pyside6/PySide6/glue/qtcore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1842,4 +1842,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

0 comments on commit 35c9611

Please sign in to comment.