Patchwork templater: allow to pipe generator-type function output to filters

login
register
mail settings
Submitter Yuya Nishihara
Date March 12, 2015, 2:31 p.m.
Message ID <f29939bf06ff33ec50a1.1426170719@mimosa>
Download mbox | patch
Permalink /patch/8020/
State Accepted
Headers show

Comments

Yuya Nishihara - March 12, 2015, 2:31 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1424703895 -32400
#      Tue Feb 24 00:04:55 2015 +0900
# Node ID f29939bf06ff33ec50a1647f9048af25ee20a702
# Parent  7cf9a9e0cf893e7ae82dc576a03c843fd6640438
templater: allow to pipe 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')]'
Matt Mackall - March 12, 2015, 7:27 p.m.
On Thu, 2015-03-12 at 23:31 +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1424703895 -32400
> #      Tue Feb 24 00:04:55 2015 +0900
> # Node ID f29939bf06ff33ec50a1647f9048af25ee20a702
> # Parent  7cf9a9e0cf893e7ae82dc576a03c843fd6640438
> templater: allow to pipe generator-type function output to filters

Queued for default, thanks.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -162,8 +162,13 @@  def buildfilter(exp, context):
 
 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]
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
@@ -1901,6 +1901,11 @@  Thrown an error if a template function d
   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