Skip to content

Commit a43df5d

Browse files
committed
Cache validity of a locale name
Currently `_cache` is only populated on `load`, this is an issue with code which do a lot of locale parsing / instantiation but for one reason or an other rarely end up needing to actually load the locale data (e.g. date formatting with non-locale-dependent patterns) because every cache miss is an `os.path.exists`. Cache `exists` separately. Update test because `lru_cache` requires all parameters to be hashasble and a list is not that.
1 parent 56c63ca commit a43df5d

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

babel/localedata.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def resolve_locale_filename(name: os.PathLike[str] | str) -> str:
6060
return os.path.join(_dirname, f"{name}.dat")
6161

6262

63+
@lru_cache(maxsize=None)
6364
def exists(name: str) -> bool:
6465
"""Check whether locale data is available for the given locale.
6566
@@ -72,7 +73,7 @@ def exists(name: str) -> bool:
7273
if name in _cache:
7374
return True
7475
file_found = os.path.exists(resolve_locale_filename(name))
75-
return True if file_found else bool(normalize_locale(name))
76+
return file_found or bool(normalize_locale(name))
7677

7778

7879
@lru_cache(maxsize=None)

tests/test_localedata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ def test_locale_argument_acceptance():
109109
assert normalized_locale is None
110110
assert not localedata.exists(None)
111111

112-
# Testing list input.
112+
# Testing tuple input.
113113
normalized_locale = localedata.normalize_locale(['en_us', None])
114114
assert normalized_locale is None
115-
assert not localedata.exists(['en_us', None])
115+
assert not localedata.exists(('en_us', None))
116116

117117

118118
def test_locale_identifiers_cache(monkeypatch):

0 commit comments

Comments
 (0)