]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph: Make stdout/stderr always output Unicode (UTF-8) 6315/head
authorDavid Coles <dcoles@gaikai.com>
Wed, 11 Nov 2015 22:06:45 +0000 (14:06 -0800)
committerDavid Coles <dcoles@gaikai.com>
Fri, 13 Nov 2015 01:26:37 +0000 (17:26 -0800)
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 <dcoles@gaikai.com>
src/ceph.in

index c6c7c498511b7bea645a7e0aefc0e1907aa028b2..a366a604a917948596b2536511641f02d83e3255 100755 (executable)
@@ -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()