2424
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.
27- __all__ = ["getlocale" , "getdefaultlocale" , " getpreferredencoding" , "Error" ,
28- "setlocale" , "resetlocale" , " localeconv" , "strcoll" , "strxfrm" ,
27+ __all__ = ["getlocale" , "getpreferredencoding" , "Error" ,
28+ "setlocale" , "localeconv" , "strcoll" , "strxfrm" ,
2929 "str" , "atof" , "atoi" , "format_string" , "currency" ,
3030 "normalize" , "LC_CTYPE" , "LC_COLLATE" , "LC_TIME" , "LC_MONETARY" ,
3131 "LC_NUMERIC" , "LC_ALL" , "CHAR_MAX" , "getencoding" ]
@@ -516,67 +516,6 @@ def _build_localename(localetuple):
516516 raise TypeError ('Locale must be None, a string, or an iterable of '
517517 'two strings -- language code, encoding.' ) from None
518518
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-
580519def getlocale (category = LC_CTYPE ):
581520
582521 """ Returns the current setting for the given locale category as
@@ -612,40 +551,15 @@ def setlocale(category, locale=None):
612551 locale = normalize (_build_localename (locale ))
613552 return _setlocale (category , locale )
614553
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-
635554
636555try :
637556 from _locale import getencoding
638557except ImportError :
558+ # When _locale.getencoding() is missing, use the Python filesystem
559+ # encoding.
560+ _encoding = sys .getfilesystemencoding ()
639561 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
649563
650564try :
651565 CODESET
@@ -1713,13 +1627,6 @@ def _init_categories(categories=categories):
17131627 _init_categories ()
17141628 del categories ['LC_ALL' ]
17151629
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-
17231630 print ('Locale settings on startup:' )
17241631 print ('-' * 72 )
17251632 for name ,category in categories .items ():
@@ -1729,17 +1636,6 @@ def _init_categories(categories=categories):
17291636 print (' Encoding: ' , enc or '(undefined)' )
17301637 print ()
17311638
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-
17431639 try :
17441640 setlocale (LC_ALL , "" )
17451641 except :
0 commit comments