Skip to content

Commit

Permalink
Fixes #35627 -- Made callers of get_supported_language_variant also h…
Browse files Browse the repository at this point in the history
…andle ValueError
  • Loading branch information
lorinkoz committed Jul 23, 2024
1 parent cf03aa4 commit 30c77c1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
8 changes: 4 additions & 4 deletions django/utils/translation/trans_real.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 555,7 @@ def get_language_from_path(path, strict=False):
lang_code = regex_match[1]
try:
return get_supported_language_variant(lang_code, strict=strict)
except LookupError:
except (LookupError, ValueError):
return None


Expand Down Expand Up @@ -584,7 584,7 @@ def get_language_from_request(request, check_path=False):

try:
return get_supported_language_variant(lang_code)
except LookupError:
except (LookupError, ValueError):
pass

accept = request.META.get("HTTP_ACCEPT_LANGUAGE", "")
Expand All @@ -597,12 597,12 @@ def get_language_from_request(request, check_path=False):

try:
return get_supported_language_variant(accept_lang)
except LookupError:
except (LookupError, ValueError):
continue

try:
return get_supported_language_variant(settings.LANGUAGE_CODE)
except LookupError:
except (LookupError, ValueError):
return settings.LANGUAGE_CODE


Expand Down
13 changes: 13 additions & 0 deletions docs/releases/4.2.15.txt
Original file line number Diff line number Diff line change
@@ -0,0 1,13 @@
==========================
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 where a long initial path fragment in the
URL would raise ``ValueError`` from the ``LocaleMiddleware``.
3 changes: 3 additions & 0 deletions docs/releases/5.0.8.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 15,6 @@ Bugfixes
* Fixed a regression in Django 5.0 where ``ModelAdmin.action_checkbox`` could
break the admin changelist HTML page when rendering a model instance with a
``__html__`` method (:ticket:`35606`).

* Fixed a regression in Django 5.0.7 where a long initial path fragment in the
URL would raise ``ValueError`` from the ``LocaleMiddleware``.
1 change: 1 addition & 0 deletions docs/releases/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 55,7 @@ versions of the documentation contain the release notes for any later releases.
.. toctree::
:maxdepth: 1

4.2.15
4.2.14
4.2.13
4.2.12
Expand Down
11 changes: 11 additions & 0 deletions tests/i18n/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 1716,7 @@ def test_get_supported_language_variant_null(self):
)
def test_get_language_from_path_real(self):
g = trans_real.get_language_from_path
long_string = "a" * 501
tests = [
("/pl/", "pl"),
("/pl", "pl"),
Expand All @@ -1734,6 1735,7 @@ def test_get_language_from_path_real(self):
("/i-mingo/", "i-mingo"),
("/kl-tunumiit/", "kl-tunumiit"),
("/nan-hani-tw/", "nan-hani-tw"),
(f"/{long_string}/", None),
]
for path, language in tests:
with self.subTest(path=path):
Expand Down Expand Up @@ -2003,6 2005,15 @@ def test_get_language_from_request(self):
lang = get_language_from_request(request)
self.assertEqual("en-us", lang)

# issue 35627
long_string = "a" * 501
request = self.rf.get(
"/",
headers={"accept-language": f"{long_string},en;q=0.8,bg;q=0.6,ru;q=0.4"},
)
lang = get_language_from_request(request)
self.assertEqual("en-us", lang)

request = self.rf.get(
"/", headers={"accept-language": "bg-bg,en-US;q=0.8,en;q=0.6,ru;q=0.4"}
)
Expand Down

0 comments on commit 30c77c1

Please sign in to comment.