Patchwork [7,of,8,V2] templater: extract private function to evaluate generator to byte string

login
register
mail settings
Submitter Yuya Nishihara
Date April 3, 2018, 3:40 p.m.
Message ID <7404b3400212b0084541.1522770009@mimosa>
Download mbox | patch
Permalink /patch/30168/
State Accepted
Headers show

Comments

Yuya Nishihara - April 3, 2018, 3:40 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521808816 -32400
#      Fri Mar 23 21:40:16 2018 +0900
# Node ID 7404b3400212b00845411af13a69ac753bff6bb2
# Parent  df181b84d8341bd2560964165624c04a1d5c84fe
templater: extract private function to evaluate generator to byte string

Patch

diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -313,6 +313,12 @@  def findsymbolicname(arg):
         else:
             return None
 
+def _unthunk(context, mapping, thing):
+    """Evaluate a lazy byte string into value"""
+    if not isinstance(thing, types.GeneratorType):
+        return thing
+    return stringify(context, mapping, thing)
+
 def evalrawexp(context, mapping, arg):
     """Evaluate given argument as a bare template object which may require
     further processing (such as folding generator of strings)"""
@@ -330,9 +336,7 @@  def _unwrapvalue(context, mapping, thing
     thing = unwrapvalue(context, mapping, thing)
     # evalrawexp() may return string, generator of strings or arbitrary object
     # such as date tuple, but filter does not want generator.
-    if isinstance(thing, types.GeneratorType):
-        thing = stringify(context, mapping, thing)
-    return thing
+    return _unthunk(context, mapping, thing)
 
 def evalboolean(context, mapping, arg):
     """Evaluate given argument as boolean, but also takes boolean literals"""