Merge pull request #360 from michael-lazar/issue_359

Added a try-catch for invalid locales on osx
This commit is contained in:
Michael Lazar
2017-04-18 21:12:30 -07:00
committed by GitHub

View File

@@ -104,8 +104,18 @@ def main():
logging.root.addHandler(logging.NullHandler()) logging.root.addHandler(logging.NullHandler())
# Make sure the locale is UTF-8 for unicode support # Make sure the locale is UTF-8 for unicode support
locale.setlocale(locale.LC_ALL, '') default_locale = locale.setlocale(locale.LC_ALL, '')
encoding = locale.getlocale()[1] or locale.getdefaultlocale()[1] try:
encoding = locale.getlocale()[1] or locale.getdefaultlocale()[1]
except ValueError:
# http://stackoverflow.com/a/19961403
# OS X on some terminals will set the LC_CTYPE to "UTF-8"
# (as opposed to something like "en_US.UTF-8") and python
# doesn't know how to handle it.
_logger.warning('Error parsing system locale: `%s`,'
' falling back to utf-8', default_locale)
encoding = 'UTF-8'
if not encoding or encoding.lower() != 'utf-8': if not encoding or encoding.lower() != 'utf-8':
text = ('System encoding was detected as (%s) instead of UTF-8' text = ('System encoding was detected as (%s) instead of UTF-8'
', falling back to ascii only mode' % encoding) ', falling back to ascii only mode' % encoding)