Patchwork [2,of,7] templateutil: reimplement stringify() using flatten()

login
register
mail settings
Submitter Yuya Nishihara
Date March 25, 2018, 5:15 a.m.
Message ID <60e473afb171c9c2fae5.1521954949@mimosa>
Download mbox | patch
Permalink /patch/29839/
State Accepted
Headers show

Comments

Yuya Nishihara - March 25, 2018, 5:15 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521284660 -32400
#      Sat Mar 17 20:04:20 2018 +0900
# Node ID 60e473afb171c9c2fae598bdacd6880c1ac41ebf
# Parent  e9ae0d2c60b7a4c623f4065559f155733a290096
templateutil: reimplement stringify() using flatten()

Patch

diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -263,18 +263,9 @@  def flatten(thing):
 
 def stringify(thing):
     """Turn values into bytes by converting into text and concatenating them"""
-    thing = 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)
+    if isinstance(thing, bytes):
+        return thing  # retain localstr to be round-tripped
+    return b''.join(flatten(thing))
 
 def findsymbolicname(arg):
     """Find symbolic name for the given compiled expression; returns None