changeset 28349:7cb2f2438f85

templater: handle exception when applying map operator to non-iterable object Before this, "{noniterable % template}" raised an exception. This tries to provide a better indication for the common case, where a left-hand-side expression is a keyword.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 14 Feb 2016 13:58:46 +0900
parents ccedb17a5657
children 8f9661d1637b
files mercurial/templater.py tests/test-command-template.t
diffstat 2 files changed, 18 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templater.py	Sun Feb 14 13:30:32 2016 +0900
+++ b/mercurial/templater.py	Sun Feb 14 13:58:46 2016 +0900
@@ -305,9 +305,17 @@
     func, data, ctmpl = data
     d = func(context, mapping, data)
     if util.safehasattr(d, 'itermaps'):
-        d = d.itermaps()
+        diter = d.itermaps()
+    else:
+        try:
+            diter = iter(d)
+        except TypeError:
+            if func is runsymbol:
+                raise error.ParseError(_("keyword '%s' is not iterable") % data)
+            else:
+                raise error.ParseError(_("%r is not iterable") % d)
 
-    for i in d:
+    for i in diter:
         lm = mapping.copy()
         if isinstance(i, dict):
             lm.update(i)
--- a/tests/test-command-template.t	Sun Feb 14 13:30:32 2016 +0900
+++ b/tests/test-command-template.t	Sun Feb 14 13:58:46 2016 +0900
@@ -2790,6 +2790,14 @@
   $ hg log -R latesttag -r tip --template 'modified files: {file_mods % " {file}\n"}\n'
   modified files:  .hgtags
   
+
+  $ hg log -R latesttag -r tip -T '{rev % "a"}\n'
+  hg: parse error: keyword 'rev' is not iterable
+  [255]
+  $ hg log -R latesttag -r tip -T '{get(extras, "unknown") % "a"}\n'
+  hg: parse error: None is not iterable
+  [255]
+
 Test the sub function of templating for expansion:
 
   $ hg log -R latesttag -r 10 --template '{sub("[0-9]", "x", "{rev}")}\n'