Patchwork [7,of,7] templater: factor out unwrapastype() from evalastype()

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

Comments

Yuya Nishihara - March 25, 2018, 5:15 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521805435 -32400
#      Fri Mar 23 20:43:55 2018 +0900
# Node ID bcfa34ae805b92d3c9d32c8bed19c6432ba44120
# Parent  b5bdcfbf663e1fa1d7c2004a0ef6b3172cc8eff9
templater: factor out unwrapastype() from evalastype()

So ParseError of unwrapastype() can be caught reliably.
Augie Fackler - March 30, 2018, 4:03 p.m.
On Sun, Mar 25, 2018 at 02:15:54PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1521805435 -32400
> #      Fri Mar 23 20:43:55 2018 +0900
> # Node ID bcfa34ae805b92d3c9d32c8bed19c6432ba44120
> # Parent  b5bdcfbf663e1fa1d7c2004a0ef6b3172cc8eff9
> templater: factor out unwrapastype() from evalastype()

queued, thanks

Patch

diff --git a/mercurial/templatefuncs.py b/mercurial/templatefuncs.py
--- a/mercurial/templatefuncs.py
+++ b/mercurial/templatefuncs.py
@@ -34,7 +34,6 @@  evalboolean = templateutil.evalboolean
 evalinteger = templateutil.evalinteger
 evalstring = templateutil.evalstring
 evalstringliteral = templateutil.evalstringliteral
-evalastype = templateutil.evalastype
 
 # dict of template built-in functions
 funcs = {}
@@ -261,9 +260,10 @@  def ifcontains(context, mapping, args):
         raise error.ParseError(_("ifcontains expects three or four arguments"))
 
     haystack = evalfuncarg(context, mapping, args[1])
+    keytype = getattr(haystack, 'keytype', None)
     try:
-        needle = evalastype(context, mapping, args[0],
-                            getattr(haystack, 'keytype', None) or bytes)
+        needle = evalrawexp(context, mapping, args[0])
+        needle = templateutil.unwrapastype(needle, keytype or bytes)
         found = (needle in haystack)
     except error.ParseError:
         found = False
diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -77,7 +77,8 @@  class mappable(object):
     - "{manifest.rev}"
 
     Unlike a hybrid, this does not simulate the behavior of the underling
-    value. Use unwrapvalue() or unwraphybrid() to obtain the inner object.
+    value. Use unwrapvalue(), unwrapastype(), or unwraphybrid() to obtain
+    the inner object.
     """
 
     def __init__(self, gen, key, value, makemap):
@@ -340,18 +341,18 @@  def evalstringliteral(context, mapping, 
         thing = func(context, mapping, data)
     return stringify(thing)
 
-_evalfuncbytype = {
-    bytes: evalstring,
-    int: evalinteger,
+_unwrapfuncbytype = {
+    bytes: stringify,
+    int: unwrapinteger,
 }
 
-def evalastype(context, mapping, arg, typ):
-    """Evaluate given argument and coerce its type"""
+def unwrapastype(thing, typ):
+    """Move the inner value object out of the wrapper and coerce its type"""
     try:
-        f = _evalfuncbytype[typ]
+        f = _unwrapfuncbytype[typ]
     except KeyError:
         raise error.ProgrammingError('invalid type specified: %r' % typ)
-    return f(context, mapping, arg)
+    return f(thing)
 
 def runinteger(context, mapping, data):
     return int(data)