templater: byte-stringify dict/list values before passing to default format
authorYuya Nishihara <yuya@tcha.org>
Thu, 01 Mar 2018 08:14:54 -0500
changeset 36663 cafd0586876b
parent 36662 034a07e60e98
child 36664 e80f8a134731
templater: byte-stringify dict/list values before passing to default format bytestr() is applied only when no custom format string like '%d' is specified.
mercurial/formatter.py
mercurial/templatekw.py
--- a/mercurial/formatter.py	Thu Mar 01 08:07:22 2018 -0500
+++ b/mercurial/formatter.py	Thu Mar 01 08:14:54 2018 -0500
@@ -248,15 +248,20 @@
     @staticmethod
     def formatdict(data, key, value, fmt, sep):
         '''stringify key-value pairs separated by sep'''
+        prefmt = pycompat.identity
         if fmt is None:
             fmt = '%s=%s'
-        return sep.join(fmt % (k, v) for k, v in _iteritems(data))
+            prefmt = pycompat.bytestr
+        return sep.join(fmt % (prefmt(k), prefmt(v))
+                        for k, v in _iteritems(data))
     @staticmethod
     def formatlist(data, name, fmt, sep):
         '''stringify iterable separated by sep'''
+        prefmt = pycompat.identity
         if fmt is None:
             fmt = '%s'
-        return sep.join(fmt % e for e in data)
+            prefmt = pycompat.bytestr
+        return sep.join(fmt % prefmt(e) for e in data)
 
 class plainformatter(baseformatter):
     '''the default text output scheme'''
--- a/mercurial/templatekw.py	Thu Mar 01 08:07:22 2018 -0500
+++ b/mercurial/templatekw.py	Thu Mar 01 08:14:54 2018 -0500
@@ -99,16 +99,20 @@
 
 def hybriddict(data, key='key', value='value', fmt=None, gen=None):
     """Wrap data to support both dict-like and string-like operations"""
+    prefmt = pycompat.identity
     if fmt is None:
         fmt = '%s=%s'
+        prefmt = pycompat.bytestr
     return _hybrid(gen, data, lambda k: {key: k, value: data[k]},
-                   lambda k: fmt % (k, data[k]))
+                   lambda k: fmt % (prefmt(k), prefmt(data[k])))
 
 def hybridlist(data, name, fmt=None, gen=None):
     """Wrap data to support both list-like and string-like operations"""
+    prefmt = pycompat.identity
     if fmt is None:
         fmt = '%s'
-    return _hybrid(gen, data, lambda x: {name: x}, lambda x: fmt % x)
+        prefmt = pycompat.bytestr
+    return _hybrid(gen, data, lambda x: {name: x}, lambda x: fmt % prefmt(x))
 
 def unwraphybrid(thing):
     """Return an object which can be stringified possibly by using a legacy