]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Replace babel.localedata.locale_identifiers cache with LRU cache
authorAarni Koskela <akx@iki.fi>
Tue, 7 Feb 2023 14:00:41 +0000 (16:00 +0200)
committerAarni Koskela <akx@iki.fi>
Tue, 7 Feb 2023 14:17:10 +0000 (16:17 +0200)
babel/localedata.py
tests/test_localedata.py

index f765a1ea34b48dda3b04d8a14a034f94b5742a2a..a9c7c75ec62a8c10897646e8fce9eb5c9c8230be 100644 (file)
@@ -20,6 +20,7 @@ import sys
 import threading
 from collections import abc
 from collections.abc import Iterator, Mapping, MutableMapping
+from functools import lru_cache
 from itertools import chain
 from typing import Any
 
@@ -74,28 +75,24 @@ def exists(name: str) -> bool:
     return True if file_found else bool(normalize_locale(name))
 
 
+@lru_cache(maxsize=None)
 def locale_identifiers() -> list[str]:
     """Return a list of all locale identifiers for which locale data is
     available.
 
-    This data is cached after the first invocation in `locale_identifiers.cache`.
-
-    Removing the `locale_identifiers.cache` attribute or setting it to `None`
-    will cause this function to re-read the list from disk.
+    This data is cached after the first invocation.
+    You can clear the cache by calling `locale_identifiers.cache_clear()`.
 
     .. versionadded:: 0.8.1
 
     :return: a list of locale identifiers (strings)
     """
-    data = getattr(locale_identifiers, 'cache', None)
-    if data is None:
-        locale_identifiers.cache = data = [
-            stem
-            for stem, extension in
-            (os.path.splitext(filename) for filename in os.listdir(_dirname))
-            if extension == '.dat' and stem != 'root'
-        ]
-    return data
+    return [
+        stem
+        for stem, extension in
+        (os.path.splitext(filename) for filename in os.listdir(_dirname))
+        if extension == '.dat' and stem != 'root'
+    ]
 
 
 def load(name: os.PathLike[str] | str, merge_inherited: bool = True) -> dict[str, Any]:
index 913922ee4935fff61181d5b51928a0d79f3cf1e2..75c34b19b9ce19ee0b567874e159af7d9249cdb8 100644 (file)
@@ -110,18 +110,15 @@ def test_locale_identifiers_cache(monkeypatch):
         rv = original_listdir(*args)
         listdir_calls.append((args, rv))
         return rv
-    monkeypatch.setattr(localedata.os, 'listdir', listdir_spy)
-
-    # In case we've already run some tests...
-    if hasattr(localedata.locale_identifiers, 'cache'):
-        del localedata.locale_identifiers.cache
 
+    monkeypatch.setattr(localedata.os, 'listdir', listdir_spy)
+    localedata.locale_identifiers.cache_clear()
     assert not listdir_calls
-    assert localedata.locale_identifiers()
+    l = localedata.locale_identifiers()
     assert len(listdir_calls) == 1
-    assert localedata.locale_identifiers() is localedata.locale_identifiers.cache
+    assert localedata.locale_identifiers() is l
     assert len(listdir_calls) == 1
-    localedata.locale_identifiers.cache = None
+    localedata.locale_identifiers.cache_clear()
     assert localedata.locale_identifiers()
     assert len(listdir_calls) == 2