- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy path_bootlocale.py
46 lines (43 loc) · 1.76 KB
/
_bootlocale.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""A minimal subset of the locale module used at interpreter startup
(imported by the _io module), in order to reduce startup time.
Don't import directly from third-party code; use the `locale` module instead!
"""
importsys
import_locale
ifsys.platform.startswith("win"):
defgetpreferredencoding(do_setlocale=True):
ifsys.flags.utf8_mode:
return'UTF-8'
return_locale._getdefaultlocale()[1]
else:
try:
_locale.CODESET
exceptAttributeError:
ifhasattr(sys, 'getandroidapilevel'):
# On Android langinfo.h and CODESET are missing, and UTF-8 is
# always used in mbstowcs() and wcstombs().
defgetpreferredencoding(do_setlocale=True):
return'UTF-8'
else:
defgetpreferredencoding(do_setlocale=True):
ifsys.flags.utf8_mode:
return'UTF-8'
# This path for legacy systems needs the more complex
# getdefaultlocale() function, import the full locale module.
importlocale
returnlocale.getpreferredencoding(do_setlocale)
else:
defgetpreferredencoding(do_setlocale=True):
assertnotdo_setlocale
ifsys.flags.utf8_mode:
return'UTF-8'
result=_locale.nl_langinfo(_locale.CODESET)
ifnotresultandsys.platform=='darwin':
# nl_langinfo can return an empty string
# when the setting has an invalid value.
# Default to UTF-8 in that case because
# UTF-8 is the default charset on OSX and
# returning nothing will crash the
# interpreter.
result='UTF-8'
returnresult