mirror of
https://github.com/django/django.git
synced 2025-03-28 02:00:45 +00:00
[4.2.x] Fixed #35627 -- Raised a LookupError rather than an unhandled ValueError in get_supported_language_variant().
LocaleMiddleware didn't handle the ValueError raised by get_supported_language_variant() when language codes were over 500 characters. Regression in 9e9792228a6bb5d6402a5d645bc3be4cf364aefb. Backport of 0e94f292cda632153f2b3d9a9037eb0141ae9c2e from main.
This commit is contained in:
parent
c5d196a652
commit
96a3497400
@ -516,7 +516,7 @@ def get_supported_language_variant(lang_code, strict=False):
|
|||||||
# There is a generic variant under the maximum length accepted length.
|
# There is a generic variant under the maximum length accepted length.
|
||||||
lang_code = lang_code[:index]
|
lang_code = lang_code[:index]
|
||||||
else:
|
else:
|
||||||
raise ValueError("'lang_code' exceeds the maximum accepted length")
|
raise LookupError(lang_code)
|
||||||
# If 'zh-hant-tw' is not supported, try special fallback or subsequent
|
# If 'zh-hant-tw' is not supported, try special fallback or subsequent
|
||||||
# language codes i.e. 'zh-hant' and 'zh'.
|
# language codes i.e. 'zh-hant' and 'zh'.
|
||||||
possible_lang_codes = [lang_code]
|
possible_lang_codes = [lang_code]
|
||||||
|
@ -1156,7 +1156,7 @@ For a complete discussion on the usage of the following see the
|
|||||||
``'es-ar'`` isn't.
|
``'es-ar'`` isn't.
|
||||||
|
|
||||||
``lang_code`` has a maximum accepted length of 500 characters. A
|
``lang_code`` has a maximum accepted length of 500 characters. A
|
||||||
:exc:`ValueError` is raised if ``lang_code`` exceeds this limit and
|
:exc:`LookupError` is raised if ``lang_code`` exceeds this limit and
|
||||||
``strict`` is ``True``, or if there is no generic variant and ``strict``
|
``strict`` is ``True``, or if there is no generic variant and ``strict``
|
||||||
is ``False``.
|
is ``False``.
|
||||||
|
|
||||||
@ -1168,10 +1168,10 @@ For a complete discussion on the usage of the following see the
|
|||||||
|
|
||||||
Raises :exc:`LookupError` if nothing is found.
|
Raises :exc:`LookupError` if nothing is found.
|
||||||
|
|
||||||
.. versionchanged:: 4.2.14
|
.. versionchanged:: 4.2.15
|
||||||
|
|
||||||
In older versions, ``lang_code`` values over 500 characters were
|
In older versions, ``lang_code`` values over 500 characters were
|
||||||
processed without raising a :exc:`ValueError`.
|
processed without raising a :exc:`LookupError`.
|
||||||
|
|
||||||
.. function:: to_locale(language)
|
.. function:: to_locale(language)
|
||||||
|
|
||||||
|
14
docs/releases/4.2.15.txt
Normal file
14
docs/releases/4.2.15.txt
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
===========================
|
||||||
|
Django 4.2.15 release notes
|
||||||
|
===========================
|
||||||
|
|
||||||
|
*Expected August 6, 2024*
|
||||||
|
|
||||||
|
Django 4.2.15 fixes a regression in 4.2.14.
|
||||||
|
|
||||||
|
Bugfixes
|
||||||
|
========
|
||||||
|
|
||||||
|
* Fixed a regression in Django 4.2.14 that caused a crash in
|
||||||
|
``LocaleMiddleware`` when processing a language code over 500 characters
|
||||||
|
(:ticket:`35627`).
|
@ -26,6 +26,7 @@ versions of the documentation contain the release notes for any later releases.
|
|||||||
.. toctree::
|
.. toctree::
|
||||||
:maxdepth: 1
|
:maxdepth: 1
|
||||||
|
|
||||||
|
4.2.15
|
||||||
4.2.14
|
4.2.14
|
||||||
4.2.13
|
4.2.13
|
||||||
4.2.12
|
4.2.12
|
||||||
|
@ -1889,14 +1889,13 @@ class MiscTests(SimpleTestCase):
|
|||||||
g("xyz")
|
g("xyz")
|
||||||
with self.assertRaises(LookupError):
|
with self.assertRaises(LookupError):
|
||||||
g("xy-zz")
|
g("xy-zz")
|
||||||
msg = "'lang_code' exceeds the maximum accepted length"
|
|
||||||
with self.assertRaises(LookupError):
|
with self.assertRaises(LookupError):
|
||||||
g("x" * LANGUAGE_CODE_MAX_LENGTH)
|
g("x" * LANGUAGE_CODE_MAX_LENGTH)
|
||||||
with self.assertRaisesMessage(ValueError, msg):
|
with self.assertRaises(LookupError):
|
||||||
g("x" * (LANGUAGE_CODE_MAX_LENGTH + 1))
|
g("x" * (LANGUAGE_CODE_MAX_LENGTH + 1))
|
||||||
# 167 * 3 = 501 which is LANGUAGE_CODE_MAX_LENGTH + 1.
|
# 167 * 3 = 501 which is LANGUAGE_CODE_MAX_LENGTH + 1.
|
||||||
self.assertEqual(g("en-" * 167), "en")
|
self.assertEqual(g("en-" * 167), "en")
|
||||||
with self.assertRaisesMessage(ValueError, msg):
|
with self.assertRaises(LookupError):
|
||||||
g("en-" * 167, strict=True)
|
g("en-" * 167, strict=True)
|
||||||
self.assertEqual(g("en-" * 30000), "en") # catastrophic test
|
self.assertEqual(g("en-" * 30000), "en") # catastrophic test
|
||||||
|
|
||||||
@ -1950,6 +1949,7 @@ class MiscTests(SimpleTestCase):
|
|||||||
("/i-mingo/", "i-mingo"),
|
("/i-mingo/", "i-mingo"),
|
||||||
("/kl-tunumiit/", "kl-tunumiit"),
|
("/kl-tunumiit/", "kl-tunumiit"),
|
||||||
("/nan-hani-tw/", "nan-hani-tw"),
|
("/nan-hani-tw/", "nan-hani-tw"),
|
||||||
|
(f"/{'a' * 501}/", None),
|
||||||
]
|
]
|
||||||
for path, language in tests:
|
for path, language in tests:
|
||||||
with self.subTest(path=path):
|
with self.subTest(path=path):
|
||||||
@ -2228,6 +2228,11 @@ class CountrySpecificLanguageTests(SimpleTestCase):
|
|||||||
lang = get_language_from_request(request)
|
lang = get_language_from_request(request)
|
||||||
self.assertEqual("bg", lang)
|
self.assertEqual("bg", lang)
|
||||||
|
|
||||||
|
def test_get_language_from_request_code_too_long(self):
|
||||||
|
request = self.rf.get("/", headers={"accept-language": "a" * 501})
|
||||||
|
lang = get_language_from_request(request)
|
||||||
|
self.assertEqual("en-us", lang)
|
||||||
|
|
||||||
def test_get_language_from_request_null(self):
|
def test_get_language_from_request_null(self):
|
||||||
lang = trans_null.get_language_from_request(None)
|
lang = trans_null.get_language_from_request(None)
|
||||||
self.assertEqual(lang, "en")
|
self.assertEqual(lang, "en")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user