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