From: xmo-odoo Date: Sat, 11 Apr 2026 15:49:38 +0000 (+0200) Subject: Cache validity of a locale name (#1254) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=326c3bfce88ee53673248fd7b0e5b3af0090a99b;p=thirdparty%2Fbabel.git Cache validity of a locale name (#1254) 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 hashable and a list is not that. --- diff --git a/babel/localedata.py b/babel/localedata.py index 2b225a14..4648e662 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 03cbed1d..42810b99 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):