Patchwork [7,of,7] templater: handle exception when applying map operator to non-iterable object

login
register
mail settings
Submitter Yuya Nishihara
Date March 4, 2016, 3:48 p.m.
Message ID <8e6ed4f20dbb321b06c9.1457106529@mimosa>
Download mbox | patch
Permalink /patch/13604/
State Accepted
Headers show

Comments

Yuya Nishihara - March 4, 2016, 3:48 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1455425926 -32400
#      Sun Feb 14 13:58:46 2016 +0900
# Node ID 8e6ed4f20dbb321b06c974560bfd2f88eb369e26
# Parent  c5d854264cf4c299b3b85250809b788459a1faed
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.
Sean Farley - March 4, 2016, 7:06 p.m.
Yuya Nishihara <yuya@tcha.org> writes:

> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1455425926 -32400
> #      Sun Feb 14 13:58:46 2016 +0900
> # Node ID 8e6ed4f20dbb321b06c974560bfd2f88eb369e26
> # Parent  c5d854264cf4c299b3b85250809b788459a1faed
> 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.

This series looks really great. I really like the tidying up of the arg
type errors. Thanks!
Yuya Nishihara - March 5, 2016, 10:47 a.m.
On Fri, 04 Mar 2016 11:06:30 -0800, Sean Farley wrote:
> Yuya Nishihara <yuya@tcha.org> writes:
> > # HG changeset patch
> > # User Yuya Nishihara <yuya@tcha.org>
> > # Date 1455425926 -32400
> > #      Sun Feb 14 13:58:46 2016 +0900
> > # Node ID 8e6ed4f20dbb321b06c974560bfd2f88eb369e26
> > # Parent  c5d854264cf4c299b3b85250809b788459a1faed
> > 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.
> 
> This series looks really great. I really like the tidying up of the arg
> type errors. Thanks!

Thanks. As this series shouldn't include controversial changes, I've pushed
them to the clowncopter.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -305,9 +305,17 @@  def runmap(context, mapping, data):
     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)
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
@@ -2790,6 +2790,14 @@  Test new-style inline templating:
   $ 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'