blob: 4bccac1139f0bf48f819de54207339882c115b69 [file] [log] [blame]
Antoine Pitroufd4722c2013-10-12 00:13:50 +02001"""A minimal subset of the locale module used at interpreter startup
2(imported by the _io module), in order to reduce startup time.
3
4Don't import directly from third-party code; use the `locale` module instead!
5"""
6
7import sys
8import _locale
9
10if sys.platform.startswith("win"):
11 def getpreferredencoding(do_setlocale=True):
12 return _locale._getdefaultlocale()[1]
13else:
14 try:
15 _locale.CODESET
Antoine Pitrou4aa8f8e2013-10-12 15:00:44 +020016 except AttributeError:
Antoine Pitroufd4722c2013-10-12 00:13:50 +020017 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