diff mercurial/templateutil.py @ 36926:6ff6e1d6b5b8

templater: move stringify() to templateutil module As we have a util module, it doesn't make sense to import stringify() from templatefilters.py.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 08 Mar 2018 23:10:46 +0900
parents da2977e674a3
children 32f9b7e3f056
line wrap: on
line diff
--- a/mercurial/templateutil.py	Wed Feb 28 15:20:41 2018 -0500
+++ b/mercurial/templateutil.py	Thu Mar 08 23:10:46 2018 +0900
@@ -13,7 +13,6 @@
 from . import (
     error,
     pycompat,
-    templatefilters,
     templatekw,
     util,
 )
@@ -24,6 +23,21 @@
 class TemplateNotFound(error.Abort):
     pass
 
+def stringify(thing):
+    """Turn values into bytes by converting into text and concatenating them"""
+    thing = templatekw.unwraphybrid(thing)
+    if util.safehasattr(thing, '__iter__') and not isinstance(thing, bytes):
+        if isinstance(thing, str):
+            # This is only reachable on Python 3 (otherwise
+            # isinstance(thing, bytes) would have been true), and is
+            # here to prevent infinite recursion bugs on Python 3.
+            raise error.ProgrammingError(
+                'stringify got unexpected unicode string: %r' % thing)
+        return "".join([stringify(t) for t in thing if t is not None])
+    if thing is None:
+        return ""
+    return pycompat.bytestr(thing)
+
 def findsymbolicname(arg):
     """Find symbolic name for the given compiled expression; returns None
     if nothing found reliably"""
@@ -223,5 +237,3 @@
     if val is None:
         return
     return templatekw.wraphybridvalue(dictarg, key, val)
-
-stringify = templatefilters.stringify