Skip to content

Commit 2b81a40

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.
1 parent 56c63ca commit 2b81a40

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
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)

0 commit comments

Comments
 (0)