templater: allow piping generator-type function output to filters
authorYuya Nishihara <yuya@tcha.org>
Tue, 24 Feb 2015 00:04:55 +0900
changeset 24280 6c55e37ba5f2
parent 24279 7cf9a9e0cf89
child 24281 e9ede9b4c2f8
templater: allow piping generator-type function output to filters Template functions use "yield"s assuming that the result will be combined into a string, which means both "f -> str" and "f -> generator" should behave in the same way. Before this patch, piping generator function resulted in a cryptic error. We had to insert "|stringify" in this case. $ hg log --template '{if(author, author)|user}\n' abort: template filter 'userfilter' is not compatible with keyword '[(<function runsymbol at 0x7f5af2e8d8c0>, 'author'), (<function runsymbol at 0x7f5af2e8d8c0>, 'author')]'
mercurial/templater.py
tests/test-command-template.t
--- a/mercurial/templater.py	Wed Mar 11 15:22:34 2015 -0700
+++ b/mercurial/templater.py	Tue Feb 24 00:04:55 2015 +0900
@@ -162,8 +162,13 @@
 
 def runfilter(context, mapping, data):
     func, data, filt = data
+    # func() may return string, generator of strings or arbitrary object such
+    # as date tuple, but filter does not want generator.
+    thing = func(context, mapping, data)
+    if isinstance(thing, types.GeneratorType):
+        thing = stringify(thing)
     try:
-        return filt(func(context, mapping, data))
+        return filt(thing)
     except (ValueError, AttributeError, TypeError):
         if isinstance(data, tuple):
             dt = data[1]
--- a/tests/test-command-template.t	Wed Mar 11 15:22:34 2015 -0700
+++ b/tests/test-command-template.t	Tue Feb 24 00:04:55 2015 +0900
@@ -1901,6 +1901,11 @@
   hg: parse error: unknown function 'foo'
   [255]
 
+Pass generator object created by template function to filter
+
+  $ hg log -l 1 --template '{if(author, author)|user}\n'
+  test
+
 Test diff function:
 
   $ hg diff -c 8