[jhbuild/pre-3-cleanup: 2/6] Clean up getting the default encoding
- From: Christoph Reiter <creiter src gnome org>
 
- To: commits-list gnome org
 
- Cc: 
 
- Subject: [jhbuild/pre-3-cleanup: 2/6] Clean up getting the default encoding
 
- Date: Sat, 21 Sep 2019 20:08:45 +0000 (UTC)
 
commit 854bf2f85532082934d63914a24861eef5071c8c
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Sat Sep 21 21:03:25 2019 +0200
    Clean up getting the default encoding
    
    darwin defaults to utf-8, even if it fails. Also we shouldn't change
    sys.platform at runtime, who knows what that breaks.
 jhbuild/main.py | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
---
diff --git a/jhbuild/main.py b/jhbuild/main.py
index 35777771..418e397e 100644
--- a/jhbuild/main.py
+++ b/jhbuild/main.py
@@ -23,6 +23,7 @@ import sys, os, errno
 import optparse
 import traceback
 import logging
+import locale
 
 import gettext
 import __builtin__
@@ -36,23 +37,22 @@ from jhbuild.moduleset import warn_local_modulesets
 from jhbuild.utils.compat import text_type
 
 
-if sys.platform == 'darwin':
-    # work around locale.getpreferredencoding() returning an empty string in
-    # Mac OS X, see http://bugzilla.gnome.org/show_bug.cgi?id=534650 and
-    # http://bazaar-vcs.org/DarwinCommandLineArgumentDecoding
-    sys.platform = 'posix'
+def _get_encoding():
     try:
-        import locale
-    finally:
-        sys.platform = 'darwin'
-else:
-    import locale
-
-try:
-    _encoding = locale.getpreferredencoding()
-    assert _encoding
-except (locale.Error, AssertionError):
-    _encoding = 'ascii'
+        encoding = locale.getpreferredencoding()
+    except locale.Error:
+        encoding = ""
+    if not encoding:
+        # work around locale.getpreferredencoding() returning an empty string in
+        # Mac OS X, see http://bugzilla.gnome.org/show_bug.cgi?id=534650
+        if sys.platform == "darwin":
+            encoding = "utf-8"
+        else:
+            encoding = "ascii"
+    return encoding
+
+_encoding = _get_encoding()
+
 
 def uencode(s):
     if isinstance(s, text_type):
[
Date Prev][
Date Next]   [
Thread Prev][
Thread Next]   
[
Thread Index]
[
Date Index]
[
Author Index]