]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Allow normalize_locale and exists to handle various unexpected input (#523)
authorJungmo Ku <suhojm@utexas.edu>
Tue, 12 Sep 2017 13:25:24 +0000 (08:25 -0500)
committerAarni Koskela <akx@iki.fi>
Tue, 12 Sep 2017 13:25:24 +0000 (16:25 +0300)
Resolves #521

babel/localedata.py
tests/test_localedata.py

index 46582212dfe82068ca960d16a308f60b5b10c685..ae21547812b0404e22f2ea9ff0b0b74c66cb4a5f 100644 (file)
@@ -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))
index 62ad93c543e65f4583e6a05eb7560f71b33d97bd..3599b2157d4de976bdaddb519349b830da492c44 100644 (file)
@@ -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