From e7ba4c03df2b97ae6d661d9d4ba3309981e74f5e Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Tue, 18 Jun 2019 20:29:03 -0400 Subject: [PATCH] ValueError unknown locale error fix (#128) --- apprise/AppriseLocale.py | 11 +++++++++++ test/test_locale.py | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/apprise/AppriseLocale.py b/apprise/AppriseLocale.py index 714d2016..714e1180 100644 --- a/apprise/AppriseLocale.py +++ b/apprise/AppriseLocale.py @@ -30,6 +30,7 @@ import contextlib from os.path import join from os.path import dirname from os.path import abspath +from .logger import logger # Define our translation domain DOMAIN = 'apprise' @@ -207,6 +208,16 @@ class AppriseLocale(object): # Detect language lang = locale.getdefaultlocale()[0] + except ValueError as e: + # This occurs when an invalid locale was parsed from the + # environment variable. While we still return None in this + # case, we want to better notify the end user of this. Users + # receiving this error should check their environment + # variables. + logger.warning( + 'Language detection failure / {}'.format(str(e))) + return None + except TypeError: # None is returned if the default can't be determined # we're done in this case diff --git a/test/test_locale.py b/test/test_locale.py index ce2fcc8d..90831718 100644 --- a/test/test_locale.py +++ b/test/test_locale.py @@ -23,6 +23,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +import os import mock import ctypes @@ -169,6 +170,16 @@ def test_detect_language_windows_users(): # Detect french language assert AppriseLocale.AppriseLocale.detect_language() == 'fr' + # The following unsets all enviroment vaiables and sets LC_CTYPE + # This was causing Python 2.7 to internally parse UTF-8 as an invalid + # locale and throw an uncaught ValueError + with environ(*list(os.environ.keys()), LC_CTYPE="UTF-8"): + assert AppriseLocale.AppriseLocale.detect_language() is None + + # Test with absolutely no environment variables what-so-ever + with environ(*list(os.environ.keys())): + assert AppriseLocale.AppriseLocale.detect_language() is None + # Tidy delattr(ctypes, 'windll')