]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/util/encodingutil.py
Only quote newline characters where necessary. fixes #1484
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / util / encodingutil.py
index 2f3bfeca2bebb116df345f7f53263bb11168e07a..3ceb1a919c7e72dda2d2d820c0b50dd16f8c65a6 100644 (file)
@@ -153,10 +153,12 @@ def unicode_to_output(s):
     return out
 
 
-def _unicode_escape(m):
+def _unicode_escape(m, quote_newlines):
     u = m.group(0)
-    if u == '"' or u == '$' or u == '`' or u == '\\':
+    if u == u'"' or u == u'$' or u == u'`' or u == u'\\':
         return u'\\' + u
+    elif u == u'\n' and not quote_newlines:
+        return u
     if len(u) == 2:
         codepoint = (ord(u[0])-0xD800)*0x400 + ord(u[1])-0xDC00 + 0x10000
     else:
@@ -168,14 +170,17 @@ def _unicode_escape(m):
     else:
         return u'\\x%02x' % (codepoint,)
 
-def _str_escape(m):
+def _str_escape(m, quote_newlines):
     c = m.group(0)
     if c == '"' or c == '$' or c == '`' or c == '\\':
         return '\\' + c
+    elif c == '\n' and not quote_newlines:
+        return c
     else:
         return '\\x%02x' % (ord(c),)
 
-MUST_DOUBLE_QUOTE = re.compile(ur'[^\x20-\x26\x28-\x7E\u00A0-\uD7FF\uE000-\uFDCF\uFDF0-\uFFFC]', re.DOTALL)
+MUST_DOUBLE_QUOTE_NL = re.compile(ur'[^\x20-\x26\x28-\x7E\u00A0-\uD7FF\uE000-\uFDCF\uFDF0-\uFFFC]', re.DOTALL)
+MUST_DOUBLE_QUOTE    = re.compile(ur'[^\n\x20-\x26\x28-\x7E\u00A0-\uD7FF\uE000-\uFDCF\uFDF0-\uFFFC]', re.DOTALL)
 
 # if we must double-quote, then we have to escape ", $ and `, but need not escape '
 ESCAPABLE_UNICODE = re.compile(ur'([\uD800-\uDBFF][\uDC00-\uDFFF])|'  # valid surrogate pairs
@@ -184,25 +189,32 @@ ESCAPABLE_UNICODE = re.compile(ur'([\uD800-\uDBFF][\uDC00-\uDFFF])|'  # valid su
 
 ESCAPABLE_8BIT    = re.compile( r'[^ !#\x25-\x5B\x5D-\x5F\x61-\x7E]', re.DOTALL)
 
-def quote_output(s, quotemarks=True, encoding=None):
+def quote_output(s, quotemarks=True, quote_newlines=None, encoding=None):
     """
     Encode either a Unicode string or a UTF-8-encoded bytestring for representation
     on stdout or stderr, tolerating errors. If 'quotemarks' is True, the string is
     always quoted; otherwise, it is quoted only if necessary to avoid ambiguity or
-    control bytes in the output.
+    control bytes in the output. (Newlines are counted as control bytes iff
+    quote_newlines is True.)
+
     Quoting may use either single or double quotes. Within single quotes, all
     characters stand for themselves, and ' will not appear. Within double quotes,
     Python-compatible backslash escaping is used.
+
+    If not explicitly given, quote_newlines is True when quotemarks is True.
     """
     precondition(isinstance(s, (str, unicode)), s)
+    if quote_newlines is None:
+        quote_newlines = quotemarks
 
     if isinstance(s, str):
         try:
             s = s.decode('utf-8')
         except UnicodeDecodeError:
-            return 'b"%s"' % (ESCAPABLE_8BIT.sub(_str_escape, s),)
+            return 'b"%s"' % (ESCAPABLE_8BIT.sub(lambda m: _str_escape(m, quote_newlines), s),)
 
-    if MUST_DOUBLE_QUOTE.search(s) is None:
+    must_double_quote = quote_newlines and MUST_DOUBLE_QUOTE_NL or MUST_DOUBLE_QUOTE
+    if must_double_quote.search(s) is None:
         try:
             out = s.encode(encoding or io_encoding)
             if quotemarks or out.startswith('"'):
@@ -212,11 +224,11 @@ def quote_output(s, quotemarks=True, encoding=None):
         except (UnicodeDecodeError, UnicodeEncodeError):
             pass
 
-    escaped = ESCAPABLE_UNICODE.sub(_unicode_escape, s)
+    escaped = ESCAPABLE_UNICODE.sub(lambda m: _unicode_escape(m, quote_newlines), s)
     return '"%s"' % (escaped.encode(encoding or io_encoding, 'backslashreplace'),)
 
 def quote_path(path, quotemarks=True):
-    return quote_output("/".join(map(to_str, path)), quotemarks=quotemarks)
+    return quote_output("/".join(map(to_str, path)), quotemarks=quotemarks, quote_newlines=True)
 
 
 def unicode_platform():