Skip to content

Commit 94a3694

Browse files
authored
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 6d77e07)
1 parent f2e894c commit 94a3694

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Lib/locale.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,22 @@ def getpreferredencoding(do_setlocale = True):
617617
except Error:
618618
pass
619619
result = nl_langinfo(CODESET)
620+
if not result and sys.platform == 'darwin':
621+
# nl_langinfo can return an empty string
622+
# when the setting has an invalid value.
623+
# Default to UTF-8 in that case because
624+
# UTF-8 is the default charset on OSX and
625+
# returning nothing will crash the
626+
# interpreter.
627+
result = 'UTF-8'
628+
620629
setlocale(LC_CTYPE, oldloc)
621630
return result
622631
else:
623-
return nl_langinfo(CODESET)
632+
result = nl_langinfo(CODESET)
633+
if not result and sys.platform == 'darwin':
634+
# See above for explanation
635+
result = 'UTF-8'
624636

625637

626638
### Database

0 commit comments

Comments
 (0)