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
This commit is contained in:
Christoph Reiter 2017-07-26 11:26:00 +02:00
parent a7926117dd
commit 867b5e6f90

View File

@ -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,6 +26,12 @@ 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'''
# 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