summaryrefslogtreecommitdiff
path: root/django/utils/encoding.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/utils/encoding.py')
-rw-r--r--django/utils/encoding.py22
1 files changed, 14 insertions, 8 deletions
diff --git a/django/utils/encoding.py b/django/utils/encoding.py
index 607b48277a..efc4eccaf9 100644
--- a/django/utils/encoding.py
+++ b/django/utils/encoding.py
@@ -236,11 +236,17 @@ def filepath_to_uri(path):
# some flexibility for hardcoding separators.
return quote(force_bytes(path.replace("\\", "/")), safe=b"/~!*()'")
-# The encoding of the default system locale but falls back to the
-# given fallback encoding if the encoding is unsupported by python or could
-# not be determined. See tickets #10335 and #5846
-try:
- DEFAULT_LOCALE_ENCODING = locale.getdefaultlocale()[1] or 'ascii'
- codecs.lookup(DEFAULT_LOCALE_ENCODING)
-except:
- DEFAULT_LOCALE_ENCODING = 'ascii'
+def get_system_encoding():
+ """
+ The encoding of the default system locale but falls back to the given
+ fallback encoding if the encoding is unsupported by python or could
+ not be determined. See tickets #10335 and #5846
+ """
+ try:
+ encoding = locale.getdefaultlocale()[1] or 'ascii'
+ codecs.lookup(encoding)
+ except Exception:
+ encoding = 'ascii'
+ return encoding
+
+DEFAULT_LOCALE_ENCODING = get_system_encoding()