summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom <tom@tomforb.es>2017-09-19 23:16:48 +0100
committerTim Graham <timograham@gmail.com>2017-10-02 14:07:28 -0400
commitf1c007bbf2fcd4996e29f0482c32faf5df397aa0 (patch)
treea21971e9f21103d6f58fff5b601b13dc4ea5c18b
parentf04e6732c33e0fd67e82ab2d59f230fa0045d15b (diff)
Fixed #28642 -- Added caching to parse_accept_lang_header().
-rw-r--r--django/utils/translation/trans_real.py11
-rw-r--r--tests/i18n/tests.py2
2 files changed, 7 insertions, 6 deletions
diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py
index 9ec8085dae..1b2472cc10 100644
--- a/django/utils/translation/trans_real.py
+++ b/django/utils/translation/trans_real.py
@@ -509,25 +509,26 @@ def get_language_from_request(request, check_path=False):
return settings.LANGUAGE_CODE
+@functools.lru_cache(maxsize=1000)
def parse_accept_lang_header(lang_string):
"""
Parse the lang_string, which is the body of an HTTP Accept-Language
- header, and return a list of (lang, q-value), ordered by 'q' values.
+ header, and return a tuple of (lang, q-value), ordered by 'q' values.
- Return an empty list if there are any format errors in lang_string.
+ Return an empty tuple if there are any format errors in lang_string.
"""
result = []
pieces = accept_language_re.split(lang_string.lower())
if pieces[-1]:
- return []
+ return tuple()
for i in range(0, len(pieces) - 1, 3):
first, lang, priority = pieces[i:i + 3]
if first:
- return []
+ return tuple()
if priority:
priority = float(priority)
else:
priority = 1.0
result.append((lang, priority))
result.sort(key=lambda k: k[1], reverse=True)
- return result
+ return tuple(result)
diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py
index 57adaae6a9..dfc2a0985e 100644
--- a/tests/i18n/tests.py
+++ b/tests/i18n/tests.py
@@ -1133,7 +1133,7 @@ class MiscTests(SimpleTestCase):
]
for value, expected in tests:
with self.subTest(value=value):
- self.assertEqual(trans_real.parse_accept_lang_header(value), expected)
+ self.assertEqual(trans_real.parse_accept_lang_header(value), tuple(expected))
def test_parse_literal_http_header(self):
"""