You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by ty...@apache.org on 2016/05/05 16:37:40 UTC

cassandra git commit: cqlsh: Handle non-ascii chars in error messages

Repository: cassandra
Updated Branches:
  refs/heads/cassandra-2.2 93c5bc616 -> 5de9de1f5


cqlsh: Handle non-ascii chars in error messages

Patch by Tyler Hobbs; reviewed by Paulo Motta for CASSANDRA-11626


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/5de9de1f
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/5de9de1f
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/5de9de1f

Branch: refs/heads/cassandra-2.2
Commit: 5de9de1f5832f2a0e92783e2f4412874423e6e15
Parents: 93c5bc6
Author: Tyler Hobbs <ty...@gmail.com>
Authored: Thu May 5 11:33:35 2016 -0500
Committer: Tyler Hobbs <ty...@gmail.com>
Committed: Thu May 5 11:33:35 2016 -0500

----------------------------------------------------------------------
 CHANGES.txt                |  1 +
 bin/cqlsh.py               | 20 ++++++++++++++------
 pylib/cqlshlib/copyutil.py | 16 ++++++++++------
 3 files changed, 25 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/5de9de1f/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 0d9d3e9..a46aa56 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 2.2.7
+ * cqlsh: correctly handle non-ascii chars in error messages (CASSANDRA-11626)
  * Exit JVM if JMX server fails to startup (CASSANDRA-11540)
  * Produce a heap dump when exiting on OOM (CASSANDRA-9861)
  * Avoid read repairing purgeable tombstones on range slices (CASSANDRA-11427)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/5de9de1f/bin/cqlsh.py
----------------------------------------------------------------------
diff --git a/bin/cqlsh.py b/bin/cqlsh.py
index d135317..85605ae 100644
--- a/bin/cqlsh.py
+++ b/bin/cqlsh.py
@@ -36,7 +36,6 @@ import codecs
 import ConfigParser
 import csv
 import getpass
-import locale
 import optparse
 import os
 import platform
@@ -883,7 +882,7 @@ class Shell(cmd.Cmd):
         if ksname is None:
             ksname = self.current_keyspace
         layout = self.get_table_meta(ksname, cfname)
-        return [str(col) for col in layout.columns]
+        return [unicode(col) for col in layout.columns]
 
     def get_usertype_names(self, ksname=None):
         if ksname is None:
@@ -1110,7 +1109,7 @@ class Shell(cmd.Cmd):
                 except EOFError:
                     self.handle_eof()
                 except CQL_ERRORS, cqlerr:
-                    self.printerr(str(cqlerr))
+                    self.printerr(unicode(cqlerr))
                 except KeyboardInterrupt:
                     self.reset_statement()
                     print
@@ -1257,10 +1256,10 @@ class Shell(cmd.Cmd):
                 break
             except cassandra.OperationTimedOut, err:
                 self.refresh_schema_metadata_best_effort()
-                self.printerr(str(err.__class__.__name__) + ": " + str(err))
+                self.printerr(unicode(err.__class__.__name__) + u": " + unicode(err))
                 return False, None
             except CQL_ERRORS, err:
-                self.printerr(str(err.__class__.__name__) + ": " + str(err))
+                self.printerr(unicode(err.__class__.__name__) + u": " + unicode(err))
                 return False, None
             except Exception, err:
                 import traceback
@@ -2237,7 +2236,16 @@ class Shell(cmd.Cmd):
     def writeresult(self, text, color=None, newline=True, out=None):
         if out is None:
             out = self.query_out
-        out.write(self.applycolor(str(text), color) + ('\n' if newline else ''))
+
+        # convert Exceptions, etc to text
+        if not isinstance(text, (unicode, str)):
+            text = unicode(text)
+
+        if isinstance(text, unicode):
+            text = text.encode(self.encoding)
+
+        to_write = self.applycolor(text, color) + ('\n' if newline else '')
+        out.write(to_write)
 
     def flush_output(self):
         self.query_out.flush()

http://git-wip-us.apache.org/repos/asf/cassandra/blob/5de9de1f/pylib/cqlshlib/copyutil.py
----------------------------------------------------------------------
diff --git a/pylib/cqlshlib/copyutil.py b/pylib/cqlshlib/copyutil.py
index a7a6671..ea49692 100644
--- a/pylib/cqlshlib/copyutil.py
+++ b/pylib/cqlshlib/copyutil.py
@@ -76,8 +76,9 @@ def printdebugmsg(msg):
         printmsg(msg)
 
 
-def printmsg(msg, eol='\n'):
-    sys.stdout.write(msg + eol)
+def printmsg(msg, eol='\n', encoding='utf8'):
+    sys.stdout.write(msg.encode(encoding))
+    sys.stdout.write(eol)
     sys.stdout.flush()
 
 
@@ -219,6 +220,7 @@ class CopyTask(object):
         self.options = self.parse_options(opts, direction)
 
         self.num_processes = self.options.copy['numprocesses']
+        self.encoding = self.options.copy['encoding']
         self.printmsg('Using %d child processes' % (self.num_processes,))
 
         if direction == 'from':
@@ -595,7 +597,8 @@ class ExportTask(CopyTask):
         if not self.writer.open():
             return 0
 
-        self.printmsg("\nStarting copy of %s.%s with columns %s." % (self.ks, self.table, self.columns))
+        columns = u"[" + u", ".join(self.columns) + u"]"
+        self.printmsg(u"\nStarting copy of %s.%s with columns %s." % (self.ks, self.table, columns), encoding=self.encoding)
 
         params = self.make_params()
         for i in xrange(self.num_processes):
@@ -1087,7 +1090,8 @@ class ImportTask(CopyTask):
         if not self.validate_columns():
             return 0
 
-        self.printmsg("\nStarting copy of %s.%s with columns %s." % (self.ks, self.table, self.valid_columns))
+        columns = u"[" + u", ".join(self.valid_columns) + u"]"
+        self.printmsg(u"\nStarting copy of %s.%s with columns %s." % (self.ks, self.table, columns), encoding=self.encoding)
 
         try:
             params = self.make_params()
@@ -1109,7 +1113,7 @@ class ImportTask(CopyTask):
                 profile_off(pr, file_name='parent_profile_%d.txt' % (os.getpid(),))
 
         except Exception, exc:
-            shell.printerr(str(exc))
+            shell.printerr(unicode(exc))
             if shell.debug:
                 traceback.print_exc()
             return 0
@@ -1452,7 +1456,7 @@ class ExportProcess(ChildProcess):
             if print_traceback and sys.exc_info()[1] == err:
                 traceback.print_exc()
         else:
-            msg = str(err)
+            msg = unicode(err)
         return msg
 
     def report_error(self, err, token_range):