Skip to content

Commit 1903a2b

Browse files
suhojmakx
authored andcommitted
Allow normalize_locale and exists to handle various unexpected input (#523)
Resolves #521
1 parent c7729cc commit 1903a2b

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

babel/localedata.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from itertools import chain
1919
import sys
2020

21-
from babel._compat import pickle
21+
from babel._compat import pickle, string_types
2222

2323

2424
def get_base_dir():
@@ -41,6 +41,8 @@ def normalize_locale(name):
4141
Returns the normalized locale ID string or `None` if the ID is not
4242
recognized.
4343
"""
44+
if not name or not isinstance(name, string_types):
45+
return None
4446
name = name.strip().lower()
4547
for locale_id in chain.from_iterable([_cache, locale_identifiers()]):
4648
if name == locale_id.lower():
@@ -54,6 +56,8 @@ def exists(name):
5456
5557
:param name: the locale identifier string
5658
"""
59+
if not name or not isinstance(name, string_types):
60+
return False
5761
if name in _cache:
5862
return True
5963
file_found = os.path.exists(os.path.join(_dirname, '%s.dat' % name))

tests/test_localedata.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
from operator import methodcaller
1717
import sys
1818

19-
from babel import localedata
20-
19+
from babel import localedata, numbers
2120

2221
class MergeResolveTestCase(unittest.TestCase):
2322

@@ -80,7 +79,6 @@ def test_locale_identification():
8079
for l in localedata.locale_identifiers():
8180
assert localedata.exists(l)
8281

83-
8482
def test_unique_ids():
8583
# Check all locale IDs are uniques.
8684
all_ids = localedata.locale_identifiers()
@@ -106,3 +104,16 @@ def test_pi_support_frozen(monkeypatch):
106104
def test_pi_support_not_frozen():
107105
assert not getattr(sys, 'frozen', False)
108106
assert localedata.get_base_dir().endswith('babel')
107+
108+
def test_locale_argument_acceptance():
109+
# Testing None input.
110+
normalized_locale = localedata.normalize_locale(None)
111+
assert normalized_locale == None
112+
locale_exist = localedata.exists(None)
113+
assert locale_exist == False
114+
115+
# # Testing list input.
116+
normalized_locale = localedata.normalize_locale(['en_us', None])
117+
assert normalized_locale == None
118+
locale_exist = localedata.exists(['en_us', None])
119+
assert locale_exist == False

0 commit comments

Comments
 (0)