Skip to content

Commit 0278582

Browse files
committed
Teach Locale.parse to share the Locale cache
1 parent 07b824f commit 0278582

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

babel/core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ def parse(cls, identifier, sep='_', resolve_likely_subtags=True):
278278
raise TypeError('Unxpected value for identifier: %r' % (identifier,))
279279

280280
parts = parse_locale(identifier, sep=sep)
281+
282+
if parts in cls._cache: # We've loaded this one before.
283+
return cls._cache[parts]
284+
281285
input_id = get_locale_identifier(parts)
282286

283287
def _try_load(parts):

tests/test_core.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ def test_locale_caching():
105105
assert len(Locale._cache) == 6 # Cache GET!
106106

107107

108+
def test_locale_cache_shared_by_parse():
109+
# Test that Locale.parse() shares the cache and doesn't do (much)
110+
# extra work loading locales.
111+
112+
# Put a dummy object into the cache...
113+
en_US_cache_key = ('en', 'US', None, None)
114+
dummy = object()
115+
Locale._cache[en_US_cache_key] = dummy
116+
117+
try:
118+
assert Locale.parse("en^US", sep="^") is dummy # That's a weird separator, man!
119+
finally:
120+
# Now purge our silliness (even in case this test failed)
121+
Locale._cache.clear()
122+
108123

109124
class TestLocaleClass:
110125
def test_attributes(self):

0 commit comments

Comments
 (0)