gh-137729: Fix support for locales with @-modifiers (GH-137253)

This commit is contained in:
Serhiy Storchaka 2025-08-18 10:11:15 +03:00 committed by GitHub
parent bc2872445b
commit 0c8fecc4cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 164 additions and 16 deletions

View file

@ -375,12 +375,14 @@ def _replace_encoding(code, encoding):
def _append_modifier(code, modifier):
if modifier == 'euro':
if '.' not in code:
return code + '.ISO8859-15'
# Linux appears to require keeping the "@euro" modifier in place,
# even when using the ".ISO8859-15" encoding.
return code + '.ISO8859-15@euro'
_, _, encoding = code.partition('.')
if encoding in ('ISO8859-15', 'UTF-8'):
if encoding == 'UTF-8':
return code
if encoding == 'ISO8859-1':
return _replace_encoding(code, 'ISO8859-15')
code = _replace_encoding(code, 'ISO8859-15')
return code + '@' + modifier
def normalize(localename):
@ -485,13 +487,18 @@ def _parse_localename(localename):
# Deal with locale modifiers
code, modifier = code.split('@', 1)
if modifier == 'euro' and '.' not in code:
# Assume Latin-9 for @euro locales. This is bogus,
# since some systems may use other encodings for these
# locales. Also, we ignore other modifiers.
return code, 'iso-8859-15'
# Assume ISO8859-15 for @euro locales. Do note that some systems
# may use other encodings for these locales, so this may not always
# be correct.
return code + '@euro', 'ISO8859-15'
else:
modifier = ''
if '.' in code:
return tuple(code.split('.')[:2])
code, encoding = code.split('.')[:2]
if modifier:
code += '@' + modifier
return code, encoding
elif code == 'C':
return None, None
elif code == 'UTF-8':
@ -516,7 +523,14 @@ def _build_localename(localetuple):
if encoding is None:
return language
else:
return language + '.' + encoding
if '@' in language:
language, modifier = language.split('@', 1)
else:
modifier = ''
localename = language + '.' + encoding
if modifier:
localename += '@' + modifier
return localename
except (TypeError, ValueError):
raise TypeError('Locale must be None, a string, or an iterable of '
'two strings -- language code, encoding.') from None
@ -888,6 +902,12 @@ def getpreferredencoding(do_setlocale=True):
# SS 2025-06-10:
# Remove 'c.utf8' -> 'en_US.UTF-8' because 'en_US.UTF-8' does not exist
# on all platforms.
#
# SS 2025-07-30:
# Remove conflicts with GNU libc.
#
# removed 'el_gr@euro'
# removed 'uz_uz@cyrillic'
locale_alias = {
'a3': 'az_AZ.KOI8-C',
@ -1021,7 +1041,6 @@ def getpreferredencoding(do_setlocale=True):
'el': 'el_GR.ISO8859-7',
'el_cy': 'el_CY.ISO8859-7',
'el_gr': 'el_GR.ISO8859-7',
'el_gr@euro': 'el_GR.ISO8859-15',
'en': 'en_US.ISO8859-1',
'en_ag': 'en_AG.UTF-8',
'en_au': 'en_AU.ISO8859-1',
@ -1456,7 +1475,6 @@ def getpreferredencoding(do_setlocale=True):
'ur_pk': 'ur_PK.CP1256',
'uz': 'uz_UZ.UTF-8',
'uz_uz': 'uz_UZ.UTF-8',
'uz_uz@cyrillic': 'uz_UZ.UTF-8',
've': 've_ZA.UTF-8',
've_za': 've_ZA.UTF-8',
'vi': 'vi_VN.TCVN',