summaryrefslogtreecommitdiff
path: root/django/utils/translation/trans_real.py
diff options
context:
space:
mode:
authorMads Jensen <mje@inducks.org>2017-03-09 16:17:41 +0100
committerTim Graham <timograham@gmail.com>2017-06-28 14:07:55 -0400
commit550cb3a365dee4edfdd1563224d5304de2a57fda (patch)
treefb532f38774ff7619edd2a4532c3daae1ee0ac5a /django/utils/translation/trans_real.py
parent43a4835edf32c57eb74c0eb207c276734a34abcf (diff)
Fixed #27818 -- Replaced try/except/pass with contextlib.suppress().
Diffstat (limited to 'django/utils/translation/trans_real.py')
-rw-r--r--django/utils/translation/trans_real.py13
1 files changed, 4 insertions, 9 deletions
diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py
index 6b3aeb127e..f36c21f0a1 100644
--- a/django/utils/translation/trans_real.py
+++ b/django/utils/translation/trans_real.py
@@ -6,6 +6,7 @@ import re
import sys
import warnings
from collections import OrderedDict
+from contextlib import suppress
from threading import local
from django.apps import apps
@@ -256,10 +257,8 @@ def get_language():
"""Return the currently selected language."""
t = getattr(_active, "value", None)
if t is not None:
- try:
+ with suppress(AttributeError):
return t.to_language()
- except AttributeError:
- pass
# If we don't have a real translation object, assume it's the default language.
return settings.LANGUAGE_CODE
@@ -425,10 +424,8 @@ def get_supported_language_variant(lang_code, strict=False):
if lang_code:
# If 'fr-ca' is not supported, try special fallback or language-only 'fr'.
possible_lang_codes = [lang_code]
- try:
+ with suppress(KeyError):
possible_lang_codes.extend(LANG_INFO[lang_code]['fallback'])
- except KeyError:
- pass
generic_lang_code = lang_code.split('-')[0]
possible_lang_codes.append(generic_lang_code)
supported_lang_codes = get_languages()
@@ -486,10 +483,8 @@ def get_language_from_request(request, check_path=False):
lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
- try:
+ with suppress(LookupError):
return get_supported_language_variant(lang_code)
- except LookupError:
- pass
accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '')
for accept_lang, unused in parse_accept_lang_header(accept):