Backport CASSANDRA-12189, formatting fixes

Patch by Caleb Rackliffe, reviewed by brandonwilliams for
CASSANDRA-15948
diff --git a/CHANGES.txt b/CHANGES.txt
index 9463403..b5b406b 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,6 +4,7 @@
  * Fix CQL formatting of read command restrictions for slow query log (CASSANDRA-15503)
  * Allow sstableloader to use SSL on the native port (CASSANDRA-14904)
 Merged from 3.0:
+ * Backport CASSANDRA-12189: escape string literals (CASSANDRA-15948)
  * Avoid hinted handoff per-host throttle being arounded to 0 in large cluster (CASSANDRA-15859)
  * Avoid emitting empty range tombstones from RangeTombstoneList (CASSANDRA-15924)
  * Avoid thread starvation, and improve compare-and-swap performance, in the slab allocators (CASSANDRA-15922)
diff --git a/bin/cqlsh.py b/bin/cqlsh.py
index 44d4d50..f1b16cd 100644
--- a/bin/cqlsh.py
+++ b/bin/cqlsh.py
@@ -217,8 +217,7 @@
 parser.add_option("-f", "--file", help="Execute commands from FILE, then exit")
 parser.add_option('--debug', action='store_true',
                   help='Show additional debugging information')
-parser.add_option("--encoding", help="Specify a non-default encoding for output." +
-                  " (Default: %s)" % (UTF8,))
+parser.add_option("--encoding", help="Specify a non-default encoding for output. (Default: %s)" % (UTF8,))
 parser.add_option("--cqlshrc", help="Specify an alternative cqlshrc file location.")
 parser.add_option('--cqlversion', default=None,
                   help='Specify a particular CQL version, '
@@ -2406,8 +2405,8 @@
             # we silently ignore and fallback to UTC unless a custom timestamp format (which likely
             # does contain a TZ part) was specified
             if options.time_format != DEFAULT_TIMESTAMP_FORMAT:
-                sys.stderr.write("Warning: custom timestamp format specified in cqlshrc, but local timezone could not be detected.\n" +
-                                 "Either install Python 'tzlocal' module for auto-detection or specify client timezone in your cqlshrc.\n\n")
+                sys.stderr.write("Warning: custom timestamp format specified in cqlshrc, but local timezone could not be detected.\n"
+                                 + "Either install Python 'tzlocal' module for auto-detection or specify client timezone in your cqlshrc.\n\n")
 
     try:
         shell = Shell(hostname,
diff --git a/pylib/cqlshlib/copyutil.py b/pylib/cqlshlib/copyutil.py
index b91bb76..065fc83 100644
--- a/pylib/cqlshlib/copyutil.py
+++ b/pylib/cqlshlib/copyutil.py
@@ -1949,9 +1949,9 @@
             return ret
 
         # this should match all possible CQL and CQLSH datetime formats
-        p = re.compile("(\d{4})\-(\d{2})\-(\d{2})\s?(?:'T')?" +  # YYYY-MM-DD[( |'T')]
-                       "(?:(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,6}))?))?" +  # [HH:MM[:SS[.NNNNNN]]]
-                       "(?:([+\-])(\d{2}):?(\d{2}))?")  # [(+|-)HH[:]MM]]
+        p = re.compile(r"(\d{4})\-(\d{2})\-(\d{2})\s?(?:'T')?"  # YYYY-MM-DD[( |'T')]
+                       + r"(?:(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,6}))?))?"  # [HH:MM[:SS[.NNNNNN]]]
+                       + r"(?:([+\-])(\d{2}):?(\d{2}))?")  # [(+|-)HH[:]MM]]
 
         def convert_datetime(val, **_):
             try:
diff --git a/pylib/cqlshlib/cql3handling.py b/pylib/cqlshlib/cql3handling.py
index ae5bc8a..7709a26 100644
--- a/pylib/cqlshlib/cql3handling.py
+++ b/pylib/cqlshlib/cql3handling.py
@@ -461,8 +461,7 @@
 
 
 def cf_prop_name_completer(ctxt, cass):
-    return [c[0] for c in (CqlRuleSet.columnfamily_layout_options +
-                           CqlRuleSet.columnfamily_layout_map_options)]
+    return [c[0] for c in (CqlRuleSet.columnfamily_layout_options + CqlRuleSet.columnfamily_layout_map_options)]
 
 
 def cf_prop_val_completer(ctxt, cass):
@@ -864,9 +863,9 @@
 def regular_column_names(table_meta):
     if not table_meta or not table_meta.columns:
         return []
-    regular_columns = list(set(table_meta.columns.keys()) -
-                           set([key.name for key in table_meta.partition_key]) -
-                           set([key.name for key in table_meta.clustering_key]))
+    regular_columns = list(set(table_meta.columns.keys())
+                           - set([key.name for key in table_meta.partition_key])
+                           - set([key.name for key in table_meta.clustering_key]))
     return regular_columns
 
 
diff --git a/pylib/cqlshlib/formatting.py b/pylib/cqlshlib/formatting.py
index 9927aa1..d87e206 100644
--- a/pylib/cqlshlib/formatting.py
+++ b/pylib/cqlshlib/formatting.py
@@ -236,6 +236,7 @@
         return f
     return registrator
 
+
 class BlobType(object):
     def __init__(self, val):
         self.val = val
@@ -243,6 +244,7 @@
     def __str__(self):
         return str(self.val)
 
+
 @formatter_for('BlobType')
 def format_value_blob(val, colormap, **_):
     bval = '0x' + binascii.hexlify(val)
@@ -385,7 +387,7 @@
         return '%d' % (seconds * 1000.0)
 
 
-microseconds_regex = re.compile("(.*)(?:\.(\d{1,6}))(.*)")
+microseconds_regex = re.compile(r"(.*)(?:\.(\d{1,6}))(.*)")
 
 
 def round_microseconds(val):
diff --git a/pylib/cqlshlib/wcwidth.py b/pylib/cqlshlib/wcwidth.py
index 985fd41..096880a 100644
--- a/pylib/cqlshlib/wcwidth.py
+++ b/pylib/cqlshlib/wcwidth.py
@@ -252,19 +252,21 @@
     # if we arrive here, ucs is not a combining or C0/C1 control character
 
     return 1 + \
-        int(ucs >= 0x1100 and
-            (ucs <= 0x115f or                     # Hangul Jamo init. consonants
-             ucs == 0x2329 or ucs == 0x232a or
-             (ucs >= 0x2e80 and ucs <= 0xa4cf and
-              ucs != 0x303f) or                   # CJK ... Yi
-                (ucs >= 0xac00 and ucs <= 0xd7a3) or  # Hangul Syllables
-                (ucs >= 0xf900 and ucs <= 0xfaff) or  # CJK Compatibility Ideographs
-                (ucs >= 0xfe10 and ucs <= 0xfe19) or  # Vertical forms
-                (ucs >= 0xfe30 and ucs <= 0xfe6f) or  # CJK Compatibility Forms
-                (ucs >= 0xff00 and ucs <= 0xff60) or  # Fullwidth Forms
-                (ucs >= 0xffe0 and ucs <= 0xffe6) or
-                (ucs >= 0x20000 and ucs <= 0x2fffd) or
-                (ucs >= 0x30000 and ucs <= 0x3fffd)))
+        int(ucs >= 0x1100
+            and (ucs <= 0x115f  # Hangul Jamo init. consonants
+                 or ucs == 0x2329
+                 or ucs == 0x232a
+                 or (ucs >= 0x2e80
+                     and ucs <= 0xa4cf
+                     and ucs != 0x303f)  # CJK ... Yi
+                 or (ucs >= 0xac00 and ucs <= 0xd7a3)  # Hangul Syllables
+                 or (ucs >= 0xf900 and ucs <= 0xfaff)  # CJK Compatibility Ideographs
+                 or (ucs >= 0xfe10 and ucs <= 0xfe19)  # Vertical forms
+                 or (ucs >= 0xfe30 and ucs <= 0xfe6f)  # CJK Compatibility Forms
+                 or (ucs >= 0xff00 and ucs <= 0xff60)  # Fullwidth Forms
+                 or (ucs >= 0xffe0 and ucs <= 0xffe6)
+                 or (ucs >= 0x20000 and ucs <= 0x2fffd)
+                 or (ucs >= 0x30000 and ucs <= 0x3fffd)))
 
 
 def mk_wcswidth(pwcs):