commit 0e94f292cda632153f2b3d9a9037eb0141ae9c2e
Author: Lorenzo Peña <lorinkoz@gmail.com>
Date:   Tue Jul 23 12:06:29 2024 +0200

    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.

Index: python-django-debian.git/django/utils/translation/trans_real.py
===================================================================
--- python-django-debian.git.orig/django/utils/translation/trans_real.py
+++ python-django-debian.git/django/utils/translation/trans_real.py
@@ -493,7 +493,7 @@ def get_supported_language_variant(lang_
                 # There is a generic variant under the maximum length accepted length.
                 lang_code = lang_code[:index]
             else:
-                raise ValueError("'lang_code' exceeds the maximum accepted length")
+                raise LookupError(lang_code)
         # If 'fr-ca' is not supported, try special fallback or language-only 'fr'.
         possible_lang_codes = [lang_code]
         try:
Index: python-django-debian.git/docs/ref/utils.txt
===================================================================
--- python-django-debian.git.orig/docs/ref/utils.txt
+++ python-django-debian.git/docs/ref/utils.txt
@@ -1130,7 +1130,7 @@ For a complete discussion on the usage o
     ``'es-ar'`` isn't.
 
     ``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``
     is ``False``.
 
@@ -1142,10 +1142,10 @@ For a complete discussion on the usage o
 
     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
-        processed without raising a :exc:`ValueError`.
+        processed without raising a :exc:`LookupError`.
 
 .. function:: to_locale(language)
 
Index: python-django-debian.git/tests/i18n/tests.py
===================================================================
--- python-django-debian.git.orig/tests/i18n/tests.py
+++ python-django-debian.git/tests/i18n/tests.py
@@ -1533,14 +1533,13 @@ class MiscTests(SimpleTestCase):
             g('xyz')
         with self.assertRaises(LookupError):
             g('xy-zz')
-        msg = "'lang_code' exceeds the maximum accepted length"
         with self.assertRaises(LookupError):
             g("x" * LANGUAGE_CODE_MAX_LENGTH)
-        with self.assertRaisesMessage(ValueError, msg):
+        with self.assertRaises(LookupError):
             g("x" * (LANGUAGE_CODE_MAX_LENGTH + 1))
         # 167 * 3 = 501 which is LANGUAGE_CODE_MAX_LENGTH + 1.
         self.assertEqual(g("en-" * 167), "en")
-        with self.assertRaisesMessage(ValueError, msg):
+        with self.assertRaises(LookupError):
             g("en-" * 167, strict=True)
         self.assertEqual(g("en-" * 30000), "en")  # catastrophic test
 
@@ -1579,6 +1578,7 @@ class MiscTests(SimpleTestCase):
         self.assertEqual(g('/de-at/'), 'de-at')
         self.assertEqual(g('/de-ch/'), 'de')
         self.assertIsNone(g('/de-simple-page/'))
+        self.assertIsNone(g(f"/{'a' * 501}/"))
 
     def test_get_language_from_path_null(self):
         g = trans_null.get_language_from_path
@@ -1866,6 +1866,11 @@ class CountrySpecificLanguageTests(Simpl
         lang = get_language_from_request(r)
         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):
         lang = trans_null.get_language_from_request(None)
         self.assertEqual(lang, 'en')
