Patchwork [2,of,6] templater: unify unwrapvalue() with _unwrapvalue()

login
register
mail settings
Submitter Yuya Nishihara
Date June 4, 2018, 1:10 p.m.
Message ID <39d5832512a049548006.1528117809@mimosa>
Download mbox | patch
Permalink /patch/31961/
State Accepted
Headers show

Comments

Yuya Nishihara - June 4, 2018, 1:10 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1522843574 -32400
#      Wed Apr 04 21:06:14 2018 +0900
# Node ID 39d5832512a0495480068c761a9e081230aac737
# Parent  8cb8d0883f5f839ff66462f718f895e0d107ac34
templater: unify unwrapvalue() with _unwrapvalue()

All weird generators got removed from the hgweb codebase. We still have
inconsistent behavior regarding join() of a byte string, which will be
addressed later.

Patch

diff --git a/mercurial/templatefuncs.py b/mercurial/templatefuncs.py
--- a/mercurial/templatefuncs.py
+++ b/mercurial/templatefuncs.py
@@ -333,8 +333,9 @@  def join(context, mapping, args):
         joiner = evalstring(context, mapping, args[1])
     if isinstance(joinset, templateutil.wrapped):
         return joinset.join(context, mapping, joiner)
-    # TODO: perhaps a generator should be stringify()-ed here, but we can't
-    # because hgweb abuses it as a keyword that returns a list of dicts.
+    # TODO: rethink about join() of a byte string, which had no defined
+    # behavior since a string may be either a bytes or a generator.
+    # TODO: fix type error on join() of non-iterable
     joinset = templateutil.unwrapvalue(context, mapping, joinset)
     return templateutil.joinitems(pycompat.maybebytestr(joinset), joiner)
 
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -26,9 +26,6 @@  generator
     values of any printable types, and  will be folded by ``stringify()``
     or ``flatten()``.
 
-    BUG: hgweb overloads this type for mappings (i.e. some hgweb keywords
-    returns a generator of dicts.)
-
 None
     sometimes represents an empty value, which can be stringified to ''.
 
diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -285,12 +285,6 @@  def unwraphybrid(context, mapping, thing
         return thing
     return thing.show(context, mapping)
 
-def unwrapvalue(context, mapping, thing):
-    """Move the inner value object out of the wrapper"""
-    if not isinstance(thing, wrapped):
-        return thing
-    return thing.tovalue(context, mapping)
-
 def wraphybridvalue(container, key, value):
     """Wrap an element of hybrid container to be mappable
 
@@ -455,12 +449,10 @@  def evalrawexp(context, mapping, arg):
 
 def evalfuncarg(context, mapping, arg):
     """Evaluate given argument as value type"""
-    return _unwrapvalue(context, mapping, evalrawexp(context, mapping, arg))
+    return unwrapvalue(context, mapping, evalrawexp(context, mapping, arg))
 
-# TODO: unify this with unwrapvalue() once the bug of templatefunc.join()
-# is fixed. we can't do that right now because join() has to take a generator
-# of byte strings as it is, not a lazy byte string.
-def _unwrapvalue(context, mapping, thing):
+def unwrapvalue(context, mapping, thing):
+    """Move the inner value object out of the wrapper"""
     if isinstance(thing, wrapped):
         return thing.tovalue(context, mapping)
     # evalrawexp() may return string, generator of strings or arbitrary object
@@ -492,7 +484,7 @@  def evaldate(context, mapping, arg, err=
     return unwrapdate(context, mapping, thing, err)
 
 def unwrapdate(context, mapping, thing, err=None):
-    thing = _unwrapvalue(context, mapping, thing)
+    thing = unwrapvalue(context, mapping, thing)
     try:
         return dateutil.parsedate(thing)
     except AttributeError:
@@ -507,7 +499,7 @@  def evalinteger(context, mapping, arg, e
     return unwrapinteger(context, mapping, thing, err)
 
 def unwrapinteger(context, mapping, thing, err=None):
-    thing = _unwrapvalue(context, mapping, thing)
+    thing = unwrapvalue(context, mapping, thing)
     try:
         return int(thing)
     except (TypeError, ValueError):
@@ -527,7 +519,7 @@  def evalstringliteral(context, mapping, 
     return stringify(context, mapping, thing)
 
 _unwrapfuncbytype = {
-    None: _unwrapvalue,
+    None: unwrapvalue,
     bytes: stringify,
     date: unwrapdate,
     int: unwrapinteger,