Patchwork [2,of,7] templater: drop redundant type conversion when evaluating integer argument

login
register
mail settings
Submitter Yuya Nishihara
Date March 4, 2016, 3:48 p.m.
Message ID <417f3531a9e67cdbaaeb.1457106524@mimosa>
Download mbox | patch
Permalink /patch/13598/
State Accepted
Headers show

Comments

Yuya Nishihara - March 4, 2016, 3:48 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1455421694 -32400
#      Sun Feb 14 12:48:14 2016 +0900
# Node ID 417f3531a9e67cdbaaeb849b77a4eb4916410287
# Parent  d5aaf79a94ec23a4fee44ce5aa758c1c443396bc
templater: drop redundant type conversion when evaluating integer argument

A function argument may be an integer. In this case, it isn't necessary to
convert a value to string and back to integer.

Because an argument may be an arbitrary object (e.g. date tuple), TypeError
should be caught as well.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -221,9 +221,10 @@  def evalfuncarg(context, mapping, arg):
     return thing
 
 def evalinteger(context, mapping, arg, err):
+    v = evalfuncarg(context, mapping, arg)
     try:
-        return int(stringify(arg[0](context, mapping, arg[1])))
-    except ValueError:
+        return int(v)
+    except (TypeError, ValueError):
         raise error.ParseError(err)
 
 def runinteger(context, mapping, data):
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
@@ -3148,6 +3148,9 @@  Test recursive evaluation:
   text.1:be wrapped
   text.1:desc to be
   text.1:wrapped (no-eol)
+  $ hg log -l1 -T '{fill(desc, date, "", "")}\n'
+  hg: parse error: fill expects an integer width
+  [255]
 
   $ hg log -l 1 --template '{sub(r"[0-9]", "-", author)}'
   {node|short} (no-eol)