Skip to content

Commit 6d77e07

Browse files
Fix for issue 6393: Python crashes on OSX when $LANG is set to some (but
not all) invalid values due to an invalid result from nl_langinfo
1 parent 5bbab3e commit 6d77e07

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
@@ -570,10 +570,22 @@ def getpreferredencoding(do_setlocale = True):
570570
except Error:
571571
pass
572572
result = nl_langinfo(CODESET)
573+
if not result and sys.platform == 'darwin':
574+
# nl_langinfo can return an empty string
575+
# when the setting has an invalid value.
576+
# Default to UTF-8 in that case because
577+
# UTF-8 is the default charset on OSX and
578+
# returning nothing will crash the
579+
# interpreter.
580+
result = 'UTF-8'
581+
573582
setlocale(LC_CTYPE, oldloc)
574583
return result
575584
else:
576-
return nl_langinfo(CODESET)
585+
result = nl_langinfo(CODESET)
586+
if not result and sys.platform == 'darwin':
587+
# See above for explanation
588+
result = 'UTF-8'
577589

578590

579591
### Database

0 commit comments

Comments
 (0)