From 604cd0d4b48f2b80eac23def53fb5c2aa52a196c Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Tue, 18 Apr 2017 21:02:58 -0700 Subject: [PATCH] Added a try-catch for invalid locales on osx --- rtv/__main__.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/rtv/__main__.py b/rtv/__main__.py index 4fd9a01..6c92123 100644 --- a/rtv/__main__.py +++ b/rtv/__main__.py @@ -104,8 +104,18 @@ def main(): logging.root.addHandler(logging.NullHandler()) # Make sure the locale is UTF-8 for unicode support - locale.setlocale(locale.LC_ALL, '') - encoding = locale.getlocale()[1] or locale.getdefaultlocale()[1] + default_locale = locale.setlocale(locale.LC_ALL, '') + 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': text = ('System encoding was detected as (%s) instead of UTF-8' ', falling back to ascii only mode' % encoding)