24
24
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
- __all__ = ["getlocale" , "getdefaultlocale" , " getpreferredencoding" , "Error" ,
28
- "setlocale" , "resetlocale" , " localeconv" , "strcoll" , "strxfrm" ,
27
+ __all__ = ["getlocale" , "getpreferredencoding" , "Error" ,
28
+ "setlocale" , "localeconv" , "strcoll" , "strxfrm" ,
29
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" ]
@@ -516,67 +516,6 @@ def _build_localename(localetuple):
516
516
raise TypeError ('Locale must be None, a string, or an iterable of '
517
517
'two strings -- language code, encoding.' ) from None
518
518
519
- def getdefaultlocale (envvars = ('LC_ALL' , 'LC_CTYPE' , 'LANG' , 'LANGUAGE' )):
520
-
521
- """ Tries to determine the default locale settings and returns
522
- them as tuple (language code, encoding).
523
-
524
- According to POSIX, a program which has not called
525
- setlocale(LC_ALL, "") runs using the portable 'C' locale.
526
- Calling setlocale(LC_ALL, "") lets it use the default locale as
527
- defined by the LANG variable. Since we don't want to interfere
528
- with the current locale setting we thus emulate the behavior
529
- in the way described above.
530
-
531
- To maintain compatibility with other platforms, not only the
532
- LANG variable is tested, but a list of variables given as
533
- envvars parameter. The first found to be defined will be
534
- used. envvars defaults to the search path used in GNU gettext;
535
- it must always contain the variable name 'LANG'.
536
-
537
- Except for the code 'C', the language code corresponds to RFC
538
- 1766. code and encoding can be None in case the values cannot
539
- be determined.
540
-
541
- """
542
-
543
- import warnings
544
- warnings .warn (
545
- "Use setlocale(), getencoding() and getlocale() instead" ,
546
- DeprecationWarning , stacklevel = 2
547
- )
548
- return _getdefaultlocale (envvars )
549
-
550
- def _getdefaultlocale (envvars = ('LC_ALL' , 'LC_CTYPE' , 'LANG' , 'LANGUAGE' )):
551
- try :
552
- # check if it's supported by the _locale module
553
- import _locale
554
- code , encoding = _locale ._getdefaultlocale ()
555
- except (ImportError , AttributeError ):
556
- pass
557
- else :
558
- # make sure the code/encoding values are valid
559
- if sys .platform == "win32" and code and code [:2 ] == "0x" :
560
- # map windows language identifier to language name
561
- code = windows_locale .get (int (code , 0 ))
562
- # ...add other platform-specific processing here, if
563
- # necessary...
564
- return code , encoding
565
-
566
- # fall back on POSIX behaviour
567
- import os
568
- lookup = os .environ .get
569
- for variable in envvars :
570
- localename = lookup (variable ,None )
571
- if localename :
572
- if variable == 'LANGUAGE' :
573
- localename = localename .split (':' )[0 ]
574
- break
575
- else :
576
- localename = 'C'
577
- return _parse_localename (localename )
578
-
579
-
580
519
def getlocale (category = LC_CTYPE ):
581
520
582
521
""" Returns the current setting for the given locale category as
@@ -612,40 +551,15 @@ def setlocale(category, locale=None):
612
551
locale = normalize (_build_localename (locale ))
613
552
return _setlocale (category , locale )
614
553
615
- def resetlocale (category = LC_ALL ):
616
-
617
- """ Sets the locale for category to the default setting.
618
-
619
- The default setting is determined by calling
620
- getdefaultlocale(). category defaults to LC_ALL.
621
-
622
- """
623
- import warnings
624
- warnings .warn (
625
- 'Use locale.setlocale(locale.LC_ALL, "") instead' ,
626
- DeprecationWarning , stacklevel = 2
627
- )
628
-
629
- with warnings .catch_warnings ():
630
- warnings .simplefilter ('ignore' , category = DeprecationWarning )
631
- loc = getdefaultlocale ()
632
-
633
- _setlocale (category , _build_localename (loc ))
634
-
635
554
636
555
try :
637
556
from _locale import getencoding
638
557
except ImportError :
558
+ # When _locale.getencoding() is missing, use the Python filesystem
559
+ # encoding.
560
+ _encoding = sys .getfilesystemencoding ()
639
561
def getencoding ():
640
- if hasattr (sys , 'getandroidapilevel' ):
641
- # On Android langinfo.h and CODESET are missing, and UTF-8 is
642
- # always used in mbstowcs() and wcstombs().
643
- return 'utf-8'
644
- encoding = _getdefaultlocale ()[1 ]
645
- if encoding is None :
646
- # LANG not set, default to UTF-8
647
- encoding = 'utf-8'
648
- return encoding
562
+ return _encoding
649
563
650
564
try :
651
565
CODESET
@@ -1713,13 +1627,6 @@ def _init_categories(categories=categories):
1713
1627
_init_categories ()
1714
1628
del categories ['LC_ALL' ]
1715
1629
1716
- print ('Locale defaults as determined by getdefaultlocale():' )
1717
- print ('-' * 72 )
1718
- lang , enc = getdefaultlocale ()
1719
- print ('Language: ' , lang or '(undefined)' )
1720
- print ('Encoding: ' , enc or '(undefined)' )
1721
- print ()
1722
-
1723
1630
print ('Locale settings on startup:' )
1724
1631
print ('-' * 72 )
1725
1632
for name ,category in categories .items ():
@@ -1729,17 +1636,6 @@ def _init_categories(categories=categories):
1729
1636
print (' Encoding: ' , enc or '(undefined)' )
1730
1637
print ()
1731
1638
1732
- print ()
1733
- print ('Locale settings after calling resetlocale():' )
1734
- print ('-' * 72 )
1735
- resetlocale ()
1736
- for name ,category in categories .items ():
1737
- print (name , '...' )
1738
- lang , enc = getlocale (category )
1739
- print (' Language: ' , lang or '(undefined)' )
1740
- print (' Encoding: ' , enc or '(undefined)' )
1741
- print ()
1742
-
1743
1639
try :
1744
1640
setlocale (LC_ALL , "" )
1745
1641
except :
0 commit comments