Patchwork [STABLE] templater: fail more gracefully for blank strings to word

login
register
mail settings
Submitter Ryan McElroy
Date April 30, 2015, 7:34 p.m.
Message ID <af94c66f4d9134f796ac.1430422464@devbig105.prn2.facebook.com>
Download mbox | patch
Permalink /patch/8822/
State Accepted
Headers show

Comments

Ryan McElroy - April 30, 2015, 7:34 p.m.
# HG changeset patch
# User Ryan McElroy <rmcelroy@fb.com>
# Date 1430422416 25200
#      Thu Apr 30 12:33:36 2015 -0700
# Branch stable
# Node ID af94c66f4d9134f796ac88d34123dceca07c1125
# Parent  995003a324da67242e5c4e9afa18fe9d335f9985
templater: fail more gracefully for blank strings to word
Matt Mackall - April 30, 2015, 8:23 p.m.
On Thu, 2015-04-30 at 12:34 -0700, Ryan McElroy wrote:
> # HG changeset patch
> # User Ryan McElroy <rmcelroy@fb.com>
> # Date 1430422416 25200
> #      Thu Apr 30 12:33:36 2015 -0700
> # Branch stable
> # Node ID af94c66f4d9134f796ac88d34123dceca07c1125
> # Parent  995003a324da67242e5c4e9afa18fe9d335f9985
> templater: fail more gracefully for blank strings to word

Queued for stable, thanks.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -539,7 +539,12 @@  def word(context, mapping, args):
         raise error.ParseError(_("word expects two or three arguments, got %d")
                                % len(args))
 
-    num = int(stringify(args[0][0](context, mapping, args[0][1])))
+    try:
+        num = int(stringify(args[0][0](context, mapping, args[0][1])))
+    except ValueError:
+        # i18n: "word" is a keyword
+        raise error.ParseError(
+                _("Use strings like '3' for numbers passed to word function"))
     text = stringify(args[1][0](context, mapping, args[1][1]))
     if len(args) == 3:
         splitter = stringify(args[2][0](context, mapping, args[2][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
@@ -2620,3 +2620,9 @@  Test word error messages for not enough 
   $ hg log -Gv -R a --template "{word('0', desc, 'o', 'h', 'b', 'o', 'y')}"
   hg: parse error: word expects two or three arguments, got 7
   [255]
+
+Test word for invalid numbers
+
+  $ hg log -Gv -R a --template "{word(2, desc)}"
+  hg: parse error: Use strings like '3' for numbers passed to word function
+  [255]