Patchwork [3,of,5] templater: reject bad fillchar argument passed to pad()

login
register
mail settings
Submitter Yuya Nishihara
Date March 18, 2017, 2:12 p.m.
Message ID <b05bfb90f25a8dfc720a.1489846333@mimosa>
Download mbox | patch
Permalink /patch/19436/
State Accepted
Headers show

Comments

Yuya Nishihara - March 18, 2017, 2:12 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1489837124 -32400
#      Sat Mar 18 20:38:44 2017 +0900
# Node ID b05bfb90f25a8dfc720ae5f6b869c0e05f1c1ab1
# Parent  2ddc0cdf8a31d932ce769396cf5bed3a4f840caa
templater: reject bad fillchar argument passed to pad()

Otherwise TypeError would be raised.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -575,6 +575,9 @@  def pad(context, mapping, args):
     fillchar = ' '
     if len(args) > 2:
         fillchar = evalstring(context, mapping, args[2])
+        if len(fillchar) != 1:
+            # i18n: "pad" is a keyword
+            raise error.ParseError(_("pad() expects a single fill character"))
     if len(args) > 3:
         left = evalboolean(context, mapping, args[3])
 
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
@@ -3521,6 +3521,15 @@  Test width argument passed to pad functi
   hg: parse error: pad() expects an integer width
   [255]
 
+Test invalid fillchar passed to pad function
+
+  $ hg log -r 0 -T '{pad(rev, 10, "")}\n'
+  hg: parse error: pad() expects a single fill character
+  [255]
+  $ hg log -r 0 -T '{pad(rev, 10, "--")}\n'
+  hg: parse error: pad() expects a single fill character
+  [255]
+
 Test boolean argument passed to pad function
 
  no crash