ValueError unknown locale error fix (#128)

This commit is contained in:
Chris Caron 2019-06-18 20:29:03 -04:00 committed by GitHub
parent 92523f191b
commit e7ba4c03df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -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

View File

@ -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')