]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Cache validity of a locale name (#1254)
authorxmo-odoo <xmo@odoo.com>
Sat, 11 Apr 2026 15:49:38 +0000 (17:49 +0200)
committerGitHub <noreply@github.com>
Sat, 11 Apr 2026 15:49:38 +0000 (18:49 +0300)
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.

babel/localedata.py
tests/test_localedata.py

index 2b225a142adf85ff790fbd0afaba4d032e6f92a9..4648e66266c93616c42a1a9707fc87dd609825bf 100644 (file)
@@ -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)
index 03cbed1dcc91ca077926a6b426c1f7f40456cd80..42810b992b46369876d7f963d0b69b02f788c0ac 100644 (file)
@@ -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):