From fd4722cacf4885c29d358b8de6718b51a8149fa3 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sat, 12 Oct 2013 00:13:50 +0200 Subject: Issue #9548: Add a minimal "_bootlocale" module that is imported by the _io module instead of the full locale module. --- Lib/locale.py | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) (limited to 'Lib/locale.py') diff --git a/Lib/locale.py b/Lib/locale.py index d2a885d519f..2e82c952ace 100644 --- a/Lib/locale.py +++ b/Lib/locale.py @@ -554,8 +554,8 @@ def resetlocale(category=LC_ALL): # On Win32, this will return the ANSI code page def getpreferredencoding(do_setlocale = True): """Return the charset that the user is likely using.""" - import _locale - return _locale._getdefaultlocale()[1] + import _bootlocale + return _bootlocale.getpreferredencoding(False) else: # On Unix, if CODESET is available, use that. try: @@ -574,27 +574,16 @@ def getpreferredencoding(do_setlocale = True): def getpreferredencoding(do_setlocale = True): """Return the charset that the user is likely using, according to the system configuration.""" + import _bootlocale if do_setlocale: oldloc = setlocale(LC_CTYPE) try: setlocale(LC_CTYPE, "") except Error: pass - result = nl_langinfo(CODESET) - if not result and sys.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' + result = _bootlocale.getpreferredencoding(False) + if do_setlocale: setlocale(LC_CTYPE, oldloc) - else: - result = nl_langinfo(CODESET) - if not result and sys.platform == 'darwin': - # See above for explanation - result = 'UTF-8' return result -- cgit v1.2.3-65-gdbad