Patchwork [3,of,7] templater: fix pad() to evaluate int argument and handle error

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

Comments

Yuya Nishihara - March 4, 2016, 3:48 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1455422709 -32400
#      Sun Feb 14 13:05:09 2016 +0900
# Node ID 6e04f8b6594ef2ee0cc87d10a06c09f43e10b4c6
# Parent  417f3531a9e67cdbaaeb849b77a4eb4916410287
templater: fix pad() to evaluate int argument and handle error

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -398,7 +398,9 @@  def pad(context, mapping, args):
         # i18n: "pad" is a keyword
         raise error.ParseError(_("pad() expects two to four arguments"))
 
-    width = int(args[1][1])
+    width = evalinteger(context, mapping, args[1],
+                        # i18n: "pad" is a keyword
+                        _("pad() expects an integer width"))
 
     text = stringify(args[0][0](context, mapping, args[0][1]))
 
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
@@ -3244,6 +3244,14 @@  Test template string in pad function
   $ hg log -r 0 -T '{pad(r"\{rev}", 10)} {author|user}\n'
   \{rev}     test
 
+Test width argument passed to pad function
+
+  $ hg log -r 0 -T '{pad(rev, "1{"0"}")} {author|user}\n'
+  0          test
+  $ hg log -r 0 -T '{pad(rev, "not an int")}\n'
+  hg: parse error: pad() expects an integer width
+  [255]
+
 Test ifcontains function
 
   $ hg log --template '{rev} {ifcontains(rev, "2 two 0", "is in the string", "is not")}\n'