From a43df5d7f101297396ff5ceacdccdb179772a0b2 Mon Sep 17 00:00:00 2001 From: xmo-odoo Date: Thu, 12 Feb 2026 15:21:03 +0100 Subject: [PATCH] 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. --- babel/localedata.py | 3 ++- tests/test_localedata.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/babel/localedata.py b/babel/localedata.py index 2b225a142..4648e6626 100644 --- a/babel/localedata.py +++ b/babel/localedata.py @@ -60,6 +60,7 @@ def resolve_locale_filename(name: os.PathLike[str] | str) -> str: return os.path.join(_dirname, f"{name}.dat") +@lru_cache(maxsize=None) def exists(name: str) -> bool: """Check whether locale data is available for the given locale. @@ -72,7 +73,7 @@ def exists(name: str) -> bool: if name in _cache: return True file_found = os.path.exists(resolve_locale_filename(name)) - return True if file_found else bool(normalize_locale(name)) + return file_found or bool(normalize_locale(name)) @lru_cache(maxsize=None) diff --git a/tests/test_localedata.py b/tests/test_localedata.py index 03cbed1dc..42810b992 100644 --- a/tests/test_localedata.py +++ b/tests/test_localedata.py @@ -109,10 +109,10 @@ def test_locale_argument_acceptance(): assert normalized_locale is None assert not localedata.exists(None) - # Testing list input. + # Testing tuple input. normalized_locale = localedata.normalize_locale(['en_us', None]) assert normalized_locale is None - assert not localedata.exists(['en_us', None]) + assert not localedata.exists(('en_us', None)) def test_locale_identifiers_cache(monkeypatch):