templater: factor out unwrapastype() from evalastype()
authorYuya Nishihara <yuya@tcha.org>
Fri, 23 Mar 2018 20:43:55 +0900
changeset 37163 0fb28899e81a
parent 37162 9ab3491f84c2
child 37164 b229fd9adeae
templater: factor out unwrapastype() from evalastype() So ParseError of unwrapastype() can be caught reliably.
mercurial/templatefuncs.py
mercurial/templateutil.py
--- a/mercurial/templatefuncs.py	Fri Mar 23 20:34:12 2018 +0900
+++ b/mercurial/templatefuncs.py	Fri Mar 23 20:43:55 2018 +0900
@@ -34,7 +34,6 @@
 evalinteger = templateutil.evalinteger
 evalstring = templateutil.evalstring
 evalstringliteral = templateutil.evalstringliteral
-evalastype = templateutil.evalastype
 
 # dict of template built-in functions
 funcs = {}
@@ -261,9 +260,10 @@
         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
--- a/mercurial/templateutil.py	Fri Mar 23 20:34:12 2018 +0900
+++ b/mercurial/templateutil.py	Fri Mar 23 20:43:55 2018 +0900
@@ -77,7 +77,8 @@
     - "{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 @@
         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)