[gtk-doc] common: more hacks to avoid UnicodeErrors in print()



commit 0cc67bd997d472b9f0a95763fef30aed11b8e6dd
Author: Stefan Sauer <ensonic users sf net>
Date:   Mon Dec 4 17:04:18 2017 +0100

    common: more hacks to avoid UnicodeErrors in print()
    
    Handle cases similar to LC_ALL=C.
    Fixes https://bugzilla.gnome.org/show_bug.cgi?id=791131

 gtkdoc/common.py |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)
---
diff --git a/gtkdoc/common.py b/gtkdoc/common.py
index ca82263..126aada 100644
--- a/gtkdoc/common.py
+++ b/gtkdoc/common.py
@@ -70,10 +70,15 @@ def setup_logging():
         logging.basicConfig(stream=sys.stdout,
                             level=logging.getLevelName(log_level.upper()),
                             
format='%(asctime)s:%(filename)s:%(funcName)s:%(lineno)d:%(levelname)s:%(message)s')
-    # When redirecting the output on python2 we get UnicodeEncodeError:
-    if not sys.stdout.encoding:
-        import codecs
-        sys.stdout = codecs.getwriter('utf8')(sys.stdout)
+    # When redirecting the output on python2 or if run with a non utf-8 locale
+    # we get UnicodeEncodeError:
+    encoding = sys.stdout.encoding
+    if 'PYTHONIOENCODING' not in os.environ and (not encoding or encoding != 'UTF-8'):
+        if six.PY3:
+            sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)
+        else:
+            import codecs
+            sys.stdout = codecs.getwriter('utf8')(sys.stdout)
 
 
 def UpdateFileIfChanged(old_file, new_file, make_backup):
@@ -148,7 +153,7 @@ def LogWarning(filename, line, message):
     filename = filename or "unknown"
 
     # TODO: write to stderr
-    print ("%s:%d: warning: %s" % (filename, line, message))
+    print("%s:%d: warning: %s" % (filename, line, message))
 
 
 def CreateValidSGMLID(xml_id):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]