25
25
# Yuck: LC_MESSAGES is non-standard: can't tell whether it exists before
26
26
# trying the import. So __all__ is also fiddled at the end of the file.
27
27
__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" ,
30
30
"normalize" , "LC_CTYPE" , "LC_COLLATE" , "LC_TIME" , "LC_MONETARY" ,
31
31
"LC_NUMERIC" , "LC_ALL" , "CHAR_MAX" , "getencoding" ]
32
32
@@ -247,21 +247,6 @@ def format_string(f, val, grouping=False, monetary=False):
247
247
248
248
return new_f % val
249
249
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
-
265
250
def currency (val , symbol = True , grouping = False , international = False ):
266
251
"""Formats val according to the currency settings
267
252
in the current locale."""
@@ -556,11 +541,15 @@ def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')):
556
541
"""
557
542
558
543
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
+
563
551
552
+ def _getdefaultlocale (envvars = ('LC_ALL' , 'LC_CTYPE' , 'LANG' , 'LANGUAGE' )):
564
553
try :
565
554
# check if it's supported by the _locale module
566
555
import _locale
@@ -625,40 +614,15 @@ def setlocale(category, locale=None):
625
614
locale = normalize (_build_localename (locale ))
626
615
return _setlocale (category , locale )
627
616
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
-
648
617
649
618
try :
650
619
from _locale import getencoding
651
620
except ImportError :
621
+ # When _locale.getencoding() is missing, locale.getencoding() uses the
622
+ # Python filesystem encoding.
652
623
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
+
662
626
663
627
try :
664
628
CODESET
@@ -896,6 +860,28 @@ def getpreferredencoding(do_setlocale=True):
896
860
# updated 'ca_es@valencia' -> 'ca_ES.ISO8859-15@valencia' to 'ca_ES.UTF-8@valencia'
897
861
# updated 'kk_kz' -> 'kk_KZ.RK1048' to 'kk_KZ.ptcp154'
898
862
# 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.
899
885
900
886
locale_alias = {
901
887
'a3' : 'az_AZ.KOI8-C' ,
@@ -975,7 +961,6 @@ def getpreferredencoding(do_setlocale=True):
975
961
'c.ascii' : 'C' ,
976
962
'c.en' : 'C' ,
977
963
'c.iso88591' : 'en_US.ISO8859-1' ,
978
- 'c.utf8' : 'en_US.UTF-8' ,
979
964
'c_c' : 'C' ,
980
965
'c_c.c' : 'C' ,
981
966
'ca' : 'ca_ES.ISO8859-1' ,
@@ -992,6 +977,7 @@ def getpreferredencoding(do_setlocale=True):
992
977
'chr_us' : 'chr_US.UTF-8' ,
993
978
'ckb_iq' : 'ckb_IQ.UTF-8' ,
994
979
'cmn_tw' : 'cmn_TW.UTF-8' ,
980
+ 'crh_ru' : 'crh_RU.UTF-8' ,
995
981
'crh_ua' : 'crh_UA.UTF-8' ,
996
982
'croatian' : 'hr_HR.ISO8859-2' ,
997
983
'cs' : 'cs_CZ.ISO8859-2' ,
@@ -1013,11 +999,12 @@ def getpreferredencoding(do_setlocale=True):
1013
999
'de_be' : 'de_BE.ISO8859-1' ,
1014
1000
'de_ch' : 'de_CH.ISO8859-1' ,
1015
1001
'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 ' ,
1018
1004
'de_lu' : 'de_LU.ISO8859-1' ,
1019
1005
'deutsch' : 'de_DE.ISO8859-1' ,
1020
1006
'doi_in' : 'doi_IN.UTF-8' ,
1007
+ 'dsb_de' : 'dsb_DE.UTF-8' ,
1021
1008
'dutch' : 'nl_NL.ISO8859-1' ,
1022
1009
'dutch.iso88591' : 'nl_BE.ISO8859-1' ,
1023
1010
'dv_mv' : 'dv_MV.UTF-8' ,
@@ -1040,7 +1027,7 @@ def getpreferredencoding(do_setlocale=True):
1040
1027
'en_gb' : 'en_GB.ISO8859-1' ,
1041
1028
'en_hk' : 'en_HK.ISO8859-1' ,
1042
1029
'en_ie' : 'en_IE.ISO8859-1' ,
1043
- 'en_il' : 'en_IL.UTF-8 ' ,
1030
+ 'en_il' : 'en_IL.ISO8859-1 ' ,
1044
1031
'en_in' : 'en_IN.ISO8859-1' ,
1045
1032
'en_ng' : 'en_NG.UTF-8' ,
1046
1033
'en_nz' : 'en_NZ.ISO8859-1' ,
@@ -1056,7 +1043,6 @@ def getpreferredencoding(do_setlocale=True):
1056
1043
'en_zw.utf8' : 'en_ZS.UTF-8' ,
1057
1044
'eng_gb' : 'en_GB.ISO8859-1' ,
1058
1045
'english' : 'en_EN.ISO8859-1' ,
1059
- 'english.iso88591' : 'en_US.ISO8859-1' ,
1060
1046
'english_uk' : 'en_GB.ISO8859-1' ,
1061
1047
'english_united-states' : 'en_US.ISO8859-1' ,
1062
1048
'english_united-states.437' : 'C' ,
@@ -1072,7 +1058,7 @@ def getpreferredencoding(do_setlocale=True):
1072
1058
'es_cl' : 'es_CL.ISO8859-1' ,
1073
1059
'es_co' : 'es_CO.ISO8859-1' ,
1074
1060
'es_cr' : 'es_CR.ISO8859-1' ,
1075
- 'es_cu' : 'es_CU.UTF-8 ' ,
1061
+ 'es_cu' : 'es_CU.ISO8859-1 ' ,
1076
1062
'es_do' : 'es_DO.ISO8859-1' ,
1077
1063
'es_ec' : 'es_EC.ISO8859-1' ,
1078
1064
'es_es' : 'es_ES.ISO8859-1' ,
@@ -1122,6 +1108,7 @@ def getpreferredencoding(do_setlocale=True):
1122
1108
'ga_ie' : 'ga_IE.ISO8859-1' ,
1123
1109
'galego' : 'gl_ES.ISO8859-1' ,
1124
1110
'galician' : 'gl_ES.ISO8859-1' ,
1111
+ 'gbm_in' : 'gbm_IN.UTF-8' ,
1125
1112
'gd' : 'gd_GB.ISO8859-1' ,
1126
1113
'gd_gb' : 'gd_GB.ISO8859-1' ,
1127
1114
'ger_de' : 'de_DE.ISO8859-1' ,
@@ -1162,6 +1149,7 @@ def getpreferredencoding(do_setlocale=True):
1162
1149
'icelandic' : 'is_IS.ISO8859-1' ,
1163
1150
'id' : 'id_ID.ISO8859-1' ,
1164
1151
'id_id' : 'id_ID.ISO8859-1' ,
1152
+ 'ie' : 'ie.UTF-8' ,
1165
1153
'ig_ng' : 'ig_NG.UTF-8' ,
1166
1154
'ik_ca' : 'ik_CA.UTF-8' ,
1167
1155
'in' : 'id_ID.ISO8859-1' ,
@@ -1216,6 +1204,7 @@ def getpreferredencoding(do_setlocale=True):
1216
1204
'ks_in' : 'ks_IN.UTF-8' ,
1217
1205
'ks_in@devanagari.utf8' : 'ks_IN.UTF-8@devanagari' ,
1218
1206
'ku_tr' : 'ku_TR.ISO8859-9' ,
1207
+ 'kv_ru' : 'kv_RU.UTF-8' ,
1219
1208
'kw' : 'kw_GB.ISO8859-1' ,
1220
1209
'kw_gb' : 'kw_GB.ISO8859-1' ,
1221
1210
'ky' : 'ky_KG.UTF-8' ,
@@ -1234,13 +1223,15 @@ def getpreferredencoding(do_setlocale=True):
1234
1223
'lo_la.mulelao1' : 'lo_LA.MULELAO-1' ,
1235
1224
'lt' : 'lt_LT.ISO8859-13' ,
1236
1225
'lt_lt' : 'lt_LT.ISO8859-13' ,
1226
+ 'ltg_lv.utf8' : 'ltg_LV.UTF-8' ,
1237
1227
'lv' : 'lv_LV.ISO8859-13' ,
1238
1228
'lv_lv' : 'lv_LV.ISO8859-13' ,
1239
1229
'lzh_tw' : 'lzh_TW.UTF-8' ,
1240
1230
'mag_in' : 'mag_IN.UTF-8' ,
1241
1231
'mai' : 'mai_IN.UTF-8' ,
1242
1232
'mai_in' : 'mai_IN.UTF-8' ,
1243
1233
'mai_np' : 'mai_NP.UTF-8' ,
1234
+ 'mdf_ru' : 'mdf_RU.UTF-8' ,
1244
1235
'mfe_mu' : 'mfe_MU.UTF-8' ,
1245
1236
'mg_mg' : 'mg_MG.ISO8859-15' ,
1246
1237
'mhr_ru' : 'mhr_RU.UTF-8' ,
@@ -1254,6 +1245,7 @@ def getpreferredencoding(do_setlocale=True):
1254
1245
'ml_in' : 'ml_IN.UTF-8' ,
1255
1246
'mn_mn' : 'mn_MN.UTF-8' ,
1256
1247
'mni_in' : 'mni_IN.UTF-8' ,
1248
+ 'mnw_mm' : 'mnw_MM.UTF-8' ,
1257
1249
'mr' : 'mr_IN.UTF-8' ,
1258
1250
'mr_in' : 'mr_IN.UTF-8' ,
1259
1251
'ms' : 'ms_MY.ISO8859-1' ,
@@ -1322,19 +1314,22 @@ def getpreferredencoding(do_setlocale=True):
1322
1314
'pt_pt' : 'pt_PT.ISO8859-1' ,
1323
1315
'quz_pe' : 'quz_PE.UTF-8' ,
1324
1316
'raj_in' : 'raj_IN.UTF-8' ,
1317
+ 'rif_ma' : 'rif_MA.UTF-8' ,
1325
1318
'ro' : 'ro_RO.ISO8859-2' ,
1326
1319
'ro_ro' : 'ro_RO.ISO8859-2' ,
1327
1320
'romanian' : 'ro_RO.ISO8859-2' ,
1328
1321
'ru' : 'ru_RU.UTF-8' ,
1329
1322
'ru_ru' : 'ru_RU.UTF-8' ,
1330
1323
'ru_ua' : 'ru_UA.KOI8-U' ,
1331
1324
'rumanian' : 'ro_RO.ISO8859-2' ,
1332
- 'russian' : 'ru_RU.KOI8-R ' ,
1325
+ 'russian' : 'ru_RU.ISO8859-5 ' ,
1333
1326
'rw' : 'rw_RW.ISO8859-1' ,
1334
1327
'rw_rw' : 'rw_RW.ISO8859-1' ,
1335
1328
'sa_in' : 'sa_IN.UTF-8' ,
1329
+ 'sah_ru' : 'sah_RU.UTF-8' ,
1336
1330
'sat_in' : 'sat_IN.UTF-8' ,
1337
1331
'sc_it' : 'sc_IT.UTF-8' ,
1332
+ 'scn_it' : 'scn_IT.UTF-8' ,
1338
1333
'sd' : 'sd_IN.UTF-8' ,
1339
1334
'sd_in' : 'sd_IN.UTF-8' ,
1340
1335
'sd_in@devanagari.utf8' : 'sd_IN.UTF-8@devanagari' ,
@@ -1376,7 +1371,7 @@ def getpreferredencoding(do_setlocale=True):
1376
1371
'sq_mk' : 'sq_MK.UTF-8' ,
1377
1372
'sr' : 'sr_RS.UTF-8' ,
1378
1373
'sr@cyrillic' : 'sr_RS.UTF-8' ,
1379
- 'sr@latn' : 'sr_CS .UTF-8@latin' ,
1374
+ 'sr@latn' : 'sr_RS .UTF-8@latin' ,
1380
1375
'sr_cs' : 'sr_CS.UTF-8' ,
1381
1376
'sr_cs.iso88592@latn' : 'sr_CS.ISO8859-2' ,
1382
1377
'sr_cs@latn' : 'sr_CS.UTF-8@latin' ,
@@ -1395,14 +1390,17 @@ def getpreferredencoding(do_setlocale=True):
1395
1390
'sr_yu@cyrillic' : 'sr_RS.UTF-8' ,
1396
1391
'ss' : 'ss_ZA.ISO8859-1' ,
1397
1392
'ss_za' : 'ss_ZA.ISO8859-1' ,
1393
+ 'ssy_er' : 'ssy_ER.UTF-8' ,
1398
1394
'st' : 'st_ZA.ISO8859-1' ,
1399
1395
'st_za' : 'st_ZA.ISO8859-1' ,
1396
+ 'su_id' : 'su_ID.UTF-8' ,
1400
1397
'sv' : 'sv_SE.ISO8859-1' ,
1401
1398
'sv_fi' : 'sv_FI.ISO8859-1' ,
1402
1399
'sv_se' : 'sv_SE.ISO8859-1' ,
1403
1400
'sw_ke' : 'sw_KE.UTF-8' ,
1404
1401
'sw_tz' : 'sw_TZ.UTF-8' ,
1405
1402
'swedish' : 'sv_SE.ISO8859-1' ,
1403
+ 'syr' : 'syr.UTF-8' ,
1406
1404
'szl_pl' : 'szl_PL.UTF-8' ,
1407
1405
'ta' : 'ta_IN.TSCII-0' ,
1408
1406
'ta_in' : 'ta_IN.TSCII-0' ,
@@ -1429,6 +1427,7 @@ def getpreferredencoding(do_setlocale=True):
1429
1427
'tn' : 'tn_ZA.ISO8859-15' ,
1430
1428
'tn_za' : 'tn_ZA.ISO8859-15' ,
1431
1429
'to_to' : 'to_TO.UTF-8' ,
1430
+ 'tok' : 'tok.UTF-8' ,
1432
1431
'tpi_pg' : 'tpi_PG.UTF-8' ,
1433
1432
'tr' : 'tr_TR.ISO8859-9' ,
1434
1433
'tr_cy' : 'tr_CY.ISO8859-9' ,
@@ -1443,8 +1442,7 @@ def getpreferredencoding(do_setlocale=True):
1443
1442
'ug_cn' : 'ug_CN.UTF-8' ,
1444
1443
'uk' : 'uk_UA.KOI8-U' ,
1445
1444
'uk_ua' : 'uk_UA.KOI8-U' ,
1446
- 'univ' : 'en_US.utf' ,
1447
- 'universal' : 'en_US.utf' ,
1445
+ 'univ.utf8' : 'en_US.UTF-8' ,
1448
1446
'universal.utf8@ucs4' : 'en_US.UTF-8' ,
1449
1447
'unm_us' : 'unm_US.UTF-8' ,
1450
1448
'ur' : 'ur_PK.CP1256' ,
@@ -1473,6 +1471,7 @@ def getpreferredencoding(do_setlocale=True):
1473
1471
'yo_ng' : 'yo_NG.UTF-8' ,
1474
1472
'yue_hk' : 'yue_HK.UTF-8' ,
1475
1473
'yuw_pg' : 'yuw_PG.UTF-8' ,
1474
+ 'zgh_ma' : 'zgh_MA.UTF-8' ,
1476
1475
'zh' : 'zh_CN.eucCN' ,
1477
1476
'zh_cn' : 'zh_CN.gb2312' ,
1478
1477
'zh_cn.big5' : 'zh_TW.big5' ,
@@ -1496,7 +1495,8 @@ def getpreferredencoding(do_setlocale=True):
1496
1495
# to include every locale up to Windows Vista.
1497
1496
#
1498
1497
# 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/
1500
1500
# Make sure you include the missing language identifier and the suggested
1501
1501
# locale code.
1502
1502
#
@@ -1742,17 +1742,6 @@ def _init_categories(categories=categories):
1742
1742
print (' Encoding: ' , enc or '(undefined)' )
1743
1743
print ()
1744
1744
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
-
1756
1745
try :
1757
1746
setlocale (LC_ALL , "" )
1758
1747
except :
0 commit comments