changeset 24903:09124cce913f stable

templater: fix crash by passing invalid object to date() function "date information" is somewhat obscure, but we call it that way in templatekw.showdate().
author Yuya Nishihara <yuya@tcha.org>
date Sun, 03 May 2015 17:33:14 +0900
parents 986a5c23b1c1
children b5c227f3e461
files mercurial/templater.py tests/test-command-template.t
diffstat 2 files changed, 15 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templater.py	Sun May 03 12:49:15 2015 -0700
+++ b/mercurial/templater.py	Sun May 03 17:33:14 2015 +0900
@@ -226,10 +226,17 @@
         raise error.ParseError(_("date expects one or two arguments"))
 
     date = args[0][0](context, mapping, args[0][1])
+    fmt = None
     if len(args) == 2:
         fmt = stringify(args[1][0](context, mapping, args[1][1]))
-        return util.datestr(date, fmt)
-    return util.datestr(date)
+    try:
+        if fmt is None:
+            return util.datestr(date)
+        else:
+            return util.datestr(date, fmt)
+    except (TypeError, ValueError):
+        # i18n: "date" is a keyword
+        raise error.ParseError(_("date expects a date information"))
 
 def diff(context, mapping, args):
     """:diff([includepattern [, excludepattern]]): Show a diff, optionally
--- a/tests/test-command-template.t	Sun May 03 12:49:15 2015 -0700
+++ b/tests/test-command-template.t	Sun May 03 17:33:14 2015 +0900
@@ -2236,6 +2236,12 @@
   date: 70 01 01 01 +0000
   date: 70 01 01 00 +0000
 
+Test invalid date:
+
+  $ hg log -R latesttag -T '{date(rev)}\n'
+  hg: parse error: date expects a date information
+  [255]
+
 Test string escaping:
 
   $ hg log -R latesttag -r 0 --template '>\n<>\\n<{if(rev, "[>\n<>\\n<]")}>\n<>\\n<\n'