From: Victor Stinner Date: Fri, 12 May 2017 09:51:38 +0000 (+0200) Subject: bpo-6393: Fix locale.getprerredencoding() on macOS (#1555) X-Git-Tag: v2.7.14rc1~155 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=94a3694c3dda97e3bcb51264bf47d948c5424d84;p=python bpo-6393: Fix locale.getprerredencoding() on macOS (#1555) Fix for bpo-6393: Python crashes on OSX when $LANG is set to some (but not all) invalid values due to an invalid result from nl_langinfo (cherry picked from commit 6d77e07196bfb5dfa4de6f5d80b2619c0643a75e) --- diff --git a/Lib/locale.py b/Lib/locale.py index 5aab163e5a..be34c5ddcd 100644 --- a/Lib/locale.py +++ b/Lib/locale.py @@ -617,10 +617,22 @@ else: 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' + setlocale(LC_CTYPE, oldloc) return result else: - return nl_langinfo(CODESET) + result = nl_langinfo(CODESET) + if not result and sys.platform == 'darwin': + # See above for explanation + result = 'UTF-8' ### Database