[PATCH 3 of 5] templater: do not reevaluate rawstring as template (BC)
Yuya Nishihara
yuya at tcha.org
Tue Jun 16 10:10:38 CDT 2015
# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1433940283 -32400
# Wed Jun 10 21:44:43 2015 +0900
# Node ID 2500c86c6c99255184db25ee60eaaf92604b20a5
# Parent fde14bbc4236a33809325c6ec30bb64d8fe2c3cb
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.
diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -496,14 +496,14 @@ def templatelabel(context, mapping, args
# 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)
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -146,8 +146,6 @@ def getfilter(exp, context):
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 @@ def fill(context, mapping, args):
# 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 @@ def pad(context, mapping, args):
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 @@ def get(context, mapping, args):
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 @@ def if_(context, mapping, args):
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 @@ def ifcontains(context, mapping, args):
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 @@ def ifeq(context, mapping, args):
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 @@ def label(context, mapping, args):
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 @@ def sub(context, mapping, args):
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):
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -2812,6 +2812,12 @@ Test string literal:
$ 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 @@ stripped before parsing:
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 @@ Test leading backslashes in "if" express
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 @@ Test template string in pad function
{0} test
$ hg log -r 0 -T '{pad(r"\{rev}", 10)} {author|user}\n'
- \0 test
+ \{rev} test
Test ifcontains function
More information about the Mercurial-devel
mailing list