[issue43115] locale.getlocale fails if locale is set
Eryk Sun
report at bugs.python.org
Thu Feb 18 04:36:06 EST 2021
Eryk Sun <eryksun at gmail.com> added the comment:
> Is this the same as the other issue where get_locale is normalising
> the result according to some particular glibc logic and isn't at
> all portable?
If I understand Anders' immediate problem correctly, I think it can be addressed by using setlocale() to save and restore the current locale in the calendar and _strptime modules. This requires no changes to the locale module, let alone the complete rewrite that's required to make getlocale(), normalize(), _parse_localename(), and _build_localename() work reliably in Windows.
It's not just a Windows problem. For example, getlocale() doesn't return the POSIX locale modifier in a 3-tuple (language, encoding, modifier). So it can't be used to restore a locale for which the modifier is mandatory.
The following example in Linux uses Serbian, a language that's customarily written with both the Cyrillic and Latin alphabets (i.e. BCP 47 / RFC 5646 language tags "sr-Cyrl-RS" and "sr-Latn-RS"). The Latin-based Unix locale name uses a "latin" modifier.
Say the process is currently using the Latin-based locale, but I need the name of a weekday in Cyrillic:
>>> locale.setlocale(locale.LC_TIME, 'sr_RS.UTF-8 at latin')
'sr_RS.UTF-8 at latin'
>>> c = calendar.LocaleTextCalendar(locale='sr_RS.UTF-8')
>>> c.formatweekday(1, 10)
' уторак '
LocaleTextCalendar() temporarily sets LC_TIME to the given locale and then restores the previous locale. But this is based on the getlocale() result, which omits the "latin" modifier. So now my current LC_TIME locale has changed to Cyrillic:
>>> locale.setlocale(locale.LC_TIME)
'sr_RS.UTF-8'
A related problem with modifiers affects getdefaultlocale(). For example, in Linux:
$ LC_ALL=sr_RS.UTF-8 at latin python -q
>>> import locale, calendar
In this case, the LC_ALL environment variable specifies the Latin-based locale, but getdefaultlocale() omits this important detail:
>>> locale.getdefaultlocale()
('sr_RS', 'UTF-8')
Based on the default locale set in the LC_ALL environment variable, the following is supposed to return the Latin name "utorak", not the Cyrillic name "уторак":
>>> c = calendar.LocaleTextCalendar()
>>> c.formatweekday(1, 10)
' уторак '
If I make it call setlocale(LC_TIME, '') instead of getdefaultlocale(), I get the right result:
>>> c = calendar.LocaleTextCalendar(locale='')
>>> c.formatweekday(1, 10)
' utorak '
Thus an empty string should be the default locale value in LocaleTextCalendar(), instead of getdefaultlocale().
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43115>
_______________________________________
More information about the Python-bugs-list
mailing list