Skip to content

Commit f802075

Browse files
committed
Replace babel.localedata.locale_identifiers cache with LRU cache
1 parent 1847661 commit f802075

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

babel/localedata.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import threading
2121
from collections import abc
2222
from collections.abc import Iterator, Mapping, MutableMapping
23+
from functools import lru_cache
2324
from itertools import chain
2425
from typing import Any
2526

@@ -74,28 +75,24 @@ def exists(name: str) -> bool:
7475
return True if file_found else bool(normalize_locale(name))
7576

7677

78+
@lru_cache(maxsize=None)
7779
def locale_identifiers() -> list[str]:
7880
"""Return a list of all locale identifiers for which locale data is
7981
available.
8082
81-
This data is cached after the first invocation in `locale_identifiers.cache`.
82-
83-
Removing the `locale_identifiers.cache` attribute or setting it to `None`
84-
will cause this function to re-read the list from disk.
83+
This data is cached after the first invocation.
84+
You can clear the cache by calling `locale_identifiers.cache_clear()`.
8585
8686
.. versionadded:: 0.8.1
8787
8888
:return: a list of locale identifiers (strings)
8989
"""
90-
data = getattr(locale_identifiers, 'cache', None)
91-
if data is None:
92-
locale_identifiers.cache = data = [
93-
stem
94-
for stem, extension in
95-
(os.path.splitext(filename) for filename in os.listdir(_dirname))
96-
if extension == '.dat' and stem != 'root'
97-
]
98-
return data
90+
return [
91+
stem
92+
for stem, extension in
93+
(os.path.splitext(filename) for filename in os.listdir(_dirname))
94+
if extension == '.dat' and stem != 'root'
95+
]
9996

10097

10198
def load(name: os.PathLike[str] | str, merge_inherited: bool = True) -> dict[str, Any]:

tests/test_localedata.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,15 @@ def listdir_spy(*args):
110110
rv = original_listdir(*args)
111111
listdir_calls.append((args, rv))
112112
return rv
113-
monkeypatch.setattr(localedata.os, 'listdir', listdir_spy)
114-
115-
# In case we've already run some tests...
116-
if hasattr(localedata.locale_identifiers, 'cache'):
117-
del localedata.locale_identifiers.cache
118113

114+
monkeypatch.setattr(localedata.os, 'listdir', listdir_spy)
115+
localedata.locale_identifiers.clear_cache()
119116
assert not listdir_calls
120-
assert localedata.locale_identifiers()
117+
l = localedata.locale_identifiers()
121118
assert len(listdir_calls) == 1
122-
assert localedata.locale_identifiers() is localedata.locale_identifiers.cache
119+
assert localedata.locale_identifiers() is l
123120
assert len(listdir_calls) == 1
124-
localedata.locale_identifiers.cache = None
121+
localedata.locale_identifiers.clear_cache()
125122
assert localedata.locale_identifiers()
126123
assert len(listdir_calls) == 2
127124

0 commit comments

Comments
 (0)