Patchwork [05,of,11] templatefuncs: use evaldate() where seems appropriate

login
register
mail settings
Submitter Yuya Nishihara
Date March 31, 2018, 1:49 a.m.
Message ID <5195b8b4c84633ed4529.1522460973@mimosa>
Download mbox | patch
Permalink /patch/30042/
State Accepted
Headers show

Comments

Yuya Nishihara - March 31, 2018, 1:49 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521356302 -32400
#      Sun Mar 18 15:58:22 2018 +0900
# Node ID 5195b8b4c84633ed4529a0c5430c3b04e88f6898
# Parent  01d55beea227cca5b033cfe9dd4a0a068aad5b8c
templatefuncs: use evaldate() where seems appropriate

This means date("today") is allowed.

Also fixes evaldate() to forcibly use the custom error message if specified.

Patch

diff --git a/mercurial/templatefuncs.py b/mercurial/templatefuncs.py
--- a/mercurial/templatefuncs.py
+++ b/mercurial/templatefuncs.py
@@ -52,18 +52,16 @@  def date(context, mapping, args):
         # i18n: "date" is a keyword
         raise error.ParseError(_("date expects one or two arguments"))
 
-    date = evalfuncarg(context, mapping, args[0])
+    date = evaldate(context, mapping, args[0],
+                    # i18n: "date" is a keyword
+                    _("date expects a date information"))
     fmt = None
     if len(args) == 2:
         fmt = evalstring(context, mapping, args[1])
-    try:
-        if fmt is None:
-            return dateutil.datestr(date)
-        else:
-            return dateutil.datestr(date, fmt)
-    except (TypeError, ValueError):
-        # i18n: "date" is a keyword
-        raise error.ParseError(_("date expects a date information"))
+    if fmt is None:
+        return dateutil.datestr(date)
+    else:
+        return dateutil.datestr(date, fmt)
 
 @templatefunc('dict([[key=]value...])', argspec='*args **kwargs')
 def dict_(context, mapping, args):
diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -330,6 +330,10 @@  def unwrapdate(thing, err=None):
         return dateutil.parsedate(thing)
     except AttributeError:
         raise error.ParseError(err or _('not a date tuple nor a string'))
+    except error.ParseError:
+        if not err:
+            raise
+        raise error.ParseError(err)
 
 def evalinteger(context, mapping, arg, err=None):
     return unwrapinteger(evalrawexp(context, mapping, arg), err)