Home | History | Annotate | Download | only in Lib
      1 """A minimal subset of the locale module used at interpreter startup
      2 (imported by the _io module), in order to reduce startup time.
      3 
      4 Don't import directly from third-party code; use the `locale` module instead!
      5 """
      6 
      7 import sys
      8 import _locale
      9 
     10 if sys.platform.startswith("win"):
     11     def getpreferredencoding(do_setlocale=True):
     12         return _locale._getdefaultlocale()[1]
     13 else:
     14     try:
     15         _locale.CODESET
     16     except AttributeError:
     17         def getpreferredencoding(do_setlocale=True):
     18             # This path for legacy systems needs the more complex
     19             # getdefaultlocale() function, import the full locale module.
     20             import locale
     21             return locale.getpreferredencoding(do_setlocale)
     22     else:
     23         def getpreferredencoding(do_setlocale=True):
     24             assert not do_setlocale
     25             result = _locale.nl_langinfo(_locale.CODESET)
     26             if not result and sys.platform == 'darwin':
     27                 # nl_langinfo can return an empty string
     28                 # when the setting has an invalid value.
     29                 # Default to UTF-8 in that case because
     30                 # UTF-8 is the default charset on OSX and
     31                 # returning nothing will crash the
     32                 # interpreter.
     33                 result = 'UTF-8'
     34             return result
     35