Skip to content

Commit 242814f

Browse files
authored
Update locale.py from 3.13.6 and made _locale available on android (#6091)
1 parent ddc0849 commit 242814f

File tree

4 files changed

+141
-98
lines changed

4 files changed

+141
-98
lines changed

Lib/locale.py

Lines changed: 61 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
# Yuck: LC_MESSAGES is non-standard: can't tell whether it exists before
2626
# trying the import. So __all__ is also fiddled at the end of the file.
2727
__all__ = ["getlocale", "getdefaultlocale", "getpreferredencoding", "Error",
28-
"setlocale", "resetlocale", "localeconv", "strcoll", "strxfrm",
29-
"str", "atof", "atoi", "format", "format_string", "currency",
28+
"setlocale", "localeconv", "strcoll", "strxfrm",
29+
"str", "atof", "atoi", "format_string", "currency",
3030
"normalize", "LC_CTYPE", "LC_COLLATE", "LC_TIME", "LC_MONETARY",
3131
"LC_NUMERIC", "LC_ALL", "CHAR_MAX", "getencoding"]
3232

@@ -247,21 +247,6 @@ def format_string(f, val, grouping=False, monetary=False):
247247

248248
return new_f % val
249249

250-
def format(percent, value, grouping=False, monetary=False, *additional):
251-
"""Deprecated, use format_string instead."""
252-
import warnings
253-
warnings.warn(
254-
"This method will be removed in a future version of Python. "
255-
"Use 'locale.format_string()' instead.",
256-
DeprecationWarning, stacklevel=2
257-
)
258-
259-
match = _percent_re.match(percent)
260-
if not match or len(match.group())!= len(percent):
261-
raise ValueError(("format() must be given exactly one %%char "
262-
"format specifier, %s not valid") % repr(percent))
263-
return _format(percent, value, grouping, monetary, *additional)
264-
265250
def currency(val, symbol=True, grouping=False, international=False):
266251
"""Formats val according to the currency settings
267252
in the current locale."""
@@ -556,11 +541,15 @@ def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')):
556541
"""
557542

558543
import warnings
559-
warnings.warn(
560-
"Use setlocale(), getencoding() and getlocale() instead",
561-
DeprecationWarning, stacklevel=2
562-
)
544+
warnings._deprecated(
545+
"locale.getdefaultlocale",
546+
"{name!r} is deprecated and slated for removal in Python {remove}. "
547+
"Use setlocale(), getencoding() and getlocale() instead.",
548+
remove=(3, 15))
549+
return _getdefaultlocale(envvars)
550+
563551

552+
def _getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')):
564553
try:
565554
# check if it's supported by the _locale module
566555
import _locale
@@ -625,40 +614,15 @@ def setlocale(category, locale=None):
625614
locale = normalize(_build_localename(locale))
626615
return _setlocale(category, locale)
627616

628-
def resetlocale(category=LC_ALL):
629-
630-
""" Sets the locale for category to the default setting.
631-
632-
The default setting is determined by calling
633-
getdefaultlocale(). category defaults to LC_ALL.
634-
635-
"""
636-
import warnings
637-
warnings.warn(
638-
'Use locale.setlocale(locale.LC_ALL, "") instead',
639-
DeprecationWarning, stacklevel=2
640-
)
641-
642-
with warnings.catch_warnings():
643-
warnings.simplefilter('ignore', category=DeprecationWarning)
644-
loc = getdefaultlocale()
645-
646-
_setlocale(category, _build_localename(loc))
647-
648617

649618
try:
650619
from _locale import getencoding
651620
except ImportError:
621+
# When _locale.getencoding() is missing, locale.getencoding() uses the
622+
# Python filesystem encoding.
652623
def getencoding():
653-
if hasattr(sys, 'getandroidapilevel'):
654-
# On Android langinfo.h and CODESET are missing, and UTF-8 is
655-
# always used in mbstowcs() and wcstombs().
656-
return 'utf-8'
657-
encoding = getdefaultlocale()[1]
658-
if encoding is None:
659-
# LANG not set, default to UTF-8
660-
encoding = 'utf-8'
661-
return encoding
624+
return sys.getfilesystemencoding()
625+
662626

663627
try:
664628
CODESET
@@ -896,6 +860,28 @@ def getpreferredencoding(do_setlocale=True):
896860
# updated 'ca_es@valencia' -> 'ca_ES.ISO8859-15@valencia' to 'ca_ES.UTF-8@valencia'
897861
# updated 'kk_kz' -> 'kk_KZ.RK1048' to 'kk_KZ.ptcp154'
898862
# updated 'russian' -> 'ru_RU.ISO8859-5' to 'ru_RU.KOI8-R'
863+
#
864+
# SS 2025-02-04:
865+
# Updated alias mapping with glibc 2.41 supported locales and the latest
866+
# X lib alias mapping.
867+
#
868+
# These are the differences compared to the old mapping (Python 3.13.1
869+
# and older):
870+
#
871+
# updated 'c.utf8' -> 'C.UTF-8' to 'en_US.UTF-8'
872+
# updated 'de_it' -> 'de_IT.ISO8859-1' to 'de_IT.UTF-8'
873+
# removed 'de_li.utf8'
874+
# updated 'en_il' -> 'en_IL.UTF-8' to 'en_IL.ISO8859-1'
875+
# removed 'english.iso88591'
876+
# updated 'es_cu' -> 'es_CU.UTF-8' to 'es_CU.ISO8859-1'
877+
# updated 'russian' -> 'ru_RU.KOI8-R' to 'ru_RU.ISO8859-5'
878+
# updated 'sr@latn' -> 'sr_CS.UTF-8@latin' to 'sr_RS.UTF-8@latin'
879+
# removed 'univ'
880+
# removed 'universal'
881+
#
882+
# SS 2025-06-10:
883+
# Remove 'c.utf8' -> 'en_US.UTF-8' because 'en_US.UTF-8' does not exist
884+
# on all platforms.
899885

900886
locale_alias = {
901887
'a3': 'az_AZ.KOI8-C',
@@ -975,7 +961,6 @@ def getpreferredencoding(do_setlocale=True):
975961
'c.ascii': 'C',
976962
'c.en': 'C',
977963
'c.iso88591': 'en_US.ISO8859-1',
978-
'c.utf8': 'en_US.UTF-8',
979964
'c_c': 'C',
980965
'c_c.c': 'C',
981966
'ca': 'ca_ES.ISO8859-1',
@@ -992,6 +977,7 @@ def getpreferredencoding(do_setlocale=True):
992977
'chr_us': 'chr_US.UTF-8',
993978
'ckb_iq': 'ckb_IQ.UTF-8',
994979
'cmn_tw': 'cmn_TW.UTF-8',
980+
'crh_ru': 'crh_RU.UTF-8',
995981
'crh_ua': 'crh_UA.UTF-8',
996982
'croatian': 'hr_HR.ISO8859-2',
997983
'cs': 'cs_CZ.ISO8859-2',
@@ -1013,11 +999,12 @@ def getpreferredencoding(do_setlocale=True):
1013999
'de_be': 'de_BE.ISO8859-1',
10141000
'de_ch': 'de_CH.ISO8859-1',
10151001
'de_de': 'de_DE.ISO8859-1',
1016-
'de_it': 'de_IT.ISO8859-1',
1017-
'de_li.utf8': 'de_LI.UTF-8',
1002+
'de_it': 'de_IT.UTF-8',
1003+
'de_li': 'de_LI.ISO8859-1',
10181004
'de_lu': 'de_LU.ISO8859-1',
10191005
'deutsch': 'de_DE.ISO8859-1',
10201006
'doi_in': 'doi_IN.UTF-8',
1007+
'dsb_de': 'dsb_DE.UTF-8',
10211008
'dutch': 'nl_NL.ISO8859-1',
10221009
'dutch.iso88591': 'nl_BE.ISO8859-1',
10231010
'dv_mv': 'dv_MV.UTF-8',
@@ -1040,7 +1027,7 @@ def getpreferredencoding(do_setlocale=True):
10401027
'en_gb': 'en_GB.ISO8859-1',
10411028
'en_hk': 'en_HK.ISO8859-1',
10421029
'en_ie': 'en_IE.ISO8859-1',
1043-
'en_il': 'en_IL.UTF-8',
1030+
'en_il': 'en_IL.ISO8859-1',
10441031
'en_in': 'en_IN.ISO8859-1',
10451032
'en_ng': 'en_NG.UTF-8',
10461033
'en_nz': 'en_NZ.ISO8859-1',
@@ -1056,7 +1043,6 @@ def getpreferredencoding(do_setlocale=True):
10561043
'en_zw.utf8': 'en_ZS.UTF-8',
10571044
'eng_gb': 'en_GB.ISO8859-1',
10581045
'english': 'en_EN.ISO8859-1',
1059-
'english.iso88591': 'en_US.ISO8859-1',
10601046
'english_uk': 'en_GB.ISO8859-1',
10611047
'english_united-states': 'en_US.ISO8859-1',
10621048
'english_united-states.437': 'C',
@@ -1072,7 +1058,7 @@ def getpreferredencoding(do_setlocale=True):
10721058
'es_cl': 'es_CL.ISO8859-1',
10731059
'es_co': 'es_CO.ISO8859-1',
10741060
'es_cr': 'es_CR.ISO8859-1',
1075-
'es_cu': 'es_CU.UTF-8',
1061+
'es_cu': 'es_CU.ISO8859-1',
10761062
'es_do': 'es_DO.ISO8859-1',
10771063
'es_ec': 'es_EC.ISO8859-1',
10781064
'es_es': 'es_ES.ISO8859-1',
@@ -1122,6 +1108,7 @@ def getpreferredencoding(do_setlocale=True):
11221108
'ga_ie': 'ga_IE.ISO8859-1',
11231109
'galego': 'gl_ES.ISO8859-1',
11241110
'galician': 'gl_ES.ISO8859-1',
1111+
'gbm_in': 'gbm_IN.UTF-8',
11251112
'gd': 'gd_GB.ISO8859-1',
11261113
'gd_gb': 'gd_GB.ISO8859-1',
11271114
'ger_de': 'de_DE.ISO8859-1',
@@ -1162,6 +1149,7 @@ def getpreferredencoding(do_setlocale=True):
11621149
'icelandic': 'is_IS.ISO8859-1',
11631150
'id': 'id_ID.ISO8859-1',
11641151
'id_id': 'id_ID.ISO8859-1',
1152+
'ie': 'ie.UTF-8',
11651153
'ig_ng': 'ig_NG.UTF-8',
11661154
'ik_ca': 'ik_CA.UTF-8',
11671155
'in': 'id_ID.ISO8859-1',
@@ -1216,6 +1204,7 @@ def getpreferredencoding(do_setlocale=True):
12161204
'ks_in': 'ks_IN.UTF-8',
12171205
'ks_in@devanagari.utf8': 'ks_IN.UTF-8@devanagari',
12181206
'ku_tr': 'ku_TR.ISO8859-9',
1207+
'kv_ru': 'kv_RU.UTF-8',
12191208
'kw': 'kw_GB.ISO8859-1',
12201209
'kw_gb': 'kw_GB.ISO8859-1',
12211210
'ky': 'ky_KG.UTF-8',
@@ -1234,13 +1223,15 @@ def getpreferredencoding(do_setlocale=True):
12341223
'lo_la.mulelao1': 'lo_LA.MULELAO-1',
12351224
'lt': 'lt_LT.ISO8859-13',
12361225
'lt_lt': 'lt_LT.ISO8859-13',
1226+
'ltg_lv.utf8': 'ltg_LV.UTF-8',
12371227
'lv': 'lv_LV.ISO8859-13',
12381228
'lv_lv': 'lv_LV.ISO8859-13',
12391229
'lzh_tw': 'lzh_TW.UTF-8',
12401230
'mag_in': 'mag_IN.UTF-8',
12411231
'mai': 'mai_IN.UTF-8',
12421232
'mai_in': 'mai_IN.UTF-8',
12431233
'mai_np': 'mai_NP.UTF-8',
1234+
'mdf_ru': 'mdf_RU.UTF-8',
12441235
'mfe_mu': 'mfe_MU.UTF-8',
12451236
'mg_mg': 'mg_MG.ISO8859-15',
12461237
'mhr_ru': 'mhr_RU.UTF-8',
@@ -1254,6 +1245,7 @@ def getpreferredencoding(do_setlocale=True):
12541245
'ml_in': 'ml_IN.UTF-8',
12551246
'mn_mn': 'mn_MN.UTF-8',
12561247
'mni_in': 'mni_IN.UTF-8',
1248+
'mnw_mm': 'mnw_MM.UTF-8',
12571249
'mr': 'mr_IN.UTF-8',
12581250
'mr_in': 'mr_IN.UTF-8',
12591251
'ms': 'ms_MY.ISO8859-1',
@@ -1322,19 +1314,22 @@ def getpreferredencoding(do_setlocale=True):
13221314
'pt_pt': 'pt_PT.ISO8859-1',
13231315
'quz_pe': 'quz_PE.UTF-8',
13241316
'raj_in': 'raj_IN.UTF-8',
1317+
'rif_ma': 'rif_MA.UTF-8',
13251318
'ro': 'ro_RO.ISO8859-2',
13261319
'ro_ro': 'ro_RO.ISO8859-2',
13271320
'romanian': 'ro_RO.ISO8859-2',
13281321
'ru': 'ru_RU.UTF-8',
13291322
'ru_ru': 'ru_RU.UTF-8',
13301323
'ru_ua': 'ru_UA.KOI8-U',
13311324
'rumanian': 'ro_RO.ISO8859-2',
1332-
'russian': 'ru_RU.KOI8-R',
1325+
'russian': 'ru_RU.ISO8859-5',
13331326
'rw': 'rw_RW.ISO8859-1',
13341327
'rw_rw': 'rw_RW.ISO8859-1',
13351328
'sa_in': 'sa_IN.UTF-8',
1329+
'sah_ru': 'sah_RU.UTF-8',
13361330
'sat_in': 'sat_IN.UTF-8',
13371331
'sc_it': 'sc_IT.UTF-8',
1332+
'scn_it': 'scn_IT.UTF-8',
13381333
'sd': 'sd_IN.UTF-8',
13391334
'sd_in': 'sd_IN.UTF-8',
13401335
'sd_in@devanagari.utf8': 'sd_IN.UTF-8@devanagari',
@@ -1376,7 +1371,7 @@ def getpreferredencoding(do_setlocale=True):
13761371
'sq_mk': 'sq_MK.UTF-8',
13771372
'sr': 'sr_RS.UTF-8',
13781373
'sr@cyrillic': 'sr_RS.UTF-8',
1379-
'sr@latn': 'sr_CS.UTF-8@latin',
1374+
'sr@latn': 'sr_RS.UTF-8@latin',
13801375
'sr_cs': 'sr_CS.UTF-8',
13811376
'sr_cs.iso88592@latn': 'sr_CS.ISO8859-2',
13821377
'sr_cs@latn': 'sr_CS.UTF-8@latin',
@@ -1395,14 +1390,17 @@ def getpreferredencoding(do_setlocale=True):
13951390
'sr_yu@cyrillic': 'sr_RS.UTF-8',
13961391
'ss': 'ss_ZA.ISO8859-1',
13971392
'ss_za': 'ss_ZA.ISO8859-1',
1393+
'ssy_er': 'ssy_ER.UTF-8',
13981394
'st': 'st_ZA.ISO8859-1',
13991395
'st_za': 'st_ZA.ISO8859-1',
1396+
'su_id': 'su_ID.UTF-8',
14001397
'sv': 'sv_SE.ISO8859-1',
14011398
'sv_fi': 'sv_FI.ISO8859-1',
14021399
'sv_se': 'sv_SE.ISO8859-1',
14031400
'sw_ke': 'sw_KE.UTF-8',
14041401
'sw_tz': 'sw_TZ.UTF-8',
14051402
'swedish': 'sv_SE.ISO8859-1',
1403+
'syr': 'syr.UTF-8',
14061404
'szl_pl': 'szl_PL.UTF-8',
14071405
'ta': 'ta_IN.TSCII-0',
14081406
'ta_in': 'ta_IN.TSCII-0',
@@ -1429,6 +1427,7 @@ def getpreferredencoding(do_setlocale=True):
14291427
'tn': 'tn_ZA.ISO8859-15',
14301428
'tn_za': 'tn_ZA.ISO8859-15',
14311429
'to_to': 'to_TO.UTF-8',
1430+
'tok': 'tok.UTF-8',
14321431
'tpi_pg': 'tpi_PG.UTF-8',
14331432
'tr': 'tr_TR.ISO8859-9',
14341433
'tr_cy': 'tr_CY.ISO8859-9',
@@ -1443,8 +1442,7 @@ def getpreferredencoding(do_setlocale=True):
14431442
'ug_cn': 'ug_CN.UTF-8',
14441443
'uk': 'uk_UA.KOI8-U',
14451444
'uk_ua': 'uk_UA.KOI8-U',
1446-
'univ': 'en_US.utf',
1447-
'universal': 'en_US.utf',
1445+
'univ.utf8': 'en_US.UTF-8',
14481446
'universal.utf8@ucs4': 'en_US.UTF-8',
14491447
'unm_us': 'unm_US.UTF-8',
14501448
'ur': 'ur_PK.CP1256',
@@ -1473,6 +1471,7 @@ def getpreferredencoding(do_setlocale=True):
14731471
'yo_ng': 'yo_NG.UTF-8',
14741472
'yue_hk': 'yue_HK.UTF-8',
14751473
'yuw_pg': 'yuw_PG.UTF-8',
1474+
'zgh_ma': 'zgh_MA.UTF-8',
14761475
'zh': 'zh_CN.eucCN',
14771476
'zh_cn': 'zh_CN.gb2312',
14781477
'zh_cn.big5': 'zh_TW.big5',
@@ -1496,7 +1495,8 @@ def getpreferredencoding(do_setlocale=True):
14961495
# to include every locale up to Windows Vista.
14971496
#
14981497
# NOTE: this mapping is incomplete. If your language is missing, please
1499-
# submit a bug report to the Python bug tracker at http://bugs.python.org/
1498+
# submit a bug report as detailed in the Python devguide at:
1499+
# https://devguide.python.org/triage/issue-tracker/
15001500
# Make sure you include the missing language identifier and the suggested
15011501
# locale code.
15021502
#
@@ -1742,17 +1742,6 @@ def _init_categories(categories=categories):
17421742
print(' Encoding: ', enc or '(undefined)')
17431743
print()
17441744

1745-
print()
1746-
print('Locale settings after calling resetlocale():')
1747-
print('-'*72)
1748-
resetlocale()
1749-
for name,category in categories.items():
1750-
print(name, '...')
1751-
lang, enc = getlocale(category)
1752-
print(' Language: ', lang or '(undefined)')
1753-
print(' Encoding: ', enc or '(undefined)')
1754-
print()
1755-
17561745
try:
17571746
setlocale(LC_ALL, "")
17581747
except:

0 commit comments

Comments
 (0)