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

login
register
mail settings
Submitter Yuya Nishihara
Date April 1, 2018, 2:46 a.m.
Message ID <5d5ecd0c1e91676bc1ac.1522550764@mimosa>
Download mbox | patch
Permalink /patch/30094/
State Deferred
Headers show

Comments

Yuya Nishihara - April 1, 2018, 2:46 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521808816 -32400
#      Fri Mar 23 21:40:16 2018 +0900
# Node ID 5d5ecd0c1e91676bc1ac6a481679cad21fb0e108
# Parent  d7a44391a574f6526d4d14925abe1dce048a83e3
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
@@ -308,6 +308,12 @@  def findsymbolicname(arg):
         else:
             return None
 
+def _unthunk(context, thing):
+    """Evaluate a lazy byte string into value"""
+    if not isinstance(thing, types.GeneratorType):
+        return thing
+    return stringify(context, 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)"""
@@ -325,9 +331,7 @@  def _unwrapvalue(context, thing):
     thing = unwrapvalue(context, 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, thing)
-    return thing
+    return _unthunk(context, thing)
 
 def evalboolean(context, mapping, arg):
     """Evaluate given argument as boolean, but also takes boolean literals"""