From 867b5e6f902e945e8a02a3d2d35d698e19f8f892 Mon Sep 17 00:00:00 2001 From: Christoph Reiter Date: Wed, 26 Jul 2017 11:26:00 +0200 Subject: [PATCH] glib-mkenums: Python2: use locale encoding when redirecting stdout In case of Python 2 and stdout being redirected to a file, sys.stdout.encoding is None and it defaults ASCII for encoding text. To match the behaviour of Python 3, which uses the locale encoding also when redirecting to a file, wrap sys.stdout with a StreamWriter using the locale encoding. https://bugzilla.gnome.org/show_bug.cgi?id=785113 --- gobject/glib-mkenums.in | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/gobject/glib-mkenums.in b/gobject/glib-mkenums.in index fccc00003..9fae3a8a9 100755 --- a/gobject/glib-mkenums.in +++ b/gobject/glib-mkenums.in @@ -16,6 +16,8 @@ import sys import tempfile import io import errno +import codecs +import locale VERSION_STR = '''glib-mkenums version @VERSION@ glib-genmarshal comes with ABSOLUTELY NO WARRANTY. @@ -24,7 +26,13 @@ the GNU General Public License which can be found in the GLib source package. Sources, examples and contact information are available at http://www.gtk.org''' -output_stream = sys.stdout +# Python 2 defaults to ASCII in case stdout is redirected. +# This should make it match Python 3, which uses the locale encoding. +if sys.stdout.encoding is None: + output_stream = codecs.getwriter( + locale.getpreferredencoding())(sys.stdout) +else: + output_stream = sys.stdout # pylint: disable=too-few-public-methods class Color: -- 2.11.4.GIT