Patchwork [2,of,3] templater: byte-stringify dict/list values before passing to default format

login
register
mail settings
Submitter Yuya Nishihara
Date March 2, 2018, 10:25 p.m.
Message ID <2c41855dbd2c20dedb97.1520029532@mimosa>
Download mbox | patch
Permalink /patch/28738/
State Accepted
Headers show

Comments

Yuya Nishihara - March 2, 2018, 10:25 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1519910094 18000
#      Thu Mar 01 08:14:54 2018 -0500
# Node ID 2c41855dbd2c20dedb97ac68a1909a9ffa9c0774
# Parent  09b7cc4c0d6e56e77553789225a2039f3de427fd
templater: byte-stringify dict/list values before passing to default format

bytestr() is applied only when no custom format string like '%d' is specified.

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -247,15 +247,20 @@  class _plainconverter(object):
     @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'''
diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -99,16 +99,20 @@  class _mappable(object):
 
 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