From: Jungmo Ku Date: Tue, 12 Sep 2017 13:25:24 +0000 (-0500) Subject: Allow normalize_locale and exists to handle various unexpected input (#523) X-Git-Tag: v2.5.1~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1903a2bc514f9b5cf2559be65dff965cf5284bd0;p=thirdparty%2Fbabel.git Allow normalize_locale and exists to handle various unexpected input (#523) Resolves #521 --- diff --git a/babel/localedata.py b/babel/localedata.py index 46582212..ae215478 100644 --- a/babel/localedata.py +++ b/babel/localedata.py @@ -18,7 +18,7 @@ from collections import MutableMapping from itertools import chain import sys -from babel._compat import pickle +from babel._compat import pickle, string_types def get_base_dir(): @@ -41,6 +41,8 @@ def normalize_locale(name): Returns the normalized locale ID string or `None` if the ID is not recognized. """ + if not name or not isinstance(name, string_types): + return None name = name.strip().lower() for locale_id in chain.from_iterable([_cache, locale_identifiers()]): if name == locale_id.lower(): @@ -54,6 +56,8 @@ def exists(name): :param name: the locale identifier string """ + if not name or not isinstance(name, string_types): + return False if name in _cache: return True file_found = os.path.exists(os.path.join(_dirname, '%s.dat' % name)) diff --git a/tests/test_localedata.py b/tests/test_localedata.py index 62ad93c5..3599b215 100644 --- a/tests/test_localedata.py +++ b/tests/test_localedata.py @@ -16,8 +16,7 @@ import random from operator import methodcaller import sys -from babel import localedata - +from babel import localedata, numbers class MergeResolveTestCase(unittest.TestCase): @@ -80,7 +79,6 @@ def test_locale_identification(): for l in localedata.locale_identifiers(): assert localedata.exists(l) - def test_unique_ids(): # Check all locale IDs are uniques. all_ids = localedata.locale_identifiers() @@ -106,3 +104,16 @@ def test_pi_support_frozen(monkeypatch): def test_pi_support_not_frozen(): assert not getattr(sys, 'frozen', False) assert localedata.get_base_dir().endswith('babel') + +def test_locale_argument_acceptance(): + # Testing None input. + normalized_locale = localedata.normalize_locale(None) + assert normalized_locale == None + locale_exist = localedata.exists(None) + assert locale_exist == False + + # # Testing list input. + normalized_locale = localedata.normalize_locale(['en_us', None]) + assert normalized_locale == None + locale_exist = localedata.exists(['en_us', None]) + assert locale_exist == False