From: David Coles Date: Wed, 11 Nov 2015 22:06:45 +0000 (-0800) Subject: ceph: Make stdout/stderr always output Unicode (UTF-8) X-Git-Tag: v10.0.1~80^2~7^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F6315%2Fhead;p=ceph.git ceph: Make stdout/stderr always output Unicode (UTF-8) If a stream is not interactive, then under Python 2, then the encoding for stdout/stderr may be None. This means that it's not possible to print Unicode characters since the encoding will fall back to ASCII. This explicitly makes sys.stdout/sys.stderr always use UTF-8 encoding for strings, regardless of the system's local or if the console is interactive or not. This matches the existing tests that assume that output of non-ASCII pool names will be UTF-8 encoded. When outputting raw binary data (such as the CRUSH-map), we must bypass the codec and write directly to raw streams (since the new stream will only accept ASCII byte-strings or Unicode strings). Signed-off-by: David Coles --- diff --git a/src/ceph.in b/src/ceph.in index c6c7c498511b..a366a604a917 100755 --- a/src/ceph.in +++ b/src/ceph.in @@ -19,6 +19,7 @@ License version 2, as published by the Free Software Foundation. See file COPYING. """ +import codecs import os import sys import platform @@ -109,6 +110,12 @@ from ceph_daemon import DaemonWatcher, admin_socket verbose = False cluster_handle = None +# Always use Unicode (UTF-8) for stdout +raw_stdout = sys.__stdout__ +raw_stderr = sys.__stderr__ +sys.stdout = codecs.getwriter('utf-8')(raw_stdout) +sys.stderr = codecs.getwriter('utf-8')(raw_stderr) + ############################################################################ def osdids(): @@ -887,6 +894,8 @@ def main(): if outs: print >> sys.stderr, prefix + outs + sys.stdout.flush() + if (parsed_args.output_file): outf.write(outbuf) else: @@ -897,13 +906,16 @@ def main(): if parsed_args.output_format and \ parsed_args.output_format.startswith('json') and \ not compat: - sys.stdout.write('\n') + raw_stdout.write('\n') # if we are prettifying things, normalize newlines. sigh. if suffix != '': outbuf = outbuf.rstrip() if outbuf != '': - sys.stdout.write(prefix + outbuf + suffix) + # Write directly to binary stdout + raw_stdout.write(prefix) + raw_stdout.write(outbuf) + raw_stdout.write(suffix) sys.stdout.flush()