changeset 25597:fd5bc660c9f0

templater: do not reevaluate rawstring as template (BC) The previous patch made 'string' is always interpreted as a template. So this patch removes the special handling of r'rawstring' instead. Now r'' disables template processing at all.
author Yuya Nishihara <yuya@tcha.org>
date Wed, 10 Jun 2015 21:44:43 +0900
parents c1975809a6b5
children 55c2cb65bdfa
files hgext/color.py mercurial/templater.py tests/test-command-template.t
diffstat 3 files changed, 29 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/color.py	Sat Jun 13 19:49:54 2015 +0900
+++ b/hgext/color.py	Wed Jun 10 21:44:43 2015 +0900
@@ -496,14 +496,14 @@
     # etc. don't need to be quoted
     mapping.update(dict([(k, k) for k in _effects]))
 
-    thing = templater._evalifliteral(args[1], context, mapping)
+    thing = args[1][0](context, mapping, args[1][1])
 
     # apparently, repo could be a string that is the favicon?
     repo = mapping.get('repo', '')
     if isinstance(repo, str):
         return thing
 
-    label = templater._evalifliteral(args[0], context, mapping)
+    label = args[0][0](context, mapping, args[0][1])
 
     thing = templater.stringify(thing)
     label = templater.stringify(label)
--- a/mercurial/templater.py	Sat Jun 13 19:49:54 2015 +0900
+++ b/mercurial/templater.py	Wed Jun 10 21:44:43 2015 +0900
@@ -146,8 +146,6 @@
 def gettemplate(exp, context):
     if exp[0] == 'template':
         return compiletemplate(exp[1], context)
-    if exp[0] == 'rawstring':
-        return compiletemplate(exp[1], context, strtoken=exp[0])
     if exp[0] == 'symbol':
         return context._load(exp[1])
     raise error.ParseError(_("expected template specifier"))
@@ -302,8 +300,8 @@
             # i18n: "fill" is a keyword
             raise error.ParseError(_("fill expects an integer width"))
         try:
-            initindent = stringify(_evalifliteral(args[2], context, mapping))
-            hangindent = stringify(_evalifliteral(args[3], context, mapping))
+            initindent = stringify(args[2][0](context, mapping, args[2][1]))
+            hangindent = stringify(args[3][0](context, mapping, args[3][1]))
         except IndexError:
             pass
 
@@ -318,7 +316,7 @@
 
     width = int(args[1][1])
 
-    text = stringify(_evalifliteral(args[0], context, mapping))
+    text = stringify(args[0][0](context, mapping, args[0][1]))
 
     right = False
     fillchar = ' '
@@ -368,15 +366,6 @@
     key = args[1][0](context, mapping, args[1][1])
     yield dictarg.get(key)
 
-def _evalifliteral(arg, context, mapping):
-    # get back to token tag to reinterpret string as template
-    strtoken = {runrawstring: 'rawstring'}.get(arg[0])
-    if strtoken:
-        yield runtemplate(context, mapping,
-                          compiletemplate(arg[1], context, strtoken))
-    else:
-        yield stringify(arg[0](context, mapping, arg[1]))
-
 def if_(context, mapping, args):
     """:if(expr, then[, else]): Conditionally execute based on the result of
     an expression."""
@@ -386,9 +375,9 @@
 
     test = stringify(args[0][0](context, mapping, args[0][1]))
     if test:
-        yield _evalifliteral(args[1], context, mapping)
+        yield args[1][0](context, mapping, args[1][1])
     elif len(args) == 3:
-        yield _evalifliteral(args[2], context, mapping)
+        yield args[2][0](context, mapping, args[2][1])
 
 def ifcontains(context, mapping, args):
     """:ifcontains(search, thing, then[, else]): Conditionally execute based
@@ -401,9 +390,9 @@
     items = args[1][0](context, mapping, args[1][1])
 
     if item in items:
-        yield _evalifliteral(args[2], context, mapping)
+        yield args[2][0](context, mapping, args[2][1])
     elif len(args) == 4:
-        yield _evalifliteral(args[3], context, mapping)
+        yield args[3][0](context, mapping, args[3][1])
 
 def ifeq(context, mapping, args):
     """:ifeq(expr1, expr2, then[, else]): Conditionally execute based on
@@ -415,9 +404,9 @@
     test = stringify(args[0][0](context, mapping, args[0][1]))
     match = stringify(args[1][0](context, mapping, args[1][1]))
     if test == match:
-        yield _evalifliteral(args[2], context, mapping)
+        yield args[2][0](context, mapping, args[2][1])
     elif len(args) == 4:
-        yield _evalifliteral(args[3], context, mapping)
+        yield args[3][0](context, mapping, args[3][1])
 
 def join(context, mapping, args):
     """:join(list, sep): Join items in a list with a delimiter."""
@@ -451,7 +440,7 @@
         raise error.ParseError(_("label expects two arguments"))
 
     # ignore args[0] (the label string) since this is supposed to be a a no-op
-    yield _evalifliteral(args[1], context, mapping)
+    yield args[1][0](context, mapping, args[1][1])
 
 def revset(context, mapping, args):
     """:revset(query[, formatargs...]): Execute a revision set query. See
@@ -567,7 +556,7 @@
 
     pat = stringify(args[0][0](context, mapping, args[0][1]))
     rpl = stringify(args[1][0](context, mapping, args[1][1]))
-    src = stringify(_evalifliteral(args[2], context, mapping))
+    src = stringify(args[2][0](context, mapping, args[2][1]))
     yield re.sub(pat, rpl, src)
 
 def startswith(context, mapping, args):
--- a/tests/test-command-template.t	Sat Jun 13 19:49:54 2015 +0900
+++ b/tests/test-command-template.t	Wed Jun 10 21:44:43 2015 +0900
@@ -2812,6 +2812,12 @@
   $ hg log -Ra -r0 -T '{r"rawstring: {rev}"}\n'
   rawstring: {rev}
 
+because map operation requires template, raw string can't be used
+
+  $ hg log -Ra -r0 -T '{files % r"rawstring"}\n'
+  hg: parse error: expected template specifier
+  [255]
+
 Test string escaping:
 
   $ hg log -R latesttag -r 0 --template '>\n<>\\n<{if(rev, "[>\n<>\\n<]")}>\n<>\\n<\n'
@@ -2865,23 +2871,23 @@
 Test leading backslashes:
 
   $ cd latesttag
-  $ hg log -r 2 -T '\{rev} {files % "\{file}"} {files % r"\{file}"}\n'
-  {rev} {file} \head1
-  $ hg log -r 2 -T '\\{rev} {files % "\\{file}"} {files % r"\\{file}"}\n'
-  \2 \head1 \\head1
-  $ hg log -r 2 -T '\\\{rev} {files % "\\\{file}"} {files % r"\\\{file}"}\n'
-  \{rev} \{file} \\\head1
+  $ hg log -r 2 -T '\{rev} {files % "\{file}"}\n'
+  {rev} {file}
+  $ hg log -r 2 -T '\\{rev} {files % "\\{file}"}\n'
+  \2 \head1
+  $ hg log -r 2 -T '\\\{rev} {files % "\\\{file}"}\n'
+  \{rev} \{file}
   $ cd ..
 
 Test leading backslashes in "if" expression (issue4714):
 
   $ cd latesttag
   $ hg log -r 2 -T '{if("1", "\{rev}")} {if("1", r"\{rev}")}\n'
-  {rev} \2
+  {rev} \{rev}
   $ hg log -r 2 -T '{if("1", "\\{rev}")} {if("1", r"\\{rev}")}\n'
-  \2 \\2
+  \2 \\{rev}
   $ hg log -r 2 -T '{if("1", "\\\{rev}")} {if("1", r"\\\{rev}")}\n'
-  \{rev} \\\2
+  \{rev} \\\{rev}
   $ cd ..
 
 "string-escape"-ed "\x5c\x786e" becomes r"\x6e" (once) or r"n" (twice)
@@ -2951,8 +2957,6 @@
   fourth
   second
   third
-  $ hg log -R a -r 8 --template '{files % r"{file}\n"}\n'
-  fourth\nsecond\nthird\n
 
 Test string escaping in nested expression:
 
@@ -3064,7 +3068,7 @@
   {0}        test
 
   $ hg log -r 0 -T '{pad(r"\{rev}", 10)} {author|user}\n'
-  \0         test
+  \{rev}     test
 
 Test ifcontains function